local DungeonWeaponIntroductionUI = class("DungeonWeaponIntroductionUI", BaseUI)

local PAGE = {
    INTRODUTION = 1,
    PASS = 2
}

function DungeonWeaponIntroductionUI:isFullScreen()
    return false
end

function DungeonWeaponIntroductionUI:ctor(params)
    self.chapterId = params.chapterId
end

function DungeonWeaponIntroductionUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_weapon/dungeon_weapon_introduction_ui.prefab"
end

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

function DungeonWeaponIntroductionUI:_display()
    local uiMap = self.root:genAllChildren()
    local page1Tx = uiMap["dungeon_weapon_introduction_ui.bg.page_1.normal_text"]
    local page1HTx = uiMap["dungeon_weapon_introduction_ui.bg.page_1.high_light_text"]
    local page2Tx = uiMap["dungeon_weapon_introduction_ui.bg.page_2.normal_text"]
    local page2HTx = uiMap["dungeon_weapon_introduction_ui.bg.page_2.high_light_text"]
    page1Tx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_8))
    page1HTx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_8))
    page2Tx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_9))
    page2HTx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_9))

    uiMap["dungeon_weapon_introduction_ui.bg.title.tx_title"]:setText(I18N:getConfig("chapter_dungeon_equip")[self.chapterId].name)
    uiMap["dungeon_weapon_introduction_ui.bg.records.title.tx_title"]:setText(I18N:getConfig("chapter_dungeon_equip")[self.chapterId].name)

    self:switch(PAGE.INTRODUTION)
end

function DungeonWeaponIntroductionUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_weapon_introduction_ui.bg.btn_close"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["dungeon_weapon_introduction_ui.bg.page_1"]:addClickListener(function()
        self:switch(PAGE.INTRODUTION)
    end)

    uiMap["dungeon_weapon_introduction_ui.bg.page_2"]:addClickListener(function()
        self:switch(PAGE.PASS)
    end)
end

function DungeonWeaponIntroductionUI:_bind()
    self:bind(DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON), "isDirty", function()
        if self.page == PAGE.PASS then
            self:refreshPassInfo()
        end
    end)
end

function DungeonWeaponIntroductionUI:refreshIntroduction()
    local uiMap = self.root:genAllChildren()
    local config = ConfigManager:getConfig("chapter_dungeon_equip")[self.chapterId]
    if not config then
        return
    end

    local txNode = uiMap["dungeon_weapon_introduction_ui.bg.introdution_node.tx_none"]
    txNode:setText(I18N:getConfig("chapter_dungeon_equip")[self.chapterId].desc)
    txNode:setSizeDeltaY(txNode:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredHeight)
    txNode:setAnchoredPositionY(0)

    local spineObj = uiMap["dungeon_weapon_introduction_ui.bg.introdution_node.ui_spine_obj"]
    spineObj:setVisible(false)
    local monsterId = config.monster[#config.monster]
    local monsterConfig = ConfigManager:getConfig("monster")[monsterId]
    local spineName = monsterConfig.model_id
    spineObj:loadAssetAsync(spineName, function()
        spineObj:setVisible(true, monsterConfig.model_ui)
        spineObj:playAnim("idle", true, false, true)
    end, true)
end

function DungeonWeaponIntroductionUI:refreshPassInfo()
    if not self.cacheInfo then
        local weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
        local info = weaponData:getCachePassInfo(self.chapterId)
        if info then
            self.cacheInfo = {}
            local indexList = {}
            local indexCount = 0
            for index, _ in ipairs(info) do
                table.insert(indexList, index)
                indexCount = indexCount + 1
            end

            for i = 1, 3 do
                if indexCount <= 0 then
                    break
                end
                local index = table.remove(indexList, math.random(1, indexCount))
                self.cacheInfo[i] = info[index]
                indexCount = indexCount - 1
            end
        end
    end

    local uiMap = self.root:genAllChildren()
    if not self.playerRecordCells then
        self.playerRecordCells = {}
        for i = 1, 3 do
            self.playerRecordCells[i] = CellManager:addCellComp(uiMap["dungeon_weapon_introduction_ui.bg.records.player_record_cell_" .. i], GConst.TYPEOF_LUA_CLASS.PLAYER_RECORD_CELL)
        end
    end

    local notx = uiMap["dungeon_weapon_introduction_ui.bg.records.tx_none"]
    if not self.cacheInfo or table.nums(self.cacheInfo) <= 0 then -- 没有通关的人
        for index, cell in pairs(self.playerRecordCells) do
            cell:setVisible(false)
        end
        notx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_12))
    else
        for index, cell in pairs(self.playerRecordCells) do
            if self.cacheInfo[index] then
                cell:refresh(self.cacheInfo[index])
                cell:setVisible(true)
            else
                cell:setVisible(false)
            end
        end
        notx:setText(GConst.EMPTY_STRING)
    end
end

function DungeonWeaponIntroductionUI:switch(page)
    if self.page == page then
        return
    end

    local uiMap = self.root:genAllChildren()
    local page1Tx = uiMap["dungeon_weapon_introduction_ui.bg.page_1.normal_text"]
    local page1HTx = uiMap["dungeon_weapon_introduction_ui.bg.page_1.high_light_text"]
    local page1H = uiMap["dungeon_weapon_introduction_ui.bg.page_1.high_light"]
    local page2Tx = uiMap["dungeon_weapon_introduction_ui.bg.page_2.normal_text"]
    local page2HTx = uiMap["dungeon_weapon_introduction_ui.bg.page_2.high_light_text"]
    local page2H = uiMap["dungeon_weapon_introduction_ui.bg.page_2.high_light"]
    local introdutionNode = uiMap["dungeon_weapon_introduction_ui.bg.introdution_node"]
    local recordsNode = uiMap["dungeon_weapon_introduction_ui.bg.records"]
    self.page = page
    if self.page == PAGE.INTRODUTION then
        page1Tx:setVisible(false)
        page1HTx:setVisible(true)
        page1H:setVisible(true)

        page2Tx:setVisible(true)
        page2HTx:setVisible(false)
        page2H:setVisible(false)

        self:refreshIntroduction()
        introdutionNode:setActive(true)
        recordsNode:setActive(false)
    else
        page1Tx:setVisible(true)
        page1HTx:setVisible(false)
        page1H:setVisible(false)

        page2Tx:setVisible(false)
        page2HTx:setVisible(true)
        page2H:setVisible(true)

        self:refreshPassInfo()
        introdutionNode:setActive(false)
        recordsNode:setActive(true)
    end
end

return DungeonWeaponIntroductionUI