local GameSettingUI = class("GameSettingUI", BaseUI)

local CHECK_WHITE_LIST_COUNT = 5
local GameSettingConst = GConst.GameSettingConst
local BG_DEFAULT_H = 881

function GameSettingUI:ctor()
    self.clickMusicCount = 0
end

function GameSettingUI:isFullScreen()
    return false
end

function GameSettingUI:getPrefabPath()
    return "assets/prefabs/ui/setting/game_setting_ui.prefab"
end

function GameSettingUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()
    self:_display()
    self:_addListeners()
    self:_bind()
    self:addEventListener(EventManager.CUSTOM_EVENT.BIND_ACCOUNT_SUCCESS, function()
        self:refreshAccountInfo()
    end)
end

function GameSettingUI:onPressBackspace()
	self:closeUI()
end

function GameSettingUI:_display()
    self.uiMap["game_setting_ui.bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.SETTING_DESC))
    self.uiMap["game_setting_ui.bg.music_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SETTING_DESC_MUSIC))
    self.uiMap["game_setting_ui.bg.voice_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SETTING_DESC_VOICE))
    self.uiMap["game_setting_ui.bg.language_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SETTING_DESC_LANGUAGE))
    self.uiMap["game_setting_ui.bg.tx_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.SERVICE_DESC))
    self.uiMap["game_setting_ui.bg.tx_2"]:setText(I18N:getGlobalText(I18N.GlobalConst.PRIVACY_DESC))
    local version = CS.BF.BFMain.Instance.GameLaunchMgr:GetCurrentVersion()
	self.uiMap["game_setting_ui.bg.version_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLIENT_VERSION, version))
    local language = I18N:getCurLanguage()
    -- 超框选择小字体特殊处理
    if language == "id" or language == "pt" then
        language = language .."_1"
    end
    self.uiMap["game_setting_ui.bg.language_btn.status"]:setSprite(GConst.ATLAS_PATH.UI_SETTING, "language_" .. language)
    local acountId = DataManager.PlayerData:getAcountId() or GConst.EMPTY_STRING
    self.uiMap["game_setting_ui.bg.player_bg.player_id"]:setText(I18N:getGlobalText(I18N.GlobalConst.SETTING_DESC_1, acountId))

    self:initLoginBtn()
    self:initCDKeyBtn()
    self:initSupportBtn()
    self:refreshMusic()
    self:refreshVoice()
    self:refreshLinkBtns()
end

function GameSettingUI:initLoginBtn()
    self:initGoogleLoginBtn()
    self:initAppleLoginBtn()
    local deleteBtn = self.uiMap["game_setting_ui.bg.delete_btn"]
    deleteBtn:addClickListener(function()
        ModuleManager.AccountManager:showDeleteUI()
    end)
    self:refreshAccountInfo()
end

function GameSettingUI:initGoogleLoginBtn()
    if Platform:isIosPlatform() then
        self.uiMap["game_setting_ui.bg.google_sign_btn"]:setVisible(false)
        self.uiMap["game_setting_ui.bg.google_switch_btn"]:setVisible(false)
    else
        self.uiMap["game_setting_ui.bg.google_sign_btn"]:setVisible(true)
        self.uiMap["game_setting_ui.bg.google_sign_btn"]:addClickListener(function()
            if ModuleManager.AccountManager:getIsBinded() then
                GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACCOUNT_ALREADY_BINDED_DESC))
                return
            end
            ModuleManager.AccountManager:bindAccount()
        end)

        self.uiMap["game_setting_ui.bg.google_switch_btn"]:setVisible(true)
        self.uiMap["game_setting_ui.bg.google_switch_btn"]:addClickListener(function()
            if ModuleManager.AccountManager:getIsBinded() then
                ModuleManager.AccountManager:changeAccount()
            else
                local params = {
                    content = I18N:getGlobalText(I18N.GlobalConst.CHANGE_ACCOUNT_TIPS_DESC),
                    boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
                    okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
                    okFunc = function()
                        ModuleManager.AccountManager:changeAccount()
                    end
                }
                GFunc.showMessageBox(params)
            end
        end)
        self.uiMap["game_setting_ui.bg.google_switch_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.CHANGE_ACCOUNT_DESC))
    end
end

function GameSettingUI:initAppleLoginBtn()
    if Platform:isIosPlatform() then
        self.uiMap["game_setting_ui.bg.apple_sign_btn"]:setVisible(true)
        self.uiMap["game_setting_ui.bg.apple_sign_btn"]:addClickListener(function()
            if ModuleManager.AccountManager:getIsBinded() then
                GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACCOUNT_ALREADY_BINDED_DESC))
                return
            end
            ModuleManager.AccountManager:bindAccount()
        end)
        self.uiMap["game_setting_ui.bg.apple_switch_btn"]:setVisible(true)
        self.uiMap["game_setting_ui.bg.apple_switch_btn"]:addClickListener(function()
            if ModuleManager.AccountManager:getIsBinded() then
                ModuleManager.AccountManager:changeAccount()
            else
                local params = {
                    content = I18N:getGlobalText(I18N.GlobalConst.CHANGE_ACCOUNT_TIPS_DESC),
                    boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
                    okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
                    okFunc = function()
                        ModuleManager.AccountManager:changeAccount()
                    end
                }
                GFunc.showMessageBox(params)
            end
        end)
        self.uiMap["game_setting_ui.bg.apple_switch_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.CHANGE_ACCOUNT_DESC))
    else
        self.uiMap["game_setting_ui.bg.apple_sign_btn"]:setVisible(false)
        self.uiMap["game_setting_ui.bg.apple_switch_btn"]:setVisible(false)
    end
end

function GameSettingUI:initCDKeyBtn()
    if GFunc.isShenhe() then
        self.uiMap["game_setting_ui.bg.cdkey_tx"]:setActive(false)
        self.uiMap["game_setting_ui.bg.cdkey_icon"]:setActive(false)
        self.uiMap["game_setting_ui.bg.cdkey_btn"]:setActive(false)
        return
    else
        self.uiMap["game_setting_ui.bg.cdkey_tx"]:setActive(true)
        self.uiMap["game_setting_ui.bg.cdkey_icon"]:setActive(true)
        self.uiMap["game_setting_ui.bg.cdkey_btn"]:setActive(true)
    end
    self.uiMap["game_setting_ui.bg.cdkey_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CDKEY_NAME))
    self.uiMap["game_setting_ui.bg.cdkey_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.CDKEY_EXCHANGE))
    self.uiMap["game_setting_ui.bg.cdkey_btn"]:addClickListener(function()
        ModuleManager.GameSettingManager:showCDKeyUI()
    end)
end

function GameSettingUI:initSupportBtn()
    if GFunc.isShenhe() then
        self.uiMap["game_setting_ui.bg.support_tx"]:setAnchoredPositionY(-370)
        self.uiMap["game_setting_ui.bg.support_icon"]:setAnchoredPositionY(-370)
        self.uiMap["game_setting_ui.bg.support_btn"]:setAnchoredPositionY(-370)
    else
        self.uiMap["game_setting_ui.bg.support_tx"]:setAnchoredPositionY(-450)
        self.uiMap["game_setting_ui.bg.support_icon"]:setAnchoredPositionY(-450)
        self.uiMap["game_setting_ui.bg.support_btn"]:setAnchoredPositionY(-450)
    end

    self.uiMap["game_setting_ui.bg.support_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUPPORT_DESC_1))
    self.uiMap["game_setting_ui.bg.support_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUPPORT_DESC_2))
    self.uiMap["game_setting_ui.bg.support_btn"]:addClickListener(function()
        ModuleManager.GameSettingManager:showSupport()
        DataManager.AIHelperData:clearRp()
    end)
end

function GameSettingUI:_addListeners()
    self.uiMap["game_setting_ui.bg.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    self.uiMap["game_setting_ui.bg.music_bg"]:addClickListener(function()
        local status = AudioManager:isMusicEnabled()
        if not status then
            AudioManager:setMusicVolume(1)
        else
            AudioManager:setMusicVolume(0)
        end

        if GFunc.isInWhitelist() then
            self.clickMusicCount = self.clickMusicCount + 1
            if self.clickMusicCount > CHECK_WHITE_LIST_COUNT then
                GFunc.showToast("Please don't frequently operate")
            end
        end

        self:refreshMusic()
    end)

    self.uiMap["game_setting_ui.bg.voice_bg"]:addClickListener(function()
        local status = AudioManager:isEffectEnabled()
        if not status then
            AudioManager:setEffectVolume(1)
        else
            AudioManager:setEffectVolume(0)
        end
        self:refreshVoice()
    end)

    self.uiMap["game_setting_ui.bg.language_btn"]:addClickListener(function()
        ModuleManager.GameSettingManager:showLanguageUI()
    end)

    self.uiMap["game_setting_ui.bg.player_bg.player_id_btn"]:addClickListener(function()
        local acountId = DataManager.PlayerData:getAcountId() or GConst.EMPTY_STRING
        GFunc.copyStr(acountId)
    end)

    self.uiMap["game_setting_ui.bg.tx_1"]:addClickListener(function()
        GFunc.openUrl("https://perfeggsgame.com/tos.html")
    end)

    self.uiMap["game_setting_ui.bg.tx_2"]:addClickListener(function()
        GFunc.openUrl("https://perfeggsgame.com/pp.html")
    end)
end

function GameSettingUI:_bind()
    self:bind(DataManager.AIHelperData, "isDirty", function()
        local supportBtn = self.uiMap["game_setting_ui.bg.support_btn"]
        if DataManager.AIHelperData:getRp() then
            supportBtn:addRedPoint(88, 24, 0.8)
        else
            supportBtn:removeRedPoint()
        end
    end, true)
end

function GameSettingUI:refreshMusic()
    local offIcon = self.uiMap["game_setting_ui.bg.music_bg.off"]
    local onIcon = self.uiMap["game_setting_ui.bg.music_bg.on"]

    local status = AudioManager:isMusicEnabled()
    offIcon:setVisible(status ~= true)
    onIcon:setVisible(status == true)
end

function GameSettingUI:refreshVoice()
    local offIcon = self.uiMap["game_setting_ui.bg.voice_bg.off"]
    local onIcon = self.uiMap["game_setting_ui.bg.voice_bg.on"]

    local status = AudioManager:isEffectEnabled()
    offIcon:setVisible(status ~= true)
    onIcon:setVisible(status == true)
end

function GameSettingUI:refreshAccountInfo()
    if Platform:isIosPlatform() then
        if ModuleManager.AccountManager:getIsBinded() then
            self.uiMap["game_setting_ui.bg.apple_sign_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACCOUNT_ALREADY_BINDED_DESC))
        else
            self.uiMap["game_setting_ui.bg.apple_sign_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.BIND_ACCOUNT_DESC))
        end
    else
        if ModuleManager.AccountManager:getIsBinded() then
            self.uiMap["game_setting_ui.bg.google_sign_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACCOUNT_ALREADY_BINDED_DESC))
        else
            self.uiMap["game_setting_ui.bg.google_sign_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.BIND_ACCOUNT_DESC))
        end
    end
end

function GameSettingUI:refreshLinkBtns()
    local uiMap = self.root:genAllChildren()
    local bg = uiMap["game_setting_ui.bg"]
    local node = uiMap["game_setting_ui.bg.community_node"]
    local curLanguage = I18N:getCurLanguage()
    if not GameSettingConst.COMMUNITY_URL[curLanguage] then
        curLanguage = GConst.LANGUAGE.ENGLISH
    end
    if not GameSettingConst.COMMUNITY_URL[curLanguage] then
        bg:setSizeDeltaY(BG_DEFAULT_H)
        node:setVisible(false)
        return
    end
    bg:setSizeDeltaY(BG_DEFAULT_H + 100)
    node:setVisible(true)
    local communityDesc = uiMap["game_setting_ui.bg.community_node.discord_desc"]
    local communityBtn = uiMap["game_setting_ui.bg.community_node.discord"]
    local socialDesc = uiMap["game_setting_ui.bg.community_node.facebook_desc"]
    local socialBtn = uiMap["game_setting_ui.bg.community_node.facebook"]

    communityDesc:setText(I18N:getGlobalText(GameSettingConst.COMMUNITY_TXT[curLanguage]))
    communityBtn:addClickListener(function()
        local url = GameSettingConst.COMMUNITY_URL[I18N:getCurLanguage()]
        if not url then
            url = GameSettingConst.COMMUNITY_URL[GConst.LANGUAGE.ENGLISH]
        end
        GFunc.openUrl(url)
    end)
    communityBtn:setVisible(false)
    communityBtn:setSprite(GConst.ATLAS_PATH.UI_SETTING, GameSettingConst.COMMUNITY_ICON[curLanguage], function()
        communityBtn:setVisible(true)
    end)

    socialDesc:setText(I18N:getGlobalText(GameSettingConst.SOCIAL_DESC[curLanguage]))
    socialBtn:addClickListener(function()
        local url = GameSettingConst.SOCIAL_URL[I18N:getCurLanguage()]
        if not url then
            url = GameSettingConst.SOCIAL_URL[GConst.LANGUAGE.ENGLISH]
        end
        GFunc.openUrl(url)
    end)
    socialBtn:setVisible(false)
    socialBtn:setSprite(GConst.ATLAS_PATH.UI_SETTING, GameSettingConst.SOCIAL_ICON[curLanguage], function()
        socialBtn:setVisible(true)
    end)
end

return GameSettingUI