local ChapterCell = class("ChapterCell", BaseCell)

function ChapterCell:refresh(id)
    local uiMap = self:getUIMap()
    local index = self:getIndex()
    local touchNode = uiMap["chapter_cell.touch_node"]

    if index % 2 ~= 0 then -- 奇数
        touchNode:setAnchoredPositionX(-125)
    else
        touchNode:setAnchoredPositionX(125)
    end

    local config = ConfigManager:getConfig("chapter_dungeon_armor")[id]
    if not config then
        return
    end

    local farmNode = uiMap["chapter_cell.touch_node.farm_node"]
    local farmTx = uiMap["chapter_cell.touch_node.farm_node.desc"]
    farmTx:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
    local chapterNode = uiMap["chapter_cell.touch_node.chapter_node"]
    uiMap["chapter_cell.touch_node.chapter_node.desc"]:setText(config.chapter .. "-" .. config.stage)

    local starNode = uiMap["chapter_cell.touch_node.star_node"]
    if not self.hStars then
        self.hStars = {}
        for i = 1, 3 do
            self.hStars[i] = uiMap["chapter_cell.touch_node.star_node.star_h_" .. i]
        end
    end

    local spineNode = uiMap["chapter_cell.touch_node.spine_node"]
    local spineObj = uiMap["chapter_cell.touch_node.spine_node.ui_spine_obj"]
    local bgNode = uiMap["chapter_cell.touch_node.bg"]

    local armorData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR)
    local config = armorData:getConfig(id)
    local starNum = armorData:getStarNum(id)
    bgNode:setSprite(GConst.ATLAS_PATH.UI_DUGEON_ARMOR, config.stage_icon)
    spineNode:setVisible(false)
    if armorData:canFarmChapter(id) then -- 可扫荡
        farmNode:setVisible(true)
        farmNode:setSizeDeltaX(farmTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth + 25)
        chapterNode:setAnchoredPositionY(43)
        starNode:setVisible(true)
        for index, node in ipairs(self.hStars) do
            node:setVisible(true)
        end
        touchNode:addClickListener(function()
            ModuleManager.DungeonArmorManager:showFarmUI(id)
        end)

        if armorData:getRemianFarmCount(id) <= 0 then
            farmNode:setVisible(false)
        end
    elseif armorData:canFightChapter(id) then -- 可挑战
        farmNode:setVisible(false)
        if starNum > 0 then -- 通关过
            chapterNode:setAnchoredPositionY(43)
            starNode:setVisible(true)
            for index, node in ipairs(self.hStars) do
                node:setVisible(index <= starNum)
            end
        else
            chapterNode:setAnchoredPositionY(8)
            starNode:setVisible(false)
            spineNode:setVisible(true)
            spineObj:setVisible(false)
            bgNode:setSprite(GConst.ATLAS_PATH.UI_DUGEON_ARMOR, "dungeon_armor_house_bg_2")
            local monsterId = config.monster[#config.monster]
            local monsterConfig = ConfigManager:getConfig("monster")[monsterId]
            local spineName = monsterConfig.model_id
            spineObj:loadAssetAsync(spineName, function()
                spineObj:setVisible(true, 0.65)
                spineObj:playAnim("idle", true, false, true)
            end, true)
        end
        touchNode:addClickListener(function()
            ModuleManager.DungeonArmorManager:showFightUI(id)
        end)
    else -- 不可挑战
        farmNode:setVisible(false)
        chapterNode:setAnchoredPositionY(8)
        starNode:setVisible(false)

        touchNode:addClickListener(function()
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.PASS_REQUIRE))
        end)

        bgNode:setSprite(GConst.ATLAS_PATH.UI_DUGEON_ARMOR, config.stage_icon .. "_1")
    end
end

return ChapterCell