local DungeonRuneTaskUI = class("DungeonRuneTaskUI", BaseUI)

local TASK_CELL = "app/ui/dungeon_rune/cell/rune_battle_task_cell"

function DungeonRuneTaskUI:onPressBackspace()
    self:closeUI()
end

function DungeonRuneTaskUI:isFullScreen()
    return false
end

function DungeonRuneTaskUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_rune/dungeon_rune_task_ui.prefab"
end

function DungeonRuneTaskUI:ctor(params)
    self.runeData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.RUNES_OPEN)

    self.id = params.id
end

function DungeonRuneTaskUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
end

function DungeonRuneTaskUI:_display()
    local uiMap = self.root:genAllChildren()
    if not self.taskCells then
        self.taskCells = {}
        for i = 1, 3 do
            self.taskCells[i] = CellManager:addCellComp(uiMap["dungeon_rune_task_ui.bg.task_cell_" .. i], TASK_CELL)
        end
    end

    local chapterCondition = self.runeData:getChapterCondition(self.id)
    local count = 0
    local taskProgress = ModuleManager.DungeonRuneManager:getTaskStatus(ModuleManager.BattleManager.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 taskNum = taskProgress[newIndex] and taskProgress[newIndex].progress
            local desc = self.runeData:getConditionDesc(condition, taskNum)
            local over = false
            if taskProgress[newIndex] then
                local info = taskProgress[newIndex]
                over = info.over
                -- local color = "#FF4949"
                -- if over then
                --     color = "#49FF49"
                -- end
                -- desc = desc .. string.format("<color=%s>(%s/%s)</color>", color, info.progress, info.totalProgress)
            end
            cell:refresh(desc, condition, over)
            count = count + 1
        else
            cell:getBaseObject():setActive(false)
        end
    end

    local y = 124 + 152 * count
    uiMap["dungeon_rune_task_ui.bg"]:setSizeDeltaY(y)

    uiMap["dungeon_rune_task_ui.bg.title_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_NAME))
end

function DungeonRuneTaskUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_rune_task_ui.bg.close_btn"]:addClickListener(function()
        self:closeUI()
    end)
end

return DungeonRuneTaskUI