local DungeonArmorFarmUI = class("DungeonArmorFarmUI", BaseUI)

function DungeonArmorFarmUI: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 DungeonArmorFarmUI:isFullScreen()
    return false
end

function DungeonArmorFarmUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_armor/dungeon_armor_farm_ui.prefab"
end

function DungeonArmorFarmUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
    self:_bind()
end

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

function DungeonArmorFarmUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_armor_farm_ui.bg.bg.btn"]:addClickListener(function()
        ModuleManager.DungeonArmorManager:reqSweep(self.chapterId)
    end)

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

function DungeonArmorFarmUI:_bind()
    self:bind(self.armorData, "isDirty", function()
        self:refreshBottom()
    end)
end

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

    uiMap["dungeon_armor_farm_ui.bg.title.tx_title"]:setText(self.config.chapter .. "-" .. self.config.stage)
end

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

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

    local rewardNode = uiMap["dungeon_armor_farm_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_farm_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 DungeonArmorFarmUI:refreshBottom()
    local uiMap = self.root:genAllChildren()

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

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

    uiMap["dungeon_armor_farm_ui.bg.bg.btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
    uiMap["dungeon_armor_farm_ui.bg.bg.desc_2"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_17, self.armorData:getRemianFarmCount(self.chapterId)))
end

return DungeonArmorFarmUI