/*
 * Copyright (c) 2022-2026 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_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H
#define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H

#include <array>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <vector>

#include "app_mgr_constants.h"
#include "app_mgr_proxy.h"
#include "application_state_observer_stub.h"
#include "delay_suspend_info_ex.h"
#include "device_info_manager.h"
#include "input_manager.h"
#include "iremote_object.h"
#include "key_info.h"
#include "pkg_delay_suspend_info.h"
#include "suspend_controller.h"
#include "timer_manager.h"

namespace OHOS {
namespace BackgroundTaskMgr {
class DecisionMaker : public IEventListener {
public:
    DecisionMaker(const std::shared_ptr<TimerManager>& timerManager, const std::shared_ptr<DeviceInfoManager>& device);
    ~DecisionMaker() override;
    ErrCode Decide(const std::shared_ptr<KeyInfo>& key, const std::shared_ptr<DelaySuspendInfoEx>& delayInfo);
    ErrCode PauseTransientTaskTimeForInner(int32_t uid, const std::string &name);
    ErrCode StartTransientTaskTimeForInner(int32_t uid, const std::string &name);
    void RemoveRequest(const std::shared_ptr<KeyInfo>& key, const int32_t requestId);
    int32_t GetRemainingDelayTime(const std::shared_ptr<KeyInfo>& key, const int32_t requestId);
    void OnInputEvent(const EventInfo& eventInfo) override;
    int32_t GetQuota(const std::shared_ptr<KeyInfo>& key);
    bool IsFrontApp(const string& pkgName, int32_t uid);
    vector<int32_t> GetRequestIdListByKey(const std::shared_ptr<KeyInfo>& key);
    void OnProcessStateChanged(const AppExecFwk::ProcessData &processData);
    void OnProcessDied(const AppExecFwk::ProcessData &processData);
    bool IsUidForeground(int32_t uid);
    void HandleStateChange(const std::string &bundleName, int32_t uid, bool isForeground, bool isBackground);

private:
    int32_t NewDelaySuspendRequestId();
    int32_t GetDelayTime();
    void HandleScreenOn();
    void HandleScreenOff();
    void HandlePowerConnect();
    void HandlePowerDisconnect();
    void HandlePkgRemove(const EventInfo& eventInfo);

    bool GetAppMgrProxy();
    void ResetDayQuotaLocked();
    bool IsAfterOneDay(int64_t lastRequestTime, int64_t currentTime);
    bool CanStartAccountingLocked(const std::shared_ptr<PkgDelaySuspendInfo>& pkgInfo);
    int GetAllowRequestTime();
    ErrCode CheckQuotaTime(const std::shared_ptr<PkgDelaySuspendInfo>& pkgInfo, const std::string &name,
        int32_t uid, const std::shared_ptr<KeyInfo>& key, bool& needSetTime);
    void UpdateForegroundUidPidMap(int32_t uid, int32_t pid, bool isForeground);
    ErrCode TryStartAccounting(int32_t uid, const std::string &bundleName);

    const int32_t initRequestId_ = 1;
    int32_t requestId_ {initRequestId_};
    std::mutex lock_;
    int64_t lastRequestTime_ {0};
    SuspendController suspendController_;
    std::shared_ptr<TimerManager> timerManager_ {nullptr};
    std::shared_ptr<DeviceInfoManager> deviceInfoManager_ {nullptr};
    std::map<std::shared_ptr<KeyInfo>, std::shared_ptr<PkgDelaySuspendInfo>, KeyInfoComp> pkgDelaySuspendInfoMap_;
    std::map<std::shared_ptr<KeyInfo>, int32_t, KeyInfoComp> pkgBgDurationMap_;
    std::recursive_mutex recMutex_;
    std::map<int32_t, std::set<int32_t>> foregroundUidPidMap_;
};
}  // namespace BackgroundTaskMgr
}  // namespace OHOS
#endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H