local DungeonRuneRankUI = class("DungeonRuneRankUI", BaseUI)

local RANK_ICON = {
    "act_ranking_1",
    "act_ranking_2",
    "act_ranking_3"
}
local PAGE_ICON = {"common_menu_1", "common_menu_2"}
local RANK_CELL = "app/ui/dungeon_rune/cell/rank_cell"

local PAGE_TYPE = {
    TODAY = 1,
    LAST_DAY = 2,
    REWARD = 3,
}

local PAGE_TX = {
    [PAGE_TYPE.LAST_DAY] = I18N.GlobalConst.DUNGEON_RUNE_DESC_9,
    [PAGE_TYPE.TODAY] = I18N.GlobalConst.DUNGEON_RUNE_DESC_8,
    [PAGE_TYPE.REWARD] = I18N.GlobalConst.ACT_BOSS_RUSH_DESC_22,
}

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

function DungeonRuneRankUI:isFullScreen()
    return false
end

function DungeonRuneRankUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_rune/dungeon_rune_rank_ui.prefab"
end

function DungeonRuneRankUI:ctor()
    self.runeData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.RUNES_OPEN)
end

function DungeonRuneRankUI:onLoadRootComplete()
    self:initScrollrect()
    self:_display()
    self:_addListeners()
    self:_bind()

    self:switchPage(PAGE_TYPE.TODAY)

    self:refreshTime()
    if self.actCountdownSid then
        self:unscheduleGlobal(self.actCountdownSid)
    end
    self.actCountdownSid = self:scheduleGlobal(function()
        self:refreshTime()
	end, 1)
end

function DungeonRuneRankUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_rune_rank_ui.bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_5))
    uiMap["dungeon_rune_rank_ui.bg.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_TIP_1))
end

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

function DungeonRuneRankUI:_bind()
    self:bind(self.runeData, "isDirty", function()
        self:refreshRankNode()
        self:refreshMyRankInfo()
        self:refreshPages()
    end)
end

function DungeonRuneRankUI:initScrollrect()
    if self.scrollRectComp then
        return
    end

    local uiMap = self.root:genAllChildren()
    self.scrollRectComp = uiMap["dungeon_rune_rank_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return RANK_CELL
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
        local info = self.rankList[index + 3]
		cell:refresh(index, info)
	end)
    self.scrollRectComp:clearCells()
    self.scrollRectComp:setTotalCount(0)
end

function DungeonRuneRankUI:refreshRankNode()
    if self.curPage == PAGE_TYPE.LAST_DAY then
        self.rankList = self.runeData:getLastRankInfo()
    elseif self.curPage == PAGE_TYPE.TODAY then
        self.rankList = self.runeData:getCurRankInfo()
    else
        self.rankList = {}
    end
    self.scrollRectComp:refillCells(#self.rankList)
end

function DungeonRuneRankUI:refreshPages()
    if not self.pageObjs then
        local uiMap = self.root:genAllChildren()
        self.pageObjs = {}
        for i = 1, 3 do
            self.pageObjs[i] = {
                page = uiMap["dungeon_rune_rank_ui.bg.page_" .. i],
                tx1 = uiMap["dungeon_rune_rank_ui.bg.page_" .. i .. ".tx"],
                tx2 = uiMap["dungeon_rune_rank_ui.bg.page_" .. i .. ".tx_hl"],
            }
            self.pageObjs[i].page:addClickListener(function()
                self:switchPage(i)
            end)
        end
    end
    for index, objs in ipairs(self.pageObjs) do
        if index == self.curPage then
            objs.page:setSprite(GConst.ATLAS_PATH.COMMON, PAGE_ICON[1])
            objs.page:setAnchoredPositionY(-56)
            objs.tx1:setText(GConst.EMPTY_STRING)
            objs.tx2:setText(I18N:getGlobalText(PAGE_TX[index]))
        else
            objs.page:setSprite(GConst.ATLAS_PATH.COMMON, PAGE_ICON[2])
            objs.page:setAnchoredPositionY(-45)
            objs.tx1:setText(I18N:getGlobalText(PAGE_TX[index]))
            objs.tx2:setText(GConst.EMPTY_STRING)
        end
    end

    if self.runeData:canGetRankReward() then
        self.pageObjs[3].page:addRedPoint(77, -6.5, 1)
    else
        self.pageObjs[3].page:removeRedPoint()
    end
end

function DungeonRuneRankUI:switchPage(page)
    if self.curPage == page then
        return
    end
    self.curPage = page
    self:refreshRankNode()
    self:refreshPages()
    self:refreshMyRankInfo()
end

function DungeonRuneRankUI:refreshMyRankInfo()
    local info
    local canGot = self.runeData:canGetRankReward()

    if self.curPage == PAGE_TYPE.TODAY then
        info = self.runeData:getCurRankSelfInfo()
    else
        info = self.runeData:getLastSelfInfo()
    end

    local rank = info and info.rank or 0
    local layer = info and info.wave or 0
    local round = info and info.round or 0
    local uiMap = self.root:genAllChildren()
    if not self.playerHeadCell then
        self.playerHeadCell = CellManager:addCellComp(uiMap["dungeon_rune_rank_ui.bg.my_node.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
        self.selfRewardCells = {}
        for i = 1, 3 do
            self.selfRewardCells[i] = CellManager:addCellComp(uiMap["dungeon_rune_rank_ui.bg.my_node.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
        end
    end

    self.playerHeadCell:refresh()
    uiMap["dungeon_rune_rank_ui.bg.my_node.tx_name"]:setText(DataManager.PlayerData:getNickname())
    local rankObj = uiMap["dungeon_rune_rank_ui.bg.my_node.tx_rank"]
    if not rank or rank <= 0 then
        rankObj:setText("--")
    else
        rankObj:setText(rank)
        if rank > GConst.DungeonRuneConst.RANK_MAX_NUM then
            rankObj:setText(GConst.DungeonRuneConst.RANK_MAX_NUM .. "+")
        end
    end

    local imgRank = uiMap["dungeon_rune_rank_ui.bg.my_node.img_rank"]
    if RANK_ICON[rank] then
        imgRank:setVisible(true)
        imgRank:setSprite(GConst.ATLAS_PATH.ACT_COMMON, RANK_ICON[rank])
        rankObj:setText(GConst.EMPTY_STRING)
    else
        imgRank:setVisible(false)
    end

    local layerObj = uiMap["dungeon_rune_rank_ui.bg.my_node.tx_max"]
    if not layer then
        layerObj:setText("--")
    else
        layerObj:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_10, layer))
    end

    local roundObj = uiMap["dungeon_rune_rank_ui.bg.my_node.tx_round"]
    if not round then
        roundObj:setText("--")
    else
        roundObj:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_11, round))
    end

    local got = self.runeData:isGotRankReward()
    local rewards
    if canGot then
        rewards = self.runeData:getRankRewards(rank)
    else
        info = self.runeData:getCurRankSelfInfo()
        rewards = self.runeData:getRankRewards(info.rank or 0)
        got = false
    end

    for index, cell in ipairs(self.selfRewardCells) do
        if rewards and rewards[index] then
            cell:refreshByConfig(rewards[index], got, got)
            cell:getBaseObject():setActive(true)
            if canGot then
                cell:showFrameAnimation()
                cell:addClickListener(function()
                    ModuleManager.ActBossRushManager:reqRankReward()
                end)
            else
                cell:hideFrameAnimation()
                cell:addClickListener(nil)
            end
        else
            cell:getBaseObject():setActive(false)
        end
    end
end

function DungeonRuneRankUI:refreshTime()
    local uiMap = self.root:genAllChildren()
    local remainTime = self.runeData:getCloseTime()
    local str
    str = Time:formatNumTimeStr(remainTime)
    uiMap["dungeon_rune_rank_ui.bg.time_node.tx_time"]:setText(str)
end

return DungeonRuneRankUI