local RuneChapterCell = class("RuneChapterCell", BaseCell)

local BG = {
    "assets/arts/textures/background/dungeon_rune/dungeon_equip_bg_3.png",
    "assets/arts/textures/background/dungeon_rune/dungeon_equip_bg_2.png",
    "assets/arts/textures/background/dungeon_rune/dungeon_equip_bg_5.png",
}

function RuneChapterCell:refresh(id, index, isFinal, chapterListCount)
    local uiMap = self:getUIMap()
    local bg = uiMap["chapter_cell.bg"]
    local touchNode = uiMap["chapter_cell.touch_node"]
    if not isFinal then
        local bgAsset = BG[2]
        local isLeft = index % 2 ~= 0
        if isLeft then
            bgAsset = BG[1]
            touchNode:setAnchoredPositionX(-190)
        else
            touchNode:setAnchoredPositionX(190)
        end
        if chapterListCount - 1 == index then
            bgAsset = BG[3]
        end
        bg:setTexture(bgAsset)
        bg:setVisible(chapterListCount ~= index)
    end

    local runeData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.RUNES_OPEN)
    local maxPassedId = runeData:getPassedMaxId()
    local fightBg = uiMap["chapter_cell.touch_node.bg"]
    local curBg = uiMap["chapter_cell.touch_node.bg_cur"]
    local lightImg = uiMap["chapter_cell.touch_node.img_light"]
    local sweep = uiMap["chapter_cell.touch_node.sweep_bg"]
    local descMinRound = uiMap["chapter_cell.touch_node.bg.desc_min_round"]
    descMinRound:setVisible(false)
    lightImg:setVisible(false)
    sweep:setVisible(false)
    local passed = false
    local isBossChapter = runeData:isBossChapter(id)
    curBg:setVisible(isBossChapter)
    fightBg:setVisible(not isBossChapter)
    local curLayerDecs
    local curLock
    local curMask
    if isBossChapter then
        curLayerDecs = uiMap["chapter_cell.touch_node.bg_cur.desc"]
        curLock = uiMap["chapter_cell.touch_node.bg_cur.lock"]
        curMask = uiMap["chapter_cell.touch_node.bg_cur.bg_mask"]
    else
        curLayerDecs = uiMap["chapter_cell.touch_node.bg.desc"]
        curLock = uiMap["chapter_cell.touch_node.bg.lock"]
        curMask = uiMap["chapter_cell.touch_node.bg.bg_mask"]
    end

    curLayerDecs:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_6, id))
    if maxPassedId >= id then -- 已通关的
        passed = true
        local canSweep = runeData:canSweep(id)
        sweep:setVisible(canSweep)
        if canSweep then
            uiMap["chapter_cell.touch_node.sweep_bg.tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
        end
        curLock:setVisible(false)
        curMask:setVisible(false)
        curLayerDecs:setAnchoredPositionX(0)
        -- lightImg:setVisible(true)
        -- descMinRound:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_MIN, runeData:getChapterPassRound(id)))
        -- descMinRound:setVisible(true)
    else
        if runeData:canFight(id) then
            curLock:setVisible(false)
            curMask:setVisible(false)
            curLayerDecs:setAnchoredPositionX(0)
        else
            curLock:setVisible(true)
            curMask:setVisible(true)
            GFunc.centerImgAndTx(curLock, curLayerDecs, 5)
        end
    end

    if not self.rewardCells then
        self.rewardCells = {}
        for i = 1, 2 do
            self.rewardCells[i] = CellManager:addCellComp(uiMap["chapter_cell.touch_node.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
        end
    end

    local rewards = runeData:getChapterRewards(id)
    local rewardCount = 0
    for i, cell in ipairs(self.rewardCells) do
        if rewards[i] then
            rewardCount = rewardCount + 1
            cell:refreshByConfig(rewards[i], passed, passed)
            if isBossChapter then
                cell:getBaseObject():setLocalScale(0.6, 0.6, 0.6)
            else
                cell:getBaseObject():setLocalScale(0.5, 0.5, 0.5)
            end
            cell:addClickListener(function()
                if self.clickFunc then
                    self.clickFunc()
                end
            end)
            cell:getBaseObject():setActive(true)
        else
            cell:getBaseObject():setActive(false)
        end
    end

    if isBossChapter then
        self.rewardCells[1]:getBaseObject():setAnchoredPositionX(-43)
        self.rewardCells[2]:getBaseObject():setAnchoredPositionX(43)
    else
        self.rewardCells[1]:getBaseObject():setAnchoredPositionX(-35)
        self.rewardCells[2]:getBaseObject():setAnchoredPositionX(35)
    end

    if rewardCount < 2 then
        self.rewardCells[1]:getBaseObject():setAnchoredPositionX(0)
    end

    sweep:addClickListener(function()
        if self.clickFunc then
            self.clickFunc()
        end
    end)
end

function RuneChapterCell:addClickListener(func)
    local uiMap = self:getUIMap()
    self.clickFunc = func
    uiMap["chapter_cell.touch_node"]:addClickListener(func)
end

return RuneChapterCell