/*
 * 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 FOUNDATION_ACE_INTERFACE_UI_CONTENT_PROXY_H
#define FOUNDATION_ACE_INTERFACE_UI_CONTENT_PROXY_H

#include <atomic>

#include "iremote_proxy.h"
#include "ui_content_errors.h"
#include "ui_content_service_interface.h"
#include "ui_session_request_guard.h"
#include "ui_report_stub.h"

namespace OHOS::Ace {
class ACE_FORCE_EXPORT UIContentServiceProxy : public IRemoteProxy<IUiContentService> {
public:
    using IUiContentService::ExeAppAIFunction;

    explicit UIContentServiceProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<IUiContentService>(impl) {};
    virtual int32_t GetInspectorTree(const std::function<void(std::string, int32_t, bool)>& eventCallback,
        ParamConfig config = ParamConfig(), int32_t timeout = DEFAULT_INSPECTOR_TREE_CALLBACK_TIMEOUT_MS) override;
    virtual int32_t Connect(const EventCallback& eventCallback,
        std::shared_ptr<AppExecFwk::EventHandler> eventHandler = nullptr) override;
    virtual int32_t RegisterClickEventCallback(const EventCallback& eventCallback) override;
    virtual int32_t RegisterRouterChangeEventCallback(const EventCallback& eventCallback) override;
    virtual int32_t RegisterSearchEventCallback(const EventCallback& eventCallback) override;
    virtual int32_t RegisterTextChangeEventCallback(const EventCallback& eventCallback) override;
    virtual int32_t RegisterComponentChangeEventCallback(const EventCallback& eventCallback,
        uint32_t mask = ComponentEventType::COMPONENT_EVENT_ALL) override;
    virtual int32_t RegisterWebUnfocusEventCallback(
        const std::function<void(int64_t accessibilityId, const std::string& data)>& eventCallback) override;
    virtual int32_t RegisterScrollEventCallback(const EventCallback& eventCallback) override;
    virtual int32_t RegisterLifeCycleEventCallback(const EventCallback& eventCallback) override;
    virtual int32_t RegisterSelectTextEventCallback(const EventCallback& eventCallback) override;
    virtual int32_t GetSpecifiedContentOffsets(int32_t id, const std::string& content,
        const std::function<void(std::vector<std::pair<float, float>>)>& eventCallback) override;
    virtual int32_t HighlightSpecifiedContent(int32_t id, const std::string& content,
        const std::vector<std::string>& nodeIds, const std::string& configs) override;
    virtual int32_t SendCommand(int32_t id, const std::string& command) override;
    virtual int32_t SendCommandAsync(int32_t id, const std::string& command) override;
    virtual int32_t SendCommand(const std::string command) override;
    virtual int32_t UnregisterClickEventCallback() override;
    virtual int32_t UnregisterSearchEventCallback() override;
    virtual int32_t UnregisterTextChangeEventCallback() override;
    virtual int32_t UnregisterRouterChangeEventCallback() override;
    virtual int32_t UnregisterComponentChangeEventCallback() override;
    virtual int32_t UnregisterWebUnfocusEventCallback() override;
    virtual int32_t UnregisterScrollEventCallback() override;
    virtual int32_t UnregisterLifeCycleEventCallback() override;
    virtual int32_t UnregisterSelectTextEventCallback() override;
    virtual bool IsConnect() override;
    virtual int32_t GetWebViewTranslateText(
        const std::string& data, const std::function<void(int32_t, std::string)>& eventCallback) override;
    virtual int32_t ResetTranslateTextAll() override;
    virtual int32_t ResetTranslateText(int32_t nodeId) override;
    virtual int32_t GetWebViewCurrentLanguage(const EventCallback& eventCallback) override;
    virtual int32_t GetCurrentPageName(const std::function<void(std::string)>& finishCallback) override;
    virtual int32_t StartWebViewTranslate(
        const std::string& data, const std::function<void(int32_t, std::string)>& eventCallback) override;
    virtual int32_t SendTranslateResult(
        int32_t nodeId, std::vector<std::string> results, std::vector<int32_t> ids) override;
    virtual int32_t EndWebViewTranslate() override;
    virtual int32_t SendTranslateResult(int32_t nodeId, std::string result) override;
    virtual int32_t GetCurrentImagesShowing(
        const std::function<void(std::vector<std::pair<int32_t, std::shared_ptr<Media::PixelMap>>>)>& finishCallback)
        override;
    virtual int32_t GetImagesById(
        const std::vector<int32_t>& arkUIIds,
        const std::function<void(int32_t, const std::unordered_map<int32_t, std::shared_ptr<Media::PixelMap>>&,
            MultiImageQueryErrorCode)>& arkUIfinishCallback,
        const std::map<int32_t, std::vector<int32_t>>& arkWebs,
        const std::function<void(int32_t, const std::map<int32_t, std::map<int32_t,
            std::shared_ptr<Media::PixelMap>>>&, MultiImageQueryErrorCode)>& arkWebfinishCallback) override;
    virtual int32_t GetVisibleInspectorTree(const std::function<void(std::string, int32_t, bool)>& eventCallback,
        ParamConfig config = ParamConfig(), int32_t timeout = DEFAULT_INSPECTOR_TREE_CALLBACK_TIMEOUT_MS) override;
    virtual int32_t GetLatestHitTestNodeInfosForTouch(
        const std::function<void(std::string, int32_t, bool)>& eventCallback,
        InteractionParamConfig config = InteractionParamConfig()) override;
    virtual int32_t ExeAppAIFunction(
        const std::string& funcName, const std::string& params, const sptr<IRemoteObject>& remoteObj, int32_t nodeId,
        const std::function<void(uint32_t, std::string)>& finishCallback) override;
    virtual int32_t RegisterContentChangeCallback(const ContentChangeConfig& config,
        const std::function<void(ChangeType type, const std::string& simpleTree)> callback) override;
    virtual int32_t UnregisterContentChangeCallback() override;
    virtual int32_t GetStateMgmtInfo(const std::string& componentName, const std::string& propertyName,
        const std::string& jsonPath, const std::function<void(std::vector<std::string>)>& eventCallback,
        bool onlyVisible) override;

    virtual int32_t GetWebInfoByRequest(
        int32_t webId,
        const std::string& request,
        const GetWebInfoByRequestCallback& finishCallback) override;
    virtual int32_t GetPageTranslateText(
        const std::string& request, const PageTranslateTextCallback& eventCallback) override;
    virtual int32_t StartPageTranslate(
        const std::string& request, const PageTranslateTextCallback& eventCallback) override;
    virtual int32_t EndPageTranslate() override;
    virtual int32_t ResetPageTranslate(int32_t nodeId = -1) override;
    virtual int32_t SendPageTranslateResult(const std::string& result) override;
    virtual int32_t GetCurrentAbilityLanguageInfo(std::string& language, std::string& region) override;
    virtual int32_t RegisterPageSceneRules(
        const std::string& ruleJson, const PageSceneEventCallback& eventCallback) override;
    virtual int32_t UnregisterPageSceneRules(const std::string& ruleSetId) override;
    virtual int32_t GetPageScene(
        const std::string& ruleJsonOrRuleSetId, const PageSceneEventCallback& eventCallback) override;

private:
    int32_t SendPageTranslateRequest(uint32_t code, const char* caller, const std::string& request,
        const PageTranslateTextCallback* eventCallback, bool isContinuous);
    int32_t SendPageTranslateControlRequest(uint32_t code, const char* caller, int32_t nodeId = -1);
    static inline BrokerDelegator<UIContentServiceProxy> delegator_;
    sptr<UiReportStub> report_ = nullptr;
    bool isConnected_ = false;
    std::atomic_bool currentAbilityLanguageInfoPending_ = false;
};

class ACE_FORCE_EXPORT UiContentProxyRecipient : public IRemoteObject::DeathRecipient {
public:
    using RemoteDiedHandler = std::function<void()>;
    explicit UiContentProxyRecipient(RemoteDiedHandler handler) : handler_(std::move(handler)) {}

    ~UiContentProxyRecipient() override = default;

    void OnRemoteDied(const wptr<IRemoteObject>& remote) override;

private:
    RemoteDiedHandler handler_;
};
} // namespace OHOS::Ace
#endif // FOUNDATION_ACE_INTERFACE_UI_CONTENT_PROXY_H