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

#include "datashare_helper.h"
#include "errors.h"
#include "setting_observer.h"

namespace OHOS {
namespace PowerMgr {
class SettingProvider : public NoCopyable {
public:
    static SettingProvider& GetInstance(int32_t systemAbilityId);
    ErrCode GetStringValue(const std::string& key, std::string& value);
    ErrCode GetIntValue(const std::string& key, int32_t& value);
    ErrCode GetLongValue(const std::string& key, int64_t& value);
    ErrCode GetBoolValue(const std::string& key, bool& value);
    ErrCode PutStringValue(const std::string& key, const std::string& value, bool needNotify = true);
    ErrCode PutIntValue(const std::string& key, int32_t value, bool needNotify = true);
    ErrCode PutLongValue(const std::string& key, int64_t value, bool needNotify = true);
    ErrCode PutBoolValue(const std::string& key, bool value, bool needNotify = true);
    bool IsValidKey(const std::string& key);
    static bool AddMultiUserKey(const std::string& key);
    sptr<SettingObserver> CreateObserver(const std::string& key, const SettingObserver::UpdateFunc& func);
    static void ExecRegisterCb(const sptr<SettingObserver>& observer);
    ErrCode RegisterObserver(const sptr<SettingObserver>& observer);
    ErrCode UnregisterObserver(const sptr<SettingObserver>& observer);
    void UpdateCurrentUserId();
    void CopyDataForUpdateScene();

protected:
    ~SettingProvider() override;

private:
#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
    // AC for Alternating Current, means charing supply
    // DC for Direct Current, means battery supply
    static constexpr const char* SETTING_DISPLAY_AC_OFF_TIME_KEY {"settings.display.ac.screen_off_timeout"};
    static constexpr const char* SETTING_DISPLAY_DC_OFF_TIME_KEY {"settings.display.dc.screen_off_timeout"};
    static constexpr const char* SETTING_POWER_AC_SLEEP_TIME_KEY {"settings.power.ac.sleep_timeout"};
    static constexpr const char* SETTING_POWER_DC_SLEEP_TIME_KEY {"settings.power.dc.sleep_timeout"};
    static constexpr const char* SETTING_POWER_AC_SUSPEND_SOURCES_KEY {"settings.power.ac.suspend_sources"};
    static constexpr const char* SETTING_POWER_DC_SUSPEND_SOURCES_KEY {"settings.power.dc.suspend_sources"};
#else
    static constexpr const char* SETTING_DISPLAY_OFF_TIME_KEY {"settings.display.screen_off_timeout"};
    static constexpr const char* SETTING_POWER_SUSPEND_SOURCES_KEY {"settings.power.suspend_sources"};
#endif
    static constexpr const char* SETTING_AUTO_ADJUST_BRIGHTNESS_KEY {"settings.display.auto_screen_brightness"};
    static constexpr const char* SETTING_BRIGHTNESS_KEY {"settings.display.screen_brightness_status"};
    static constexpr const char* SETTING_VIBRATION_KEY {"physic_navi_haptic_feedback_enabled"};
    static constexpr const char* SETTING_WINDOW_ROTATION_KEY {"settings.general.accelerometer_rotation_status"};
    static constexpr const char* SETTING_POWER_WAKEUP_SOURCES_KEY {"settings.power.wakeup_sources"};
    static constexpr const char* SETTING_INTELL_VOICE_KEY {"intell_voice_trigger_enabled"};
    static constexpr const char* SETTING_POWER_WAKEUP_DOUBLE_KEY {"settings.power.wakeup_double_click"};
    static constexpr const char* SETTING_POWER_WAKEUP_PICKUP_KEY {"settings.power.wakeup_pick_up"};
    static constexpr const char* SETTING_POWER_MODE_KEY  {"settings.power.smart_mode_status"};
    static constexpr const char* SETTING_POWER_MODE_BACKUP_KEY  {"settings.power.smart_mode_status.backup"};
    static constexpr const char* SETTING_POWER_WAKEUP_LID_KEY {"settings.power.wakeup_lid"};
    static constexpr const char* SETTING_DURING_CALL_STATE_KEY {"during_call_state"};
#ifdef POWER_MANAGER_SCREEN_SAVER
    static constexpr const char* SETTING_AC_SCREEN_SAVER_TIME_KEY {"settings.momentx.show_wait_time_with_charging"};
    static constexpr const char* SETTING_DC_SCREEN_SAVER_TIME_KEY {"settings.momentx.show_wait_time_with_battery"};
#endif
    static constexpr const int32_t INITIAL_USER_ID = 100;
    static constexpr const uint32_t MULTI_USER_STR_VEC_SIZE_MAX = 1000;

    static inline std::atomic<SettingProvider*> instance_{nullptr};
    static inline sptr<IRemoteObject> remoteObj_;
    static inline int32_t currentUserId_ = INITIAL_USER_ID;
    static inline std::vector<std::string> needMultiUserStrVec = {
        SETTING_POWER_WAKEUP_DOUBLE_KEY,
        SETTING_POWER_WAKEUP_PICKUP_KEY,
        SETTING_POWER_WAKEUP_SOURCES_KEY,
        SETTING_DURING_CALL_STATE_KEY,
#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
        SETTING_DISPLAY_AC_OFF_TIME_KEY,
        SETTING_DISPLAY_DC_OFF_TIME_KEY,
        SETTING_POWER_AC_SLEEP_TIME_KEY,
        SETTING_POWER_DC_SLEEP_TIME_KEY,
        SETTING_POWER_AC_SUSPEND_SOURCES_KEY,
        SETTING_POWER_DC_SUSPEND_SOURCES_KEY,
#endif
#ifdef POWER_MANAGER_SCREEN_SAVER
        SETTING_AC_SCREEN_SAVER_TIME_KEY,
        SETTING_DC_SCREEN_SAVER_TIME_KEY
#endif
    };

    static void Initialize(int32_t systemAbilityId);
    static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string& key);
    static bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper);
    static Uri AssembleUri(const std::string& key);
    static bool IsNeedMultiUser(const std::string& key);
    static std::string ReplaceUserIdForUri(int32_t userId);
    bool IsNeedDataMigrationCopy();
    void DataMigrationCopy();
    ErrCode GetStringValueGlobal(const std::string& key, std::string& value);
    bool IsValidKeyGlobal(const std::string& key);
};
} // namespace PowerMgr
} // namespace OHOS
#endif // POWERMGR_POWER_MANAGER_POWER_SETTING_HELPER_H