local ChapterLayerCell = class("ChapterLayerCell", BaseCell)

local FIGHT_BTN = {"common_btn_yellow_3", "common_btn_green_3"}

function ChapterLayerCell:refresh(id)
    local config = ConfigManager:getConfig("chapter_dungeon_equip")[id]
    local uiMap = self:getUIMap()
    local mask = uiMap["chapter_layer_cell.bg.mask"]
    local bossBanner = uiMap["chapter_layer_cell.bg.boss_banner"]
    local bossMask = uiMap["chapter_layer_cell.bg.boss_mask"]
    local rewardsNode = uiMap["chapter_layer_cell.bg.rewards_node"]
    local title = uiMap["chapter_layer_cell.bg.title_desc"]
    local unlockDesc = uiMap["chapter_layer_cell.bg.unlock_desc"]
    local fightBtn = uiMap["chapter_layer_cell.bg.fight_btn"]
    local fightDesc = uiMap["chapter_layer_cell.bg.fight_btn.desc"]
    local introdutionBtn = uiMap["chapter_layer_cell.bg.introduction_btn"]
    local introdutionBtnTx = uiMap["chapter_layer_cell.bg.introduction_btn.desc"]
    local iconBg = uiMap["chapter_layer_cell.bg.fight_btn.icon_bg"]
    local iconBgDesc = uiMap["chapter_layer_cell.bg.fight_btn.icon_bg.desc"]
    local iconBgIcon = uiMap["chapter_layer_cell.bg.fight_btn.icon_bg.icon"]
    local iconBgNum = uiMap["chapter_layer_cell.bg.fight_btn.icon_bg.num"]

    if not self.rewardCells then
        self.rewardCells = {}
        for i = 1, 4 do
            self.rewardCells[i] = CellManager:addCellComp(uiMap["chapter_layer_cell.bg.rewards_node.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
        end
    end
    local weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
    fightDesc:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_CHALLENGE))
    unlockDesc:setText(GConst.EMPTY_STRING)
    introdutionBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_7))
    title:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_6, id))
    bossBanner:setVisible(false)
    bossBanner:setTexture("assets/arts/textures/background/dungeon_equip/" .. config.icon .. ".png", function()
        bossBanner:setVisible(true)
    end)
    bossMask:setTexture("assets/arts/textures/background/dungeon_equip/" .. config.icon .. ".png")
    if weaponData:canFarmChapter(id) and weaponData:getRemianFarmCount() > 0 then -- 可扫荡 切有扫荡次数
        iconBg:setVisible(true)
        fightDesc:setVisible(false)
        mask:setVisible(false)
        bossMask:setVisible(false)
        rewardsNode:setActive(true)
        self.rewardCells[4]:getBaseObject():setActive(false)
        introdutionBtn:setActive(false)
        fightBtn:setActive(true)

        fightBtn:setSprite(GConst.ATLAS_PATH.COMMON, FIGHT_BTN[2])
        iconBgDesc:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
        iconBgNum:setText(GFunc.getRewardNum(weaponData:getChallengeHpCost()))
        GFunc.centerImgAndTx(iconBgIcon, iconBgNum, 10)
        for i = 1, 3 do
            if config.item_show[i] then
                self.rewardCells[i]:refreshByConfig(config.item_show[i])
                self.rewardCells[i]:hideCountTx()
                self.rewardCells[i]:getBaseObject():setActive(true)
            else
                self.rewardCells[i]:getBaseObject():setActive(false)
            end
        end
        fightBtn:addClickListener(function()
            ModuleManager.DungeonWeaponManager:reqSweep(id)
        end)
        rewardsNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
    elseif weaponData:canFightChapter(id) then -- 可挑战
        if weaponData:canFarmChapter(id) then -- 没扫荡次数
            iconBg:setVisible(true)
            fightDesc:setVisible(false)
            mask:setVisible(false)
            bossMask:setVisible(false)
            rewardsNode:setActive(true)
            self.rewardCells[4]:getBaseObject():setActive(false)
            introdutionBtn:setActive(false)
            fightBtn:setActive(true)
    
            fightBtn:setSprite(GConst.ATLAS_PATH.COMMON, FIGHT_BTN[1])
            iconBgDesc:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_CHALLENGE))
            iconBgNum:setText(GFunc.getRewardNum(weaponData:getChallengeHpCost()))
            GFunc.centerImgAndTx(iconBgIcon, iconBgNum, 10)
            for i = 1, 3 do
                if config.item_show[i] then
                    self.rewardCells[i]:refreshByConfig(config.item_show[i])
                    self.rewardCells[i]:hideCountTx()
                    self.rewardCells[i]:getBaseObject():setActive(true)
                else
                    self.rewardCells[i]:getBaseObject():setActive(false)
                end
            end
            fightBtn:addClickListener(function()
                ModuleManager.DungeonWeaponManager:reqFight(id)
            end)
            rewardsNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
        else
            iconBg:setVisible(false)
            fightDesc:setVisible(true)
            mask:setVisible(false)
            bossMask:setVisible(false)
            rewardsNode:setActive(true)
            self.rewardCells[4]:getBaseObject():setActive(true)
            introdutionBtn:setActive(true)
            fightBtn:setActive(true)
            fightBtn:setSprite(GConst.ATLAS_PATH.COMMON, FIGHT_BTN[1])
            for i = 1, 3 do
                if config.item_show[i] then
                    self.rewardCells[i]:refreshByConfig(config.item_show[i])
                    self.rewardCells[i]:hideCountTx()
                    self.rewardCells[i]:getBaseObject():setActive(true)
                else
                    self.rewardCells[i]:getBaseObject():setActive(false)
                end
            end
            self.rewardCells[4]:refreshByConfig(config.first_reward)
            self.rewardCells[4]:showFirstPass(true)
            fightBtn:addClickListener(function()
                ModuleManager.DungeonWeaponManager:reqFight(id)
            end)
            rewardsNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
        end
    else -- 不可挑战
        mask:setVisible(true)
        bossMask:setVisible(true)
        rewardsNode:setActive(false)
        introdutionBtn:setActive(false)
        fightBtn:setActive(false)

        if weaponData:getLevelEnough(id) then
            unlockDesc:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_11))
        else
            unlockDesc:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_10, config.level))
        end
    end

    introdutionBtn:addClickListener(function()
        ModuleManager.DungeonWeaponManager:showIntroductionUI(id)
    end)
end

return ChapterLayerCell