local ActPvpPlayingUI = class("ActPvpPlayingUI", BaseUI)

local SIMPLE_HERO_CELL = "app/ui/activity/act_pvp/cell/simple_hero_cell"
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"
}

function ActPvpPlayingUI:ctor()

end

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

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

function ActPvpPlayingUI:onLoadRootComplete()
    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 ActPvpPlayingUI:onPressBackspace()
    self:closeUI()
end

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

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

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

function ActPvpPlayingUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["act_pvp_playing_ui.bg.ui_spine_obj"]:playAnim("idle", true, false, false)
    uiMap["act_pvp_main_ui.down.playing_node.macth_btn.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_2))
    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))
    local title = uiMap["act_pvp_main_ui.bg.title_tx"]
    title:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_6))
    title:setSizeDeltaX(title:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth)

    self:refreshPlayingNode()
    self:refreshDialyRewardNode()
    self:refreshBtns()
end

function ActPvpPlayingUI:_addListeners()
    local uiMap = self.root:genAllChildren()

    uiMap["act_pvp_main_ui.down.playing_node.macth_btn"]:addClickListener(function()
        if DataManager.ActPvpData:isFinalDay() then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_24))
            return
        end
        ModuleManager.ActPvpManager:showMatchUI()
    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)
end

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

function ActPvpPlayingUI: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.playing_node"]:setVisible(false)
        end
    end
end

function ActPvpPlayingUI:refreshPlayingNode()
    local curPairIds = DataManager.ActPvpData:getCurSelectPairId()
    if curPairIds and curPairIds[1] then
        ModuleManager.ActPvpManager:showSelectUI() -- 还有可选择的英雄
    end

    local uiMap = self.root:genAllChildren()
    if not self.simpleHeroCells then
        self.simpleHeroCells = {}
        for i = 1, 5 do
            self.simpleHeroCells[i] = CellManager:addCellComp(uiMap["act_pvp_main_ui.down.playing_node.hero_formation_comp.simple_hero_cell_" .. i], SIMPLE_HERO_CELL)
        end
    end
    if not self.skillObjs then
        self.skillObjs = {}
        for i = 1, 5 do
            self.skillObjs[i] = {}
            self.skillObjs[i].bg = uiMap["act_pvp_playing_ui.bg.playing_node.skill_up_" .. i]
            self.skillObjs[i].icon = uiMap["act_pvp_playing_ui.bg.playing_node.skill_up_" .. i .. ".icon"]
            self.skillObjs[i].spine = uiMap["act_pvp_playing_ui.bg.playing_node.skill_up_node.ui_spine_obj_" .. i]
        end
    end

    local heroes = DataManager.ActPvpData:getHeros()
    local skillUpNode = uiMap["act_pvp_playing_ui.bg.playing_node.skill_up_node"]
    skillUpNode:setVisible(false)
    if DataManager.ActPvpData:getHeroLevelUp() then
        DataManager.ActPvpData:setHeroLevelUp(false)
        local heroLv = DataManager.ActPvpData:getHeroLv()
        local info = ConfigManager:getConfig("hero_level")[heroLv]
        local infoLast = ConfigManager:getConfig("hero_level")[heroLv - 1]
        if info and infoLast and info.unlock_skill > infoLast.unlock_skill then
            skillUpNode:setVisible(true)
            for matchType, objs in ipairs(self.skillObjs) do
                local entity = heroes[matchType]
                local skillId = entity:getRogueSkillList()[info.unlock_skill]
                if entity and skillId then
                    objs.bg:addClickListener(function()
                        local cfg = ConfigManager:getConfig("skill_rogue")[skillId]
                        ModuleManager.TipsManager:showSkillTips(objs.icon, cfg.buff_id, skillId)
                    end)
                    objs.icon:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueIcon(skillId))
                    objs.bg:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueBg(skillId, true))
                    objs.spine:playAnim("idle", false, false, false)
                    objs.bg:setVisible(true)
                else
                    objs.bg:setVisible(false)
                end
            end
            if self.showLevelUpSid then
                self:unscheduleGlobal(self.showLevelUpSid)
                self.showLevelUpSid = nil
            end
            self.showLevelUpSid = self:performWithDelayGlobal(function()
                skillUpNode:setVisible(false)
            end, 5)
        else -- 只升级，没有升级技能
            for matchType, objs in ipairs(self.skillObjs) do
                objs.bg:setVisible(false)
                objs.spine:playAnim("idle", false, false, false)
            end
            skillUpNode:setVisible(true)
            if self.showLevelUpSid then
                self:unscheduleGlobal(self.showLevelUpSid)
                self.showLevelUpSid = nil
            end
            self.showLevelUpSid = self:performWithDelayGlobal(function()
                skillUpNode:setVisible(false)
            end, 2)
        end
    end

    local actData = DataManager.ActPvpData
    local txObj = uiMap["act_pvp_main_ui.down.playing_node.tx_fight"]
    local txLose = uiMap["act_pvp_playing_ui.bg.playing_node.tx_fight_lose"]
    txObj:setText(actData:getWinCount() .. "/" .. actData:getWinLimit())
    txLose:setText(actData:getLoseCount() .. "/" .. actData:getLoseLimit())

    for matchType, cell in ipairs(self.simpleHeroCells) do
        local entity = heroes[matchType]
        if entity then
            cell:getBaseObject():setVisible(true)
            cell:refresh(entity)
        else
            cell:getBaseObject():setVisible(false)
        end
    end
end

function ActPvpPlayingUI: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 ActPvpPlayingUI: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
end

function ActPvpPlayingUI:showTalk()
    local uiMap = self.root:genAllChildren()
    local popBg = uiMap["act_pvp_playing_ui.bg.pop_bg"]
    local popDesc = uiMap["act_pvp_main_ui.bg.pop_desc"]
    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, #TASK_DESC - 1)
        local desc = table.remove(TASK_DESC, index)
        table.insert(TASK_DESC, 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 ActPvpPlayingUI