local BattlePauseUI = class("BattlePauseUI", BaseUI)

local SELECT_SKILL_CELL = "app/ui/battle/cell/select_skill_cell"
local SKILL_ROGUE_CFG = ConfigManager:getConfig("skill_rogue")
local HIDE_TYPE = {
    [10] = true
}
local HIDE_ID = {
    [29] = true
}

-- 响应安卓后退事件
function BattlePauseUI:onPressBackspace()
    self:closeUI()
end

function BattlePauseUI:isFullScreen()
    return false
end

function BattlePauseUI:showCommonBG()
    return false
end

function BattlePauseUI:getPrefabPath()
    return "assets/prefabs/ui/battle/battle_pause_ui.prefab"
end

function BattlePauseUI:ctor(params)
    self.battleType = params.battleType
    self.battleController = ModuleManager.BattleManager.battleController
    local map = self.battleController.battleData:getSelectSkillMap()
    self.skillList = {}
    for skillId, info in pairs(map) do
        local cfg = SKILL_ROGUE_CFG[skillId]
        if cfg and not HIDE_TYPE[cfg.type] and not HIDE_ID[skillId] then
            table.insert(self.skillList, {skillId = skillId, info = info})
        end
    end
end

function BattlePauseUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
end

function BattlePauseUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["battle_pause_ui.skill_node.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_2))
    self:_refreshScrollRect()
    self:refreshDailyChallengeNode()
end

function BattlePauseUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["battle_pause_ui.home_btn"]:addClickListener(function()
        if self.battleType == GConst.BattleConst.BATTLE_TYPE.ACT_BOSS_RUSH then
            local params = {
                content = I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_34),
                boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
                okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
                cancelText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL),
                okFunc = function()
                    self.battleController:battleEnd()
                end,
            }
            GFunc.showMessageBox(params)
        else
            local params = {
                content = I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_1),
                boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
                okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
                cancelText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL),
                okFunc = function()
                    ModuleManager.BattleManager:endBattleAndExit(true)
                end,
            }
            GFunc.showMessageBox(params)
        end
    end)

    uiMap["battle_pause_ui.continue_btn"]:addClickListener(function()
        self:closeUI()
    end)
end

function BattlePauseUI:_refreshScrollRect()
    if self.scrollRect then
        self.scrollRect:updateAllCell()
        return
    end

    local uiMap = self.root:genAllChildren()
    self.scrollRect = uiMap["battle_pause_ui.skill_node.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
		return SELECT_SKILL_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
        local skillId = self.skillList[index].skillId
        local count = self.skillList[index].info.count
        local value = self.skillList[index].info.value
        local cfg = ConfigManager:getConfig("skill_rogue")[skillId]
        local str
        if cfg and cfg.attr and ModuleManager.HeroManager:showValueRogue(skillId) then
            str = GFunc.getPerStr(cfg.attr.type, value)
        end

		cell:refresh(skillId, count, str)
        cell:addClickListener(function()
            local valueStr
            if skillId == 24 then -- 特殊处理
                if ModuleManager.BattleManager.battleController then
                    valueStr = ModuleManager.BattleManager.battleController.battleData:getSkillCount(skillId)
                    valueStr = I18N:getText("skill_rogue", skillId, "desc", valueStr)
                end
            else
                valueStr = ModuleManager.HeroManager:getSkillRogueDesc(skillId, value)
            end
            ModuleManager.TipsManager:showDescTips(valueStr, cell:getBaseObject())
        end)
	end)
    self.scrollRect:clearCells()
    self.scrollRect:refillCells(#self.skillList)
end

function BattlePauseUI:refreshDailyChallengeNode()
    local uiMap = self.root:genAllChildren()
    local node = uiMap["battle_pause_ui.daily_challenge_node"]
    if self.battleType ~= GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE then
        node:setVisible(false)
        return
    end
    node:setVisible(true)
    local buffDescs = {}
    for _, id in ipairs(DataManager.DailyChallengeData:getTodayBuffIds()) do
        local cfg = ConfigManager:getConfig("buff_daily_challenge")[id]
        if cfg then
            buffDescs[cfg.buff_type] = ModuleManager.DailyChallengeManager:getBuffDesc(id)
        end
    end
    local buffBtns = {
        uiMap["battle_pause_ui.daily_challenge_node.buff_1"],
        uiMap["battle_pause_ui.daily_challenge_node.buff_2"]
    }
    for i, obj in ipairs(buffBtns) do
        obj:addClickListener(function()
            ModuleManager.TipsManager:showDescTips(buffDescs[i] or GConst.EMPTY_STRING, obj)
        end)
    end
    uiMap["battle_pause_ui.daily_challenge_node.buff_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.BUFF_DESC))
end

return BattlePauseUI