/*
 * Copyright (C) 2021-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 CALL_STATUS_MANAGER_H
#define CALL_STATUS_MANAGER_H

#include <map>

#include "refbase.h"
#include "ffrt.h"
#include "common_type.h"
#include "call_status_policy.h"
#include "call_incoming_filter_manager.h"
#include "time_wait_helper.h"
#include "voip_call_manager_info.h"
#include "anti_fraud_service_client_type.h"
#ifdef SUPPORT_RTT_CALL
#include "ims_rtt_manager.h"
#endif

/**
 * Singleton
 * @ClassName:CallStatusManager
 * @Description:CallStatusManager is designed for watching call state changes,
 * when call state changes, cellularcall will notify callstatusmanager to handle it.
 * call state: idle,disconnected, disconnecting,dialing,alerting,active,holding,waiting
 * incoming
 */
namespace OHOS {
namespace Telephony {
class SpamCallAdapter;
class IWatchTelephonyNode;
const int32_t SLOT_NUM = 2;
constexpr int32_t DEVICE_PROVISION_UNDEF = -1;
constexpr int32_t DEVICE_PROVISION_INVALID = 0;
constexpr int32_t DEVICE_PROVISION_VALID = 1;

class OOBEStatusObserver : public AAFwk::DataAbilityObserverStub {
public:
    OOBEStatusObserver() = default;
    ~OOBEStatusObserver() = default;
    void OnChange() override;
};

class CallStatusManager : public CallStatusPolicy {
public:
    CallStatusManager();
    ~CallStatusManager();
    int32_t Init();
    int32_t HandleCallReportInfo(const CallDetailInfo &info);
    int32_t ReportCallProcedureEvents(const std::string &callId, const std::string &procedureJsonStr);
    void HandleDsdaInfo(int32_t slotId);
    void SetImsDomainInfo(const sptr<CallBase> call, int32_t imsDomain);
    void SetPhoneIndexInfo(const sptr<CallBase> call, int32_t phoneIndex);
    int32_t HandleCallsReportInfo(const CallDetailsInfo &info);
    int32_t HandleDisconnectedCause(const DisconnectedDetails &details);
    int32_t HandleEventResultReportInfo(const CellularCallEventInfo &info);
    int32_t HandleOttEventReportInfo(const OttCallEventInfo &info);
    int32_t HandleVoipCallReportInfo(const CallDetailInfo &info);
    void CallFilterCompleteResult(const CallDetailInfo &info);
    int32_t HandleVoipEventReportInfo(const VoipCallEventInfo &info);
    void HandleCeliaCall(sptr<CallBase> &call);
    void TriggerAntiFraud(int32_t antiFraudState, const OHOS::AntiFraudService::AntiFraudResultExt &antiFraudResultExt);
    int32_t GetAntiFraudSlotId();
    int32_t GetAntiFraudIndex();
    static int32_t GetDevProvisioned();
    static void RegisterObserver();
    static void UpdateDevProvisioned();
#ifdef SUPPORT_RTT_CALL
    void HandleRttEventInfo(const ImsRTTEventType &eventType);
#endif

private:
    void InitCallBaseEvent();
    int32_t IncomingHandle(const CallDetailInfo &info);
    int32_t FinalizeIncomingState(sptr<CallBase> &call, const TelCallState nextState);
    int32_t PrepareIncomingCall(const CallDetailInfo &info, bool &isExisted);
    int32_t IncomingVoipCallHandle(const CallDetailInfo &info);
    int32_t OutgoingVoipCallHandle(const CallDetailInfo &info);
    int32_t AnsweredVoipCallHandle(const CallDetailInfo &info);
    void HangUpCellularCall();
    void HandleAntiFraudState(const sptr<CallBase> &call, const CallDetailInfo &info);
    int32_t DisconnectingVoipCallHandle(const CallDetailInfo &info);
    int32_t DialingHandle(const CallDetailInfo &info);
    int32_t ActiveHandle(const CallDetailInfo &info);
    int32_t ActiveVoipCallHandle(const CallDetailInfo &info);
    int32_t HoldingHandle(const CallDetailInfo &info);
    int32_t WaitingHandle(const CallDetailInfo &info);
    int32_t AlertHandle(const CallDetailInfo &info);
    int32_t DisconnectingHandle(const CallDetailInfo &info);
    int32_t DisconnectedVoipCallHandle(const CallDetailInfo &info);
    int32_t DisconnectedHandle(const CallDetailInfo &info);
    void HandleHoldCallOrAutoAnswerCall(const sptr<CallBase> call,
        std::vector<std::u16string> callIdList, CallRunningState previousState, TelCallState priorState);
    void IsCanUnHold(int32_t activeCallNum, int32_t waitingCallNum, int32_t size, bool &canUnHold);
    void AutoAnswer(int32_t activeCallNum, int32_t waitingCallNum);
    void AutoHandleForDsda(
        bool canSwitchCallState, TelCallState priorState, int32_t activeCallNum, int32_t slotId, bool continueAnswer);
    void AutoAnswerForVoiceCall(sptr<CallBase> ringCall, int32_t slotId, bool continueAnswer);
    void AutoAnswerForVideoCall(int32_t activeCallNum);
    void AutoUnHoldForDsda(bool canSwitchCallState, TelCallState priorState, int32_t activeCallNum, int32_t slotId);
    sptr<CallBase> CreateNewCall(const CallDetailInfo &info, CallDirection dir);
    void SetCallParams(const sptr<CallBase> &callPtr, const CallDetailInfo &info);
    sptr<CallBase> CreateNewCallByCallType(
        DialParaInfo &paraInfo, const CallDetailInfo &info, CallDirection dir, AppExecFwk::PacMap &extras);
    sptr<CallBase> CreateNewCallByCallTypeEx(
        DialParaInfo &paraInfo, const CallDetailInfo &info, CallDirection dir, AppExecFwk::PacMap &extras);
    sptr<CallBase> RefreshCallIfNecessary(const sptr<CallBase> &call, const CallDetailInfo &info);
    sptr<CallBase> RefreshCall(const sptr<CallBase> &call, const CallDetailInfo &info);
    void SetOriginalCallTypeForActiveState(sptr<CallBase> &call);
    void SetOriginalCallTypeForDisconnectState(sptr<CallBase> &call);
    void PackParaInfo(
        DialParaInfo &paraInfo, const CallDetailInfo &info, CallDirection dir, AppExecFwk::PacMap &extras);
    int32_t UpdateCallState(sptr<CallBase> &call, TelCallState nextState);
    int32_t ToSpeakerPhone(sptr<CallBase> &call);
    int32_t TurnOffMute(sptr<CallBase> &call);
    int32_t IncomingFilterPolicy(const CallDetailInfo &info);
    void QueryCallerInfo(ContactInfo &contactInfo, std::string phoneNum);
    void SetAntiFraudSlotId(int32_t slotId);
    void SetAntiFraudIndex(int32_t index);
    void SetupAntiFraudService(const sptr<CallBase> &call, const CallDetailInfo &info);
    uint32_t GetAntiFraudDetectType(
        int32_t slotId, const CallDirection callDirection, const VideoStateType videoStateType);
    uint32_t GetAntiFraudDetectType(int32_t slotId, const CallDirection callDirection);
    void StopAntiFraudDetect(sptr<CallBase> &call, const CallDetailInfo &info);
    void PauseAntiFraudDetect(sptr<CallBase> &call, const CallDetailInfo &info);
    void UpdateAntiFraudState(sptr<CallBase> &call, int32_t antiFraudState,
        const OHOS::AntiFraudService::AntiFraudResultExt &antiFraudResultExt);
    bool IsContactPhoneNum(const std::string &phoneNum);
    void HandleVideoCallInAdvsecMode(const sptr<CallBase> &call, const CallDetailInfo &info);
    bool IsTrustedNumber(MarkType markType, std::string phoneNumber);
    int32_t UpdateDialingCallInfo(const CallDetailInfo &info);
    void SetContactInfo(sptr<CallBase> &call, std::string phoneNum);
    bool SetBluetoothCallContactInfo(sptr<CallBase> &call, ContactInfo &contactInfo, std::string phoneNum);
    void StartAntiFraudDetectTask(const sptr<CallBase> &call, const CallDetailInfo &info,
        const std::string &phoneNum, const VideoStateType videoStateType);
    int32_t HandleRejectCall(sptr<CallBase> &call, bool isBlock);
    bool ShouldRejectIncomingCall(sptr<CallBase> &call);
    bool ShouldBlockIncomingCall(const sptr<CallBase> &call, const CallDetailInfo &info);
    bool IsRingOnceCall(const sptr<CallBase> &call, const CallDetailInfo &info);
    int32_t HandleRingOnceCall(sptr<CallBase> &call);
    void CarrierAndVoipConflictProcess(int32_t callId);
    void SetVideoCallState(sptr<CallBase> &call, TelCallState nextState);
    bool IsFocusModeOpen();
    bool IsRejectCall(sptr<CallBase> &call, const CallDetailInfo &info, bool &block);
    bool PublishIncomingCallBlockInfo(const sptr<CallBase> &callObjectPtr, bool block);
    void CreateAndSaveNewCall(const CallDetailInfo &info, CallDirection direction);
    int32_t UpdateCallStateAndHandleDsdsMode(const CallDetailInfo &info, sptr<CallBase> &call);
    bool IsDcCallConneceted();
    void HandleDialWhenHolding(int32_t callId, sptr<CallBase> &call);
    void SetConferenceCall(std::vector<sptr<CallBase>>);
    std::vector<sptr<CallBase>> GetConferenceCallList(int32_t slotId);
    void UpdateCallDetailsInfo(const CallDetailsInfo &info);
    bool IsDistributeCallSourceStatus();
    void HandleBluetoothCallReportInfo(const CallDetailInfo &info);
    void SetBtCallDialByPhone(const sptr<CallBase> &call, bool isBtCallDialByPhone);
    void BtCallDialingHandle(sptr<CallBase> &call, const CallDetailInfo &info);
    void SetDistributedDeviceDialing(sptr<CallBase> call, bool isDistributedDeviceDialing);
    void BtCallDialingHandleFirst(sptr<CallBase> call, const CallDetailInfo &infol);
    int32_t HandleCallReportInfoEx(const CallDetailInfo &info);
    void ClearPendingState(sptr<CallBase> &call);
    void RefreshCallDisconnectReason(const sptr<CallBase> &call, int32_t reason, const std::string &message);
    bool AutoAnswerVideoCallForNotDsda(const sptr<CallBase> disconnectedCall, int32_t activeCallNum);
#ifdef CALL_MANAGER_WATCH_CALL_BLOCKING
    bool HandleWatchCallDisposition(std::shared_ptr<SpamCallAdapter> &spamCallAdapterPtr, const sptr<CallBase> &call);
#endif

#ifdef NOT_SUPPORT_MULTICALL
    void AnswerPendingCall();
#endif
    bool IsFromTheSameNumberAtTheSameTime(const sptr<CallBase> &newCall);
    void ModifyEsimType();
    int32_t RefreshOldCall(const CallDetailInfo &info, bool &isExistedOldCall);
    bool IsCallMotionRecognitionEnable(const std::string &key);
    void StartInComingCallMotionRecognition();
    void StartOutGoingCallMotionRecognition();
    void StopCallMotionRecognition(TelCallState nextState);
    bool UpdateDialingHandle(const CallDetailInfo &info, bool &isDistributedDeviceDialing);
    bool RefreshDialingStateByOtherState(sptr<CallBase> &call, const CallDetailInfo &info);
    void PackVoipCallInfo(DialParaInfo &paraInfo, const CallDetailInfo &info);
#ifdef CALL_MANAGER_WATCH_CALL_BLOCKING
    void InitWatchSystemServiceWrapper();
    void DeInitWatchSystemServiceWrapper();
#endif

private:
    CallDetailInfo callReportInfo_;
    CallDetailsInfo callDetailsInfo_[SLOT_NUM];
    CallDetailsInfo tmpCallDetailsInfo_[SLOT_NUM];
    void HandleConnectingCallReportInfo(const CallDetailInfo &info);
    sptr<CallIncomingFilterManager> CallIncomingFilterManagerPtr_;
    std::map<RequestResultEventId, CallAbilityEventId> mEventIdTransferMap_;
    std::map<OttCallEventId, CallAbilityEventId> mOttEventIdTransferMap_;
    VideoStateType priorVideoState_[SLOT_NUM] = {VideoStateType::TYPE_VOICE};
    const std::string DETAIL_INFO = "detail_info";
    const std::string FORMAT_PHONE_NUMBER = "format_phone_number";
    const std::string CONTENT_TYPE = "content_type";
    const std::string PHONE = "phone";
    const std::string TYPE_ID = "type_id";
    const std::string IS_DELETED = "is_deleted";
    const int32_t CALL_NUMBER = 2;
    std::unique_ptr<TimeWaitHelper> timeWaitHelper_ {nullptr};
    std::chrono::system_clock::time_point detectStartTime_ = std::chrono::system_clock::from_time_t(0);
    int32_t antiFraudSlotId_ = -1;
    int32_t antiFraudIndex_ = -1;
    ffrt::mutex mutex_;
    static sptr<OOBEStatusObserver> oobeStatusObserver_;
    static int32_t deviceProvisioned_;
#ifdef CALL_MANAGER_WATCH_CALL_BLOCKING
    void *watchSystemServiceHandler_{nullptr};
    IWatchTelephonyNode *watchTelephonyNode_{nullptr};
#endif
};
} // namespace Telephony
} // namespace OHOS

#endif // CALL_STATUS_MANAGER_H