local DungeonArmorFightUI = class("DungeonArmorFightUI", BaseUI)

local TASK_VIEW_CELL = "app/ui/dungeon_armor/cell/task_view_cell"

function DungeonArmorFightUI:ctor(params)
    self.armorData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR)
    self.chapterId = params.chapterId
    self.config = self.armorData:getConfig(self.chapterId)
end

function DungeonArmorFightUI:isFullScreen()
    return false
end

function DungeonArmorFightUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_armor/dungeon_armor_fight_ui.prefab"
end

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

function DungeonArmorFightUI:_display()
    self:refreshTop()
    self:refreshMiddle()
    self:refreshBottom()
end

function DungeonArmorFightUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_armor_fight_ui.bg.bg.btn"]:addClickListener(function()
        ModuleManager.DungeonArmorManager:reqFight(self.chapterId)
    end)

    uiMap["dungeon_armor_fight_ui.bg.btn_close"]:addClickListener(function()
        self:closeUI()
    end)
end

function DungeonArmorFightUI:refreshTop()
    local uiMap = self.root:genAllChildren()

    uiMap["dungeon_armor_fight_ui.bg.title.tx_title"]:setText(self.config.chapter .. "-" .. self.config.stage)
    uiMap["dungeon_armor_fight_ui.bg.bg.desc_3"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_6))

    if not self.taskViewCells then
        self.taskViewCells = {}
        for i = 1, 3 do
            self.taskViewCells[i] = CellManager:addCellComp(uiMap["dungeon_armor_fight_ui.bg.bg.banner.task_view_cell_" .. i], TASK_VIEW_CELL)
        end
    end

    for index, cell in ipairs(self.taskViewCells) do
        cell:refresh(self.armorData:getStarStarId(self.chapterId, index), self.armorData:getStarDone(self.chapterId, index), self.armorData:getStarReward(self.chapterId, index))
    end
end

function DungeonArmorFightUI:refreshMiddle()
    local uiMap = self.root:genAllChildren()

    uiMap["dungeon_armor_fight_ui.bg.bg.desc_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_5))

    local rewardNode = uiMap["dungeon_armor_fight_ui.bg.bg.reward_node"]
    if not self.rewardCells then
        self.rewardCells = {}
        for i = 1, 4 do
            self.rewardCells[i] = CellManager:addCellComp(uiMap["dungeon_armor_fight_ui.bg.bg.reward_node.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
        end
    end

    for index, cell in ipairs(self.rewardCells) do
        local reward = self.config.item_show[index]
        cell:getBaseObject():setActive(reward ~= nil)
        if reward ~= nil then
            cell:refreshByConfig(reward)
            cell:hideCountTx()
        end
    end

    rewardNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
end

function DungeonArmorFightUI:refreshBottom()
    local uiMap = self.root:genAllChildren()

    local costVit = uiMap["dungeon_armor_fight_ui.bg.bg.btn.num"]
    costVit:setText(GFunc.getRewardNum(self.armorData:getChallengeHpCost()))

    local vitIcon = uiMap["dungeon_armor_fight_ui.bg.bg.btn.icon"]
    GFunc.centerImgAndTx(vitIcon, costVit, 10)

    uiMap["dungeon_armor_fight_ui.bg.bg.btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_CHALLENGE))
    local str = GConst.EMPTY_STRING
    if self.armorData:getStarNum(self.chapterId) > 0 then
        str = I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_17, self.armorData:getRemianFarmCount(self.chapterId))
    end
    uiMap["dungeon_armor_fight_ui.bg.bg.desc_2"]:setText(str)
end

return DungeonArmorFightUI