local BattleBaseUI = require "app/ui/battle/battle_base_ui"
local BattleUI = class("BattleUI", BattleBaseUI)
local GRID_CELL = "app/ui/battle/cell/grid_cell"
local SELECT_SKILL_CELL = "app/ui/battle/cell/select_skill_cell"
local BATTLE_SELECT_SKILL_COMP = "app/ui/battle/battle_skill_select_comp"
local ELIMINATION_TOUCH_EVENT = GConst.ELIMINATION_TOUCH_EVENT
local BATTLE_TASK_CELL = "app/ui/battle/cell/battle_task_cell"

local DEFAULT_X = 10000
local BOARD_POS_UP = BF.Vector2(0, 47)
local BOARD_POS_DOWN = BF.Vector2(0, -740)
local CacheVector2 = CS.UnityEngine.Vector2(0, 0)
---------------------------------必须重写的方法----------------------------------
function BattleUI:initBaseInfo()
    local uiMap = self.root:genAllChildren()
    self.uiMap = uiMap
    self.gridNode = uiMap["battle_ui.bg_2.board_node.grid_node"]
    self.boardNode = uiMap["battle_ui.bg_2.board_node"]
    self.boardCenterNode = uiMap["battle_ui.bg_2.board_node.board_center_node"]
    self.boardNode:setAnchoredPositionX(DEFAULT_X)
    self.boardMask2D = self.gridNode:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_RECT_MASK_2D)
    self.boardMask = uiMap["battle_ui.bg_2.board_node.grid_node.board_mask"]
    self.boardMask:setVisible(false)
    self.boardCacheNode = uiMap["battle_ui.bg_2.board_cache_node"]
    self.boardCacheNode:setVisible(false)
    self.boardCacheBox = uiMap["battle_ui.bg_2.board_cache_node.skill_box"]
    self.boardCacheBox:setAnchoredPositionX(DEFAULT_X)
    self.battleRoot = uiMap["battle_ui.battle_root"]
    self.maxLayerNode = uiMap["battle_ui.battle_root.battle_node.max_layer_show_node"]
    self.aniNode = uiMap["battle_ui.bg_2.ani_node"]
    self.gridEdgeNode = uiMap["battle_ui.bg_2.board_node.board_center_node.grid_edge_node"]
    self.gridEdgeCacheCell = uiMap["battle_ui.cache_node.grid_edge_cell"]
    self.topNode = uiMap["battle_ui.top_node"]

    -- taskNode
    self.taskNode = uiMap["battle_ui.top_node.task_node"]
    if not self.taskCells then
        self.taskCells = {}
        for i = 1, 4 do
            self.taskCells[i] = CellManager:addCellComp(uiMap["battle_ui.top_node.task_node.battle_task_cell_" .. i], BATTLE_TASK_CELL)
        end
    end
    self.taskNode:setVisible(false)


    if self.topNode then
        local comp = self.topNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_CANVAS_SORTING_ORDER_HELPER)
        if not comp then
            comp = self.topNode:addComponent(GConst.TYPEOF_UNITY_CLASS.BF_CANVAS_SORTING_ORDER_HELPER)
        end
        local gr = self.topNode:getComponent(GConst.TYPEOF_UNITY_CLASS.GRAPHIC_RAYCASTER)
        if not gr then
            self.topNode:addComponent(GConst.TYPEOF_UNITY_CLASS.GRAPHIC_RAYCASTER)
        end
        if comp then
            comp:SetSortingOrder(self:getUIOrder(), 12)
        end
    end
end

function BattleUI:initBg()
    self.bg = self.uiMap["battle_ui.battle_root.bg"]
    self.bg:setLocalScale(0, 0, 0)
    local width = self.bg:fastGetSizeDelta()
    self.bg:setAnchoredPositionX(width/4)
end
function BattleUI:initSkill()
    local uiMap = self.root:genAllChildren()
    local atkNode = uiMap["battle_ui.bg_2.skill_node"]
    local atkCellPrefix = "battle_ui.bg_2.skill_node.skill_node_cell_"
    self:_initSkill(atkNode, atkCellPrefix, nil, nil)
end

function BattleUI:initBuff()
    local atkBuffPrefix = "battle_ui.top_node.buff_l.tiny_buff_cell_"
    local defBuffPrefix = "battle_ui.top_node.buff_r.tiny_buff_cell_"
    local battleBuffTipsRoot = self.uiMap["battle_ui.battle_buff_tips"]
    local battleBuffTipsMask = self.uiMap["battle_ui.battle_buff_tips.mask"]
    local battleBuffTipsBg = self.uiMap["battle_ui.battle_buff_tips.bg"]
    local battleBuffTipsBuff = self.uiMap["battle_ui.battle_buff_tips.bg.buff"]
    self:_initBuff(atkBuffPrefix, defBuffPrefix, battleBuffTipsRoot, battleBuffTipsMask, battleBuffTipsBg, battleBuffTipsBuff)
end

function BattleUI:initBossSkill()
	local bossSkillPrefix = "battle_ui.top_node.buff_boss.boss_skill_cell_"
	self:_initBossSkill(bossSkillPrefix)
end

function BattleUI:initBattlefield()
    self.battleNode = self.uiMap["battle_ui.battle_root.battle_node"]
end

function BattleUI:initNumberNode()
    self.battleNumberNode = self.uiMap["battle_ui.battle_root.battle_number_node"]
    self.battleNumberRed = self.uiMap["battle_ui.cache_node.battle_number_red"]
    self.battleNumberGreen = self.uiMap["battle_ui.cache_node.battle_number_green"]
    self.battleNumberBlue = self.uiMap["battle_ui.cache_node.battle_number_blue"]
    self.battleNumberWhite = self.uiMap["battle_ui.cache_node.battle_number_white"]
    self.battleNumberSpecial = self.uiMap["battle_ui.cache_node.battle_number_special"]
end

function BattleUI:initComboNode()
    self.comboNode = self.uiMap["battle_ui.top_node.combo"]
    self.comboBg = self.uiMap["battle_ui.top_node.combo.bg"]
    self.comboBg1 = self.uiMap["battle_ui.top_node.combo.bg.bg_1"]
    self.comboTx1 = self.uiMap["battle_ui.top_node.combo.number.text.text_1"]
    self.comboTx2 = self.uiMap["battle_ui.top_node.combo.number.text.text_2"]
    self.comboFx1 = self.uiMap["battle_ui.top_node.combo.bg.vfx_ui_hit_b01"]
    self.comboFx2 = self.uiMap["battle_ui.top_node.combo.bg.vfx_ui_hit_b02"]
    self.comboFx3 = self.uiMap["battle_ui.top_node.combo.bg.vfx_ui_hit_b03"]
    self:_initComboNode()
end

function BattleUI:initHpNode()
    self.hpProgressLeft = self.uiMap["battle_ui.top_node.bg_l.atk_slider_green"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.hpProgressRight = self.uiMap["battle_ui.top_node.bg_r.def_slider_red"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.hpProgressYellowLeft = self.uiMap["battle_ui.top_node.bg_l.atk_slider_yellow"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.hpProgressYellowRight = self.uiMap["battle_ui.top_node.bg_r.def_slider_yellow"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.hpTextLeft = self.uiMap["battle_ui.top_node.atk_hp"]
    self.hpTextRight = self.uiMap["battle_ui.top_node.def_hp"]
    self:_initHpNode()
end

function BattleUI:initFxNode()
    self.fxNode = self.uiMap["battle_ui.battle_root.batttle_fx_node"]
end

function BattleUI:hideGenerateSkillGridCells()
    local generateSkillCellPrefix = "battle_ui.bg_2.ani_node.grid_cell_"
    self:_hideGenerateSkillGridCells(generateSkillCellPrefix)
end

function BattleUI:initSkillLineSfx()
    local uiMap = self.root:genAllChildren()
    if not self.skillLineSfxs then
        self.skillLineSfxs = {}
        self.skillLineSfxs[13] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_2_1h"]
        self.skillLineSfxs[11] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_2_1v"]
        self.skillLineSfxs[15] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_2_1l"]
        self.skillLineSfxs[17] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_2_1r"]

        self.skillLineSfxs[23] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_1_2h"]
        self.skillLineSfxs[21] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_1_2v"]
        self.skillLineSfxs[25] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_1_2l"]
        self.skillLineSfxs[27] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_1_2r"]

        self.skillLineSfxs[33] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_3h"]
        self.skillLineSfxs[31] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_3v"]
        self.skillLineSfxs[35] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_3l"]
        self.skillLineSfxs[37] = uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b01_3r"]
    end

    if not self.root.skillLightSfxs then
        self.root.skillLightSfxs = {
            point = {
                isLoaded = true,
                obj = self.uiMap["battle_ui.bg_2.board_node.grid_node.sfx_skill_b03"],
            }
        }
    end

    self:_initSkillLineSfx()
end

function BattleUI:initGenerateSkillEffect()
    local generateSkillEffecPrefix = "battle_ui.bg_2.ani_node.sfx_piece_skill_b01_"
    self:_initGenerateSkillEffect(generateSkillEffecPrefix)
end

function BattleUI:initCounterAttack()
    self.counterAttackNode = self.uiMap["battle_ui.battle_root.battle_number_node.counter_attack"]
    self.counterTx = self.uiMap["battle_ui.battle_root.battle_number_node.counter_attack.text_number"]
    self.counterTxbgComp = self.uiMap["battle_ui.battle_root.top_node.counter_attack.bg"]
    self.counterTxTmp = self.counterTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
    self.counterAttackNode:setVisible(false)
end

--------------------------------end必须重写的方法--------------------------------
----------------------------------按需重写的方法 可以为nil，已容错----------------

function BattleUI:initTutorialNode()
    local uiMap = self.root:genAllChildren()
    self.tutorialNode = uiMap["battle_ui.tutorial_node"]
    self.tutorialFinger = uiMap["battle_ui.tutorial_node.board_node.finger"]
    self:showTutorialFinger()
end

function BattleUI:initCommonSkillDescTips()
    local uiMap = self.root:genAllChildren()
    self.skillDescTipsNode = uiMap["battle_ui.bg_2.lv_node.skill_desc_tips"]
    self.skillDescTips = uiMap["battle_ui.bg_2.lv_node.skill_desc_tips.desc"]
    self.skillDescTipsNode:setVisible(false)
end

function BattleUI:initSelectSkillNode()
    if not self.selectSkillComp then
        local uiMap = self.root:genAllChildren()
        local obj = uiMap["battle_ui.bg_2.battle_select_skill_comp"]
        obj:initPrefabHelper()
        obj:genAllChildren()
		self.selectSkillComp = obj:addLuaComponent(BATTLE_SELECT_SKILL_COMP)
        self.selectSkillComp:hide()
    end
end

function BattleUI:initBossEnterAni()
    local uiMap = self.root:genAllChildren()
    self.bossEnterNode = uiMap["battle_ui.bg_2.boss_enter_node"]
    self.bossEnterImg = uiMap["battle_ui.bg_2.boss_enter_node.ui_spine_obj"]
    self.bossName = uiMap["battle_ui.bg_2.boss_enter_node.boss_name"]
    self.bossEnterNodeAnimator = self.bossEnterNode:getComponent(GConst.TYPEOF_UNITY_CLASS.ANIMATOR)
    self.bossEnterNodeAnimator.enabled = false
    self.bossEnterNodeCanvasGroup = self.bossEnterNode:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
    self.bossEnterNode:setVisible(false)
end

function BattleUI:initSkillSelectCells()
    if not self.skillSelectCell then
        local uiMap = self.root:genAllChildren()
        self.skillSelectCell = CellManager:addCellComp(uiMap["battle_ui.bg_2.ani_node.skill_select_cell"], SELECT_SKILL_CELL)
        self.skillSelectCell:getBaseObject():setAnchoredPositionX(DEFAULT_X)
    end
end

function BattleUI:showTutorialFinger(posIdList)
    if self.showTutorialFingerSeq then
        self.showTutorialFingerSeq:Kill()
        self.showTutorialFingerSeq = nil
        self.battleController:clearGridSequence()
    end

    if not self.tutorialFinger then
        return
    end

    local show = false
    if posIdList and posIdList[1] then
        show = true
    end
    self.tutorialNode:setVisible(show)
    if not show then
        return
    end

    self.showTutorialFingerSeq = self.root:createBindTweenSequence()
    local path = {}
    local count = 0
    for index, posId in ipairs(posIdList) do
        local curPos = self:getPosInfo(posId)
        if index == 1 then
            self.tutorialFinger:setAnchoredPosition(curPos.x, curPos.y)
        else
            table.insert(path, curPos)
            count = count + 1
        end
    end

    self.showTutorialFingerSeq:Append(self.tutorialFinger:getTransform():DOLocalPath(path, count * 0.5):SetEase(CS.DG.Tweening.Ease.Linear))
    for index, posId in ipairs(posIdList) do
        local time = (index - 1.2) * 0.5
        if time < 0 then
            time = 0
        end
        self.showTutorialFingerSeq:InsertCallback(time, function()
            local eventType
            if index == 1 then
                eventType = GConst.ELIMINATION_TOUCH_EVENT.DOWN
            else
                eventType = GConst.ELIMINATION_TOUCH_EVENT.ENTER
            end
            self.battleController:onTouchEvent(eventType, posId, true)
        end)
    end
    self.showTutorialFingerSeq:AppendInterval(0.15)
    self.showTutorialFingerSeq:AppendCallback(function()
        self.battleController:clearGridSequence()
        self.tutorialFinger:setAnchoredPositionX(DEFAULT_X)
        self:showBoardMask(nil)
    end)
    self.showTutorialFingerSeq:AppendInterval(1)
    self.showTutorialFingerSeq:SetLoops(-1)
end

function BattleUI:initTouchCancel()
    local uiMap = self.root:genAllChildren()
    local touchCancelNode = uiMap["battle_ui.bg_2.board_node.board_center_node.touch_cancel_node"]
    touchCancelNode:setVisible(false)
    touchCancelNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_ELIMINATION_TOUCH_EVENT):AddTouchEventListener(function(eventType)
        if eventType == ELIMINATION_TOUCH_EVENT.ENTER then -- 取消链接
            self.battleController:clearGridSequence()
            AudioManager:playEffect(AudioManager.EFFECT_ID.LINK_CANCEL)
            DataManager.ChapterData:addLinkTouchCancelCount()
        end
    end)
    local w, h = GFunc.getUIExpandScreenSize()
    local addW = h - CS.BF.GameConst.DESIGN_RESOLUTION_HEIGHT
    touchCancelNode:setSizeDeltaY(509 + addW)
    local allImg = uiMap["battle_ui.bg_2.board_node.board_center_node.touch_cancel_node.all_img"]
    allImg:setVisible(false)
    uiMap["battle_ui.bg_2.board_node.board_center_node.touch_cancel_node.all_img.tips_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_13))
end

function BattleUI:showTouchCancel()
    local uiMap = self.root:genAllChildren()
    local touchCancelNode = uiMap["battle_ui.bg_2.board_node.board_center_node.touch_cancel_node"]
    touchCancelNode:setVisible(true)

    -- 判断是否显示提示框
    local allImg = uiMap["battle_ui.bg_2.board_node.board_center_node.touch_cancel_node.all_img"]
    if DataManager.ChapterData:getNeedShowTouchCancelTips() then
        allImg:setVisible(true)
    else
        allImg:setVisible(false)
    end
end

function BattleUI:hideTouchCancel()
    local uiMap = self.root:genAllChildren()
    local touchCancelNode = uiMap["battle_ui.bg_2.board_node.board_center_node.touch_cancel_node"]
    touchCancelNode:setVisible(false)
end

--------------------------------end按需重写的方法--------------------------------

function BattleUI:getPrefabPath()
    return "assets/prefabs/ui/battle/battle_ui.prefab"
end

function BattleUI:_display()
    BattleBaseUI._display(self)
    self:refreshTaskBtn()
end

function BattleUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["battle_ui.top_node.close_btn"]:addClickListener(function()
        ModuleManager.BattleManager:showPauseUI(self.battleController.battleType, self.battleController)
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.SHOW_ELIMINATION_TUTORAIL, function(posIdList)
        self:showTutorialFinger(posIdList)
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.SKILL_REFRESH_SUCC, function(posIdList)
        if self.selectSkillComp then
            self.selectSkillComp:getNewRandomSkill()
        end
    end)
end

function BattleUI:_bind()
    self:bind(self.battleData, "lvDirty", function()
        self:refreshLv()
    end, true)
end

function BattleUI:moveBattlefield(time)
    local width = self.bg:fastGetSizeDelta()
    self.bg:setAnchoredPositionX(width/4)
    if self.bgMoveTween == nil then
        self.bgMoveTween = self.bg:getTransform():DOAnchorPosX(-width/4, time)
        self.bgMoveTween:SetIntId(GConst.DOTWEEN_IDS.BATTLE)
        self.bgMoveTween:SetAutoKill(false)
    else
        local x, y = self.bg:fastGetAnchoredPosition()
        CacheVector2.x = -width/4
        CacheVector2.y = y
        self.bgMoveTween:ChangeEndValue(CacheVector2, time, true)
        self.bgMoveTween:Restart()
    end
end

function BattleUI:showLeftBuffTips(buffList, autoClose)
    local x = self.battleBuffTipsBg:fastGetAnchoredPosition()
    if x > 0 then
        self.battleBuffTipsBg:setAnchoredPositionX(-x)
    end
    self:showBuffTips(buffList, autoClose)
end

function BattleUI:showRightBuffTips(buffList, autoClose)
    local x = self.battleBuffTipsBg:fastGetAnchoredPosition()
    if x < 0 then
        self.battleBuffTipsBg:setAnchoredPositionX(-x)
    end
    self:showBuffTips(buffList, autoClose)
end

function BattleUI:onInitGridCellOver(...)
    BattleBaseUI.onInitGridCellOver(self, ...)
    ---- 检查引导
    ModuleManager.TutorialManager:checkFuncTutorial(GConst.TutorialConst.START_TUTORIAL)
end

function BattleUI:switchBoard(downCallback, callback, isFirst)
    if self.switchBoardSeq then
        self.switchBoardSeq:Kill()
        self.switchBoardSeq = nil
    end

    if isFirst then
        if downCallback then
            downCallback()
        end
        if callback then
            callback()
        end
        self.boardNode:setAnchoredPositionY(BOARD_POS_UP.y)
        return
    end

    self.switchBoardSeq = self.root:createBindTweenSequence()
    self.switchBoardSeq:Append(self.boardNode:getTransform():DOAnchorPos(BOARD_POS_DOWN, 0.5))
    self.switchBoardSeq:AppendCallback(function()
        if downCallback then
            downCallback()
        end
    end)
    self.switchBoardSeq:Append(self.boardNode:getTransform():DOAnchorPos(BOARD_POS_UP, 0.5))
    self.switchBoardSeq:AppendCallback(function()
        if callback then
            callback()
        end
    end)
end

function BattleUI:hideMonsterSkillGridCells()
    if not self.monsterSkillGridEntities then
        local uiMap = self.root:genAllChildren()
        self.monsterSkillGridEntities = {}
        for name, elementType in pairs(GConst.BattleConst.ELEMENT_TYPE) do
            local obj = uiMap["battle_ui.bg_2.ani_node.grid_cell_m" .. elementType]
            if obj then
                local cell = CellManager:addCellComp(obj, GRID_CELL)
                local entity = self.battleData:getNewGridEntity()
                entity:setCell(cell)
                self.monsterSkillGridEntities[elementType] = entity
            end
        end
    end

    for _, entity in pairs(self.monsterSkillGridEntities) do
        if entity:getCell() then
            entity:getCell():getBaseObject():setAnchoredPositionX(DEFAULT_X)
        end
    end
end

function BattleUI:refreshLv()
    local uiMap = self.root:genAllChildren()
    if not self.lvSlider then
        self.lvSlider = uiMap["battle_ui.bg_2.lv_node.slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
        self.lvDesc = uiMap["battle_ui.bg_2.lv_node.icon.lv_desc"]
    end
    local curExp = self.battleData:getBattleExp()
    local curNeedExp = self.battleData:getBattleNeedExp()
    self.lvSlider.value = curExp / curNeedExp
    local lv = self.battleData:getBattleLv()
    self.lvDesc:setText(lv)
    local sfx = uiMap["battle_ui.bg_2.lv_node.icon.vfx_ui_battle_progress_light_b01"]
    if not self.lastLv then
        sfx:setActive(true)
        sfx:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_EFFECT_HELPER):SetSortingOrder(self:getUIOrder(), GConst.UI_EFFECT_ORDER.LEVEL5)
        sfx:setActive(false)
    elseif self.lastLv ~= lv then
        sfx:setActive(true)
        sfx:play()
    end
    self.lastLv = lv
end

function BattleUI:refreshWave(wave, iconAtlas, iconName)
    local uiMap = self.root:genAllChildren()
    local icon = uiMap["battle_ui.top_node.wave_icon"]
    local desc = uiMap["battle_ui.top_node.wave_desc"]
    desc:setText(wave)
    GFunc.centerImgAndTx(icon, desc, 10)

    iconAtlas = iconAtlas or GConst.ATLAS_PATH.COMMON
    iconName = iconName or "common_dec_3"
    icon:setSprite(iconAtlas, iconName)
end

function BattleUI:refreshTaskBtn()
    local uiMap = self.root:genAllChildren()
    local taskBtn = uiMap["battle_ui.top_node.task_btn"]
    if self.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE then
        taskBtn:setSprite(GConst.ATLAS_PATH.BATTLE, "battle_btn_task")
        taskBtn:setActive(true)
        taskBtn:addClickListener(function()
            ModuleManager.DailyChallengeManager:showBattleTaskUI()
        end)
    elseif self.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.DUNGEON_ARMOR then
        taskBtn:setSprite(GConst.ATLAS_PATH.BATTLE, "battle_btn_task_2")
        taskBtn:setActive(true)
        taskBtn:addClickListener(function()
            ModuleManager.DungeonArmorManager:showBattleTaskUI()
        end)
    else
        taskBtn:setActive(false)
    end
end

function BattleUI:refreshTaskNode()
    if self.battleController.battleType == GConst.BattleConst.BATTLE_TYPE.DUNGEON_RUNE then
        self.taskNode:setLocalScale(0.6, 0.6, 0.6)
        local runeData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.RUNES_OPEN)
        local chapterCondition = runeData:getChapterCondition(self.battleController.chapterId)
        local taskProgress = ModuleManager.DungeonRuneManager:getTaskStatus(self.battleController, chapterCondition)
        for index, cell in ipairs(self.taskCells) do
            local newIndex = index + 1
            local condition = chapterCondition[newIndex]
            if condition then
                cell:getBaseObject():setActive(true)

                local atlast, iconName = runeData:getConditionIcon(condition)
                local taskNum = condition[3] or 0
                local over = false
                if taskProgress[newIndex] then
                    taskNum = taskProgress[newIndex].totalProgress - taskProgress[newIndex].progress
                    if taskNum < 0 then
                        taskNum = 0
                    end
                    over = taskProgress[newIndex].over
                end
                cell:refresh(atlast, iconName, taskNum, over)
                cell:addClickListener(function()
                    ModuleManager.DungeonRuneManager:showTaskUI(self.battleController.chapterId)
                end)
            else
                cell:getBaseObject():setActive(false)
            end
        end
        self.taskNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
    end
end

return BattleUI