/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GNSS_GEOFENCE_REQUEST_H
#define GNSS_GEOFENCE_REQUEST_H

#include <mutex>
#include <vector>
#include "geofence_definition.h"
#include <parcel.h>
#include "iremote_object.h"
#include "nlohmann/json.hpp"

namespace OHOS {
namespace Notification {
    class NotificationRequest;
}

namespace AbilityRuntime::WantAgent {
    class WantAgent;
}

namespace Location {
typedef struct {
    double latitude;
    double longitude;
    double radius;
    double expiration;
    CoordinateSystemType coordinateSystemType;
} GeoFence;

class GeofenceRequest : public Parcelable {
public:
    GeofenceRequest();

    GeofenceRequest(GeofenceRequest& geofenceRequest);

    ~GeofenceRequest();

    GeoFence GetGeofence();
    void SetGeofence(GeoFence geofence);

    int GetScenario();

    void SetScenario(int scenario);

    int GetLoiterTimeMs();

    void SetLoiterTimeMs(int loiterTimeMs);

    void SetWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent);

    std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> GetWantAgent();

    std::vector<GeofenceTransitionEvent> GetGeofenceTransitionEventList();

    void SetGeofenceTransitionEvent(GeofenceTransitionEvent status);

    void SetGeofenceTransitionEventList(std::vector<GeofenceTransitionEvent> statusList);

    std::vector<std::shared_ptr<OHOS::Notification::NotificationRequest>> GetNotificationRequestList();

    void SetNotificationRequest(std::shared_ptr<OHOS::Notification::NotificationRequest> request);

    void SetNotificationRequestList(std::vector<std::shared_ptr<OHOS::Notification::NotificationRequest>> requestList);

    void SetGeofenceTransitionCallback(const sptr<IRemoteObject>& callback);

    sptr<IRemoteObject> GetGeofenceTransitionCallback();

    int GetFenceId();

    void SetFenceId(int fenceId);

    const std::string& GetBundleName();

    void SetBundleName(const std::string& bundleName);

    const std::string& GetFenceExtensionAbilityName();

    void SetFenceExtensionAbilityName(const std::string& fenceExtensionAbilityName);

    int32_t GetUid();

    void SetUid(int32_t uid);

    int32_t GetPid();
 
    void SetPid(int32_t pid);
 
    uint32_t GetTokenId();
 
    void SetTokenId(uint32_t tokenId);
 
    uint32_t GetTokenIdEx();
 
    void SetTokenIdEx(uint32_t tokenIdEx);
 
    uint32_t GetFirstTokenId();
 
    void SetFirstTokenId(uint32_t firstTokenId);

    bool GetAppAliveStatus();

    void SetAppAliveStatus(bool appAliveStatus);

    int64_t GetRequestExpirationTimeStamp();

    void SetRequestExpirationTimeStamp(int64_t requestExpirationTimeStamp);
    void SetTransitionCallbackDeathRecipient(const sptr<IRemoteObject::DeathRecipient>& recipient);
    sptr<IRemoteObject::DeathRecipient> GetTransitionCallbackRecipient();

    void ReadFromParcel(Parcel& parcel);
    void ReadAppInfo(Parcel& parcel);
    bool Marshalling(Parcel& parcel) const override;
    static std::shared_ptr<GeofenceRequest> UnmarshallingShared(Parcel& parcel);
    static GeofenceRequest* Unmarshalling(Parcel& parcel);
    bool ToJson(nlohmann::json &jsonObject);
    static std::shared_ptr<GeofenceRequest> FromJson(const nlohmann::json &jsonObject);
    static void ConvertNotificationInfo(std::shared_ptr<GeofenceRequest>& request, const nlohmann::json &jsonObject);
    static void ConvertWantAgentInfo(std::shared_ptr<GeofenceRequest>& request, const nlohmann::json &jsonObject);
    static void ConvertTransitionEventInfo(std::shared_ptr<GeofenceRequest>& request, const nlohmann::json &jsonObject);
    static void ConvertGeofenceRequestInfo(std::shared_ptr<GeofenceRequest>& request, const nlohmann::json &jsonObject);
    static void ConvertGeofenceRequestInfoExt(
        std::shared_ptr<GeofenceRequest>& request, const nlohmann::json &jsonObject);
    bool IsTokenIdValid();

private:
    static void ConvertGeoFenceInfo(const nlohmann::json &geofenceObj, GeoFence& geofence);
    std::vector<GeofenceTransitionEvent> transitionStatusList_;
    std::vector<std::shared_ptr<OHOS::Notification::NotificationRequest>> notificationRequestList_;
    sptr<IRemoteObject> callback_ = nullptr;
    sptr<IRemoteObject::DeathRecipient> transitionCallbackDeathRecipient_;
    GeoFence geofence_{0.0, 0.0, 0.0, WGS84};
    int scenario_;
    int loiterTimeMs_;
    int fenceId_;
    int32_t uid_;
    int32_t pid_;
    uint32_t tokenId_;
    uint32_t tokenIdEx_;
    uint32_t firstTokenId_;
    std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent_;
    std::string bundleName_;
    bool appAliveStatus_;
    int64_t requestExpirationTimeStamp_ = 0;
    mutable std::mutex geofenceRequestMutex_;
    std::string fenceExtensionAbilityName_;
};
} // namespace Location
} // namespace OHOS
#endif // GNSS_GEOFENCE_REQUEST_H