local DungeonRuneMainUI = class("DungeonRuneMainUI", BaseUI)

local RUNE_CHAPTER_CELL = "app/ui/dungeon_rune/cell/rune_chapter_cell"

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

function DungeonRuneMainUI:getCurrencyParams()
    if self.currencyParams == nil then
        self.currencyParams = {
            itemIds = {
                GConst.ItemConst.ITEM_ID_SLIVER_WING,
                GConst.ItemConst.ITEM_ID_GLOD_WING,
            },
            showType = GConst.CURRENCY_TYPE.HORIZONTAL
        }
    end
 	return self.currencyParams
end

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

function DungeonRuneMainUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_rune/dungeon_rune_main_ui.prefab"
end

function DungeonRuneMainUI:ctor()
    self.runeData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.RUNES_OPEN)
    self.chapterList = {}
    self.chapterListCount = 0
    for id, info in ipairs(ConfigManager:getConfig(self.runeData:getConfigName())) do
        table.insert(self.chapterList, id)
        self.chapterListCount = self.chapterListCount + 1
    end
    self.chapterListCount = self.chapterListCount - 1
    self.lastChapterId = table.remove(self.chapterList)
    self.targetId = self.runeData:getPassedMaxId()
    if self.targetId <= 0 then
        self.targetId = 1
    end
end

function DungeonRuneMainUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
    self:_bind()
end

function DungeonRuneMainUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_rune_main_ui.banner.btn_formation.tx_ok"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_4))
    self:refreshScrollrect()
    self:refreshFormation()
end

function DungeonRuneMainUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_rune_main_ui.banner.btn_formation"]:addClickListener(function()
        ModuleManager.CommonManager:showFormationUI(GConst.BattleConst.FORMATION_TYPE.DUNGEON_RUNE)
    end)

    uiMap["dungeon_rune_main_ui.banner.btn_back"]:addClickListener(function()
        self:closeUI()
    end)
end

function DungeonRuneMainUI:_bind()
    self:bind(self.runeData, "isDirty", function()
        self:refreshFormation()
        self:refreshAllCells()
    end)

    self:bind(DataManager.FormationData, "dirty", function()
        self:refreshFormation()
    end)

    self:bind(DataManager.HeroData, "isDirty", function()
        self:refreshFormation()
    end)
end

function DungeonRuneMainUI:refreshFormation()
    if not self.formationComp then
        local uiMap = self.root:genAllChildren()
        self.formationComp = uiMap["dungeon_rune_main_ui.hero_formation_comp"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_FORMATION_COMP)
    end
    self.formationComp:refreshByFormation(DataManager.FormationData:getDungeonRuneFormation())
end

function DungeonRuneMainUI:refreshScrollrect()
    local uiMap = self.root:genAllChildren()
    if not self.scrollRect then
        self.scrollRect = uiMap["dungeon_rune_main_ui.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
        self.scrollRect:addInitCallback(function()
            return RUNE_CHAPTER_CELL
        end)
        self.scrollRect:addRefreshCallback(function(index, cell)
            cell:refresh(self.chapterList[index], index, false, self.chapterListCount)
        end)
        self.scrollRect:addInitFinishCallback(function(cell)
            cell:addClickListener(function()
                self:onClickCell(self.chapterList[cell:getIndex()])
            end)
        end)
    end
    self.scrollRect:refillCells(self.chapterListCount, nil, self.targetId)

    local content = uiMap["dungeon_rune_main_ui.scrollrect.viewport.content"]
    content:setSizeDeltaY(279 * (self.chapterListCount - 1) + 966)
    if not self.lastChapterCell then
        self.lastChapterCell = CellManager:addCellComp(uiMap["dungeon_rune_main_ui.scrollrect.viewport.content.last_cell"], RUNE_CHAPTER_CELL)
    end
    self.lastChapterCell:refresh(self.lastChapterId, nil, true)
    self.lastChapterCell:addClickListener(function()
        self:onClickCell(self.lastChapterId)
    end)
end

function DungeonRuneMainUI:refreshAllCells()
    if not self.scrollRect then
        return
    end

    self.scrollRect:refreshAll()
end

function DungeonRuneMainUI:onClickCell(id)
    ModuleManager.DungeonRuneManager:showFightUI(id)
end

return DungeonRuneMainUI