local ActPvpMainUI = class("ActPvpMainUI", BaseUI)

local ENTER_BTN_BG = {"common_btn_green_1", "common_btn_grey_1"}

local TASK_DESC =
{
    "ACT_PVP_TALK_DESC_1",
    "ACT_PVP_TALK_DESC_2",
    "ACT_PVP_TALK_DESC_3",
    "ACT_PVP_TALK_DESC_4",
    "ACT_PVP_TALK_DESC_5"
}

local WIN_TASK_DESC =
{
    "ACT_PVP_TALK_DESC_6",
    "ACT_PVP_TALK_DESC_7",
}

local FAIL_TASK_DESC =
{
    "ACT_PVP_TALK_DESC_8",
    "ACT_PVP_TALK_DESC_9",
}

function ActPvpMainUI:ctor()

end

function ActPvpMainUI:getBGMId()
    return AudioManager.BGM_ID.ACT_PVP_MAINBGM
end

function ActPvpMainUI:getCurrencyParams()
    if self.currencyParams == nil then
        self.currencyParams = {
            itemIds = {
                GConst.ItemConst.ITEM_ID_GEM,
            },
            showType = GConst.CURRENCY_TYPE.HORIZONTAL
        }
    end
 	return self.currencyParams
end

function ActPvpMainUI:getPrefabPath()
    return "assets/prefabs/ui/activity/act_pvp/act_pvp_main_ui.prefab"
end

function ActPvpMainUI:onLoadRootComplete()
    if not DataManager.ActPvpData:getActPvpShowHelpTag() then
        DataManager.ActPvpData:recordActPvpShowHelpTag()
        ModuleManager.ActPvpManager:showHelpUI()
    end
    self:_display()
    self:_addListeners()
    self:_bind()

    self:showTalk()
    self:refreshTime()
    if self.actCountdownSid then
        self:unscheduleGlobal(self.actCountdownSid)
    end
    self.actCountdownSid = self:scheduleGlobal(function()
        self:refreshTime()
	end, 1)
end

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

function ActPvpMainUI:onClose()
    if self.showTalkSeq then
        self.showTalkSeq:Kill()
        self.showTalkSeq = nil
    end

    if self.countdownSid then
        self:unscheduleGlobal(self.countdownSid)
        self.countdownSid = nil
    end
end

function ActPvpMainUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["act_pvp_main_ui.bg.ui_spine_obj"]:playAnim("idle", true, false, false)
    uiMap["act_pvp_main_ui.down.btn_rank.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_10))
    uiMap["act_pvp_main_ui.down.btn_bounty.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_4))

    if DataManager.ActPvpData:isFinalDay() then
        uiMap["act_pvp_main_ui.bg.desc"]:setText(GConst.EMPTY_STRING)
        uiMap["act_pvp_main_ui.down.enter_node.enter_btn"]:setSprite(GConst.ATLAS_PATH.COMMON, ENTER_BTN_BG[2])
        uiMap["act_pvp_main_ui.down.enter_node.enter_btn.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_24))
    else
        uiMap["act_pvp_main_ui.bg.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_5))
        uiMap["act_pvp_main_ui.down.enter_node.enter_btn"]:setSprite(GConst.ATLAS_PATH.COMMON, ENTER_BTN_BG[1])
        uiMap["act_pvp_main_ui.down.enter_node.enter_btn.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_2))
    end
    local titleBg = uiMap["act_pvp_main_ui.bg.title_img"]
    local title = uiMap["act_pvp_main_ui.bg.title_tx"]
    title:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_6))
    GFunc.centerImgAndTx(titleBg, title, 5)

    self:refreshEnterNode()
    self:refreshDialyRewardNode()
    self:refreshBtns()
end

function ActPvpMainUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["act_pvp_main_ui.down.enter_node.enter_btn"]:addClickListener(function()
        ModuleManager.ActPvpManager:reqEnter()
    end)

    uiMap["act_pvp_main_ui.bg.time_bg.btn_help"]:addClickListener(function()
        ModuleManager.ActPvpManager:showHelpUI()
    end)

    uiMap["act_pvp_main_ui.down.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["act_pvp_main_ui.down.btn_rank"]:addClickListener(function()
        ModuleManager.ActPvpManager:showRankUI()
    end)

    uiMap["act_pvp_main_ui.down.btn_bounty"]:addClickListener(function()
        ModuleManager.ActPvpManager:showBountyUI()
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.ACT_PVP_EVENT, function(params)
        if params and params.closeMainUI then
            self:closeUI()
        end
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.GO_SHOP, function()
        self:closeUI()
    end)
end

function ActPvpMainUI:_bind()
    self:bind(DataManager.ActPvpData, "isDirty", function()
        self:_display()
    end)
end

function ActPvpMainUI:refreshTime()
    local uiMap = self.root:genAllChildren()
    local remainTime = DataManager.ActPvpData:getRemainTime()
    local str
    if remainTime <= 0 then
        str = I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_EDSC)
    else
        str = Time:formatNumTimeStr(remainTime)
    end
    local icon = uiMap["act_pvp_main_ui.bg.time_bg.time_icon"]
    local tx = uiMap["act_pvp_main_ui.bg.time_bg.time"]
    tx:setText(str)
    GFunc.centerImgAndTx(icon, tx, 5)

    local canFight = remainTime > 86400
    if self.canFight ~= canFight then
        self.canFight = canFight
        if not self.canFight then
            uiMap["act_pvp_main_ui.down.enter_node.enter_btn"]:setSprite(GConst.ATLAS_PATH.COMMON, ENTER_BTN_BG[2])
            uiMap["act_pvp_main_ui.down.enter_node.enter_btn.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_24))
        end
    end
end

function ActPvpMainUI:refreshEnterNode()
    local uiMap = self.root:genAllChildren()
    local freeCount = DataManager.ActPvpData:getFreeEnterCount()
    local txFree = uiMap["act_pvp_main_ui.down.enter_node.tx_count"]
    local costNode = uiMap["act_pvp_main_ui.down.enter_node.cost"]
    local costDesc = uiMap["act_pvp_main_ui.down.enter_node.cost.tx_cost_desc"]
    local costNum = uiMap["act_pvp_main_ui.down.enter_node.cost.tx_cost"]
    local costIcon = uiMap["act_pvp_main_ui.down.enter_node.cost.icon"]
    local btnHelp = uiMap["act_pvp_main_ui.bg.time_bg.btn_help"]
    if freeCount > 0 then
        if DataManager.ActPvpData:isFinalDay() then
            freeCount = 0
        end
        txFree:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_6, freeCount))
        costNode:setVisible(false)
        btnHelp:setAnchoredPositionX(txFree:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth / 2 + 30)
    else
        txFree:setText(GConst.EMPTY_STRING)
        costNode:setVisible(true)
        costDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_7))
        costNum:setText(DataManager.ActPvpData:getEnterCost().num)
        GFunc.centerTxAndImgAndTx(costDesc, 5, costIcon, 5, costNum)
        local x = costNum:fastGetAnchoredPositionX()
        btnHelp:setAnchoredPositionX(x + costNum:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth / 2 + 30)
    end
end

function ActPvpMainUI:refreshBtns()
    local uiMap = self.root:genAllChildren()
    local btnRank = uiMap["act_pvp_main_ui.down.btn_rank"]
    if DataManager.ActPvpData:canGetRankReward() then
        btnRank:addRedPoint(30, 36, 0.6)
    else
        btnRank:removeRedPoint()
    end

    local btnBounty = uiMap["act_pvp_main_ui.down.btn_bounty"]
    if DataManager.ActPvpData:getBountyRp() then
        btnBounty:addRedPoint(30, 36, 0.6)
    else
        btnBounty:removeRedPoint()
    end
end

function ActPvpMainUI:refreshDialyRewardNode()
    local uiMap = self.root:genAllChildren()
    local boxIndex = 0
    for i = 1, 10 do
        local obj = uiMap["act_pvp_main_ui.bg.win_point_" .. i]
        local color = "#ffffff"
        local countEnough = false
        if DataManager.ActPvpData:winCountEnough(i) then
            color = "#FFF667"
            countEnough = true
        end

        if DataManager.ActPvpData:getWinCountMap()[i] then
            boxIndex = boxIndex + 1
            local boxIcon = "act_pvp_chest_" .. boxIndex
            local tx = uiMap["act_pvp_main_ui.bg.win_desc_" .. i]
            if tx then
                local str = I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_7, i)
                str = string.format("<color=%s>%s</color>", color, str)
                tx:setText(str)
            end

            if DataManager.ActPvpData:canGetDailyReward(i) then
                GFunc.getShakeSeq(obj, false, 1)
                obj:addClickListener(function()
                    ModuleManager.ActPvpManager:reqDailyReward(DataManager.ActPvpData:getWinCountMap()[i])
                end)
            else
                GFunc.getShakeSeq(obj, true, 1)
                obj:addClickListener(function()
                    ModuleManager.TipsManager:showRewardsTips(DataManager.ActPvpData:getDailyRewards(i), nil, obj)
                end)
            end

            if DataManager.ActPvpData:gotDailyReward(i) then
                boxIcon = "act_pvp_chest_" .. boxIndex .. "_1"
            end
            obj:setSprite(GConst.ATLAS_PATH.UI_ACT_PVP, boxIcon)
        else
            local icon = "act_pvp_point_2"
            if countEnough then
                icon = "act_pvp_point_1"
            end
            obj:setSprite(GConst.ATLAS_PATH.UI_ACT_PVP, icon)
        end
    end
    uiMap["act_pvp_main_ui.bg.mask_img"]:setVisible(DataManager.ActPvpData:isFinalDay())
end

function ActPvpMainUI:showTalk()
    local uiMap = self.root:genAllChildren()
    local popBg = uiMap["act_pvp_main_ui.bg.pop_bg"]
    local popDesc = uiMap["act_pvp_main_ui.bg.pop_desc"]
    self.curTaskDescList = TASK_DESC
    if DataManager.ActPvpData:getLastTurnWin() ~= nil then
        if DataManager.ActPvpData:getLastTurnWin() then
            self.curTaskDescList = WIN_TASK_DESC
        else
            self.curTaskDescList = FAIL_TASK_DESC
        end
        DataManager.ActPvpData:setLastTurnWin(nil)
    end
    if self.showTalkSeq then
        self.showTalkSeq:Kill()
        self.showTalkSeq = nil
    end
    self.showTalkSeq = self.root:createBindTweenSequence()
    self.showTalkSeq:AppendCallback(function()
        local index = math.random(1, #self.curTaskDescList - 1)
        local desc = table.remove(self.curTaskDescList, index)
        table.insert(self.curTaskDescList, desc)
        popDesc:setText(I18N:getGlobalText(I18N.GlobalConst[desc]))
        popBg:setVisible(false)
    end)
    self.showTalkSeq:Append(popBg:getTransform():DOScale(1, 1))
    self.showTalkSeq:AppendInterval(5)
    self.showTalkSeq:Append(popBg:getTransform():DOScale(0, 1))
    self.showTalkSeq:SetLoops(-1)
end

return ActPvpMainUI