/*
 * Copyright (c) 2025 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 VPE_VIDEO_PROCESSING_CLENT_H
#define VPE_VIDEO_PROCESSING_CLENT_H

#include <atomic>
#include <cinttypes>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <string>
#include <vector>

#include "ipc_types.h"
#include "iremote_object.h"
#include "refbase.h"
#include "system_ability_load_callback_stub.h"

#include "algorithm_errors.h"
#include "surface_buffer_info.h"
#include "video_processing_service_manager_proxy.h"
#include "vpe_log.h"

namespace OHOS {
namespace Media {
namespace VideoProcessingEngine {
class VideoProcessingManager {
public:
    static VideoProcessingManager& GetInstance();

    /*
     * @brief Initialize the client environment.
     *
     */
    void Connect();

    /*
     * @brief Clear the client environment.
     *
     */
    void Disconnect();

    /*
     * @brief Read file from system to pass surface buffer to VPE module.
     * @param key
     */
    ErrCode LoadInfo(int32_t key, SurfaceBufferInfo& bufferInfo);

    void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
    void LoadSystemAbilityFail();

    /*
     * @brief Create a new client to access feature algorithm such as HDR2SDR, SDR2HDR, AISR and etc.
     * @param feature The name of feature algorithm such as "AisrImage".
     * @param clientName The name of caller process.
     * @param clientID The unique client ID generated by feature algorithms is used for other algorithm methods.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode Create(const std::string& feature, const std::string& clientName, uint32_t& clientID);

    /*
     * @brief Destroy the specified client.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode Destroy(uint32_t clientID);

    /*
     * @brief Set parameters to feature algorithm.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param tag A int value that is used to specify the parameter.
     * @param parameter The variable-length buffer is used to transfer the actual parameter.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode SetParameter(uint32_t clientID, int32_t tag, const std::vector<uint8_t>& parameter);

    /*
     * @brief Set parameters to feature algorithm.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param tag A int value that is used to specify the parameter.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode SetParameter(uint32_t clientID, int32_t tag);

    /*
     * @brief Set parameters to feature algorithm.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param tag A int value that is used to specify the parameter.
     * @param parameter The parameter for the tag.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    template <typename T>
    VPEAlgoErrCode SetParameter(uint32_t clientID, int32_t tag, const T& parameter)
    {
        std::vector<uint8_t> param;
        param.resize(sizeof(parameter));
        *reinterpret_cast<T*>(param.data()) = parameter;
        return SetParameter(clientID, tag, param);
    }

    /*
     * @brief Get parameters from feature algorithm.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param tag A int value that is used to specify the parameter.
     * @param parameter The variable-length buffer is used to transfer the actual parameter.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode GetParameter(uint32_t clientID, int32_t tag, std::vector<uint8_t>& parameter);

    /*
     * @brief Get parameters from feature algorithm.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param tag A int value that is used to specify the parameter.
     * @param parameter The parameter for the tag.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    template <typename T>
    VPEAlgoErrCode GetParameter(uint32_t clientID, int32_t tag, T& parameter)
    {
        std::vector<uint8_t> param;
        param.resize(sizeof(parameter));
        *reinterpret_cast<T*>(param.data()) = parameter;
        auto ret = GetParameter(clientID, tag, param);
        parameter = *reinterpret_cast<T*>(param.data());
        return ret;
    }

    /*
     * @brief Update metadata of the surface buffer.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param image The surface buffer that contain the image content.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode UpdateMetadata(uint32_t clientID, SurfaceBufferInfo& image);

    /*
     * @brief Process the image buffer.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param input Input surface buffer of image.
     * @param output Output surface buffer of image.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode Process(uint32_t clientID, const SurfaceBufferInfo& input, SurfaceBufferInfo& output);

    /*
     * @brief Composition from dual-layer HDR images to single-layer HDR images.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param inputSdrImage Input surface buffer of SDR image.
     * @param inputGainmap Input surface buffer of gainmap.
     * @param outputHdrImage Output surface buffer of HDR image.
     * @param legacy A bool value that indicates weather the format of dual-layer HDR image is old format.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode ComposeImage(uint32_t clientID, const SurfaceBufferInfo& inputSdrImage,
        const SurfaceBufferInfo& inputGainmap, SurfaceBufferInfo& outputHdrImage, bool legacy);

    /*
     * @brief Decomposition from single-layer HDR images to dual-layer HDR images.
     * @param clientID The unique client ID generated by {@linke Create}.
     * @param inputImage Input surface buffer of HDR image.
     * @param outputSdrImage Output surface buffer of SDR image.
     * @param outputGainmap Output surface buffer of gainmap.
     * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h.
     */
    VPEAlgoErrCode DecomposeImage(uint32_t clientID, const SurfaceBufferInfo& inputImage,
        SurfaceBufferInfo& outputSdrImage, SurfaceBufferInfo& outputGainmap);

private:
    // Inner callback class for SA loading
    class LoadCallback : public SystemAbilityLoadCallbackStub {
    public:
        LoadCallback(std::function<void(const sptr<IRemoteObject>&)>&& onSuccess, std::function<void(void)>&& onFail)
            : onSuccess_(onSuccess), onFail_(onFail) {}
        virtual ~LoadCallback() = default;
        LoadCallback(const LoadCallback&) = delete;
        LoadCallback& operator=(const LoadCallback&) = delete;
        LoadCallback(LoadCallback&&) = delete;
        LoadCallback& operator=(LoadCallback&&) = delete;

        void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject) final;
        void OnLoadSystemAbilityFail(int32_t systemAbilityId) final;

    private:
        std::function<void(const sptr<IRemoteObject>&)> onSuccess_;
        std::function<void(void)> onFail_;
    };

    class DeathObserver : public IPCObjectProxy::DeathRecipient {
    public:
        DeathObserver(std::function<void(const wptr<IRemoteObject>&)>&& onRemoteDied) : onRemoteDied_(onRemoteDied) {}
        virtual ~DeathObserver() = default;
        DeathObserver(const DeathObserver&) = delete;
        DeathObserver& operator=(const DeathObserver&) = delete;
        DeathObserver(DeathObserver&&) = delete;
        DeathObserver& operator=(DeathObserver&&) = delete;

        void OnRemoteDied(const wptr<IRemoteObject>& remoteObject) final;

    private:
        std::function<void(const wptr<IRemoteObject>&)> onRemoteDied_;
    };

    VideoProcessingManager() = default;
    virtual ~VideoProcessingManager() = default;
    VideoProcessingManager(const VideoProcessingManager&) = delete;
    VideoProcessingManager& operator=(const VideoProcessingManager&) = delete;
    VideoProcessingManager(VideoProcessingManager&&) = delete;
    VideoProcessingManager& operator=(VideoProcessingManager&&) = delete;

    sptr<IVideoProcessingServiceManager> GetService();
    void OnSaLoad(const sptr<IRemoteObject>& remoteObject);
    void OnSaDied(const wptr<IRemoteObject>& remoteObject);
    VPEAlgoErrCode Execute(std::function<ErrCode(sptr<IVideoProcessingServiceManager>&)>&& operation,
        const LogInfo& logInfo);
    void ClearSa();
    void ClearSaLocked();

    std::condition_variable cvProxy_{};
    std::mutex lock_{};
    // Guarded by lock_ begin
    std::atomic<bool> isLoading_{};
    sptr<IVideoProcessingServiceManager> proxy_{};
    // Guarded by lock_ end
    std::atomic<int> deadRetryCount_{};
};
} // namespace VideoProcessingEngine
} // namespace Media
} // namespace OHOS

#endif // VPE_VIDEO_PROCESSING_CLENT_H