local ActBossRushRankUI = class("ActBossRushRankUI", BaseUI)

local RANK_REWARD_CELL = "app/ui/activity/act_boss_rush/cell/rank_reward_cell"
local RANK_CELL = "app/ui/activity/act_boss_rush/cell/rank_cell"
local TOP_RANK_CELL = "app/ui/activity/act_boss_rush/cell/top_rank_cell"

local PAGE_ICON = {"common_menu_1", "common_menu_2"}

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

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

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

function ActBossRushRankUI:isFullScreen()
    return false
end

function ActBossRushRankUI:getPrefabPath()
    return "assets/prefabs/ui/activity/act_boss_rush/act_boss_rush_rank_ui.prefab"
end

function ActBossRushRankUI:ctor()
    self.rankRewardList = {}
    self.rankRewardListCount = 0
    local cfg = DataManager.ActBossRushData:getRankConfig()
    local list = DataManager.ActBossRushData:getRankRewardList()
    for index, id in ipairs(list) do
        local info = cfg[id]
        table.insert(self.rankRewardList, {id = id, ranking1 = info.ranking[1], ranking2 = info.ranking[2], rewards = info.reward})
        self.rankRewardListCount = self.rankRewardListCount + 1
    end
end

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

function ActBossRushRankUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_rank_ui.bg.rank_node.rank_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_24))
    uiMap["act_boss_rush_rank_ui.bg.rank_node.player_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_25))
    uiMap["act_boss_rush_rank_ui.bg.rank_node.record_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_26))

    if not DataManager.ActBossRushData:getIsFinalDay() then
        self:switchPage(PAGE_TYPE.TODAY)
    else
        self:switchPage(PAGE_TYPE.LAST_DAY)
    end
end

function ActBossRushRankUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_rank_ui.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["act_boss_rush_rank_ui.bg.rank_bg.btn_help"]:addClickListener(function()
        local params = {
            type = GConst.TipsConst.HELP_TIPS_TYPE.NORMAL,
            title = I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_11),
            desc = I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_27),
        }
        ModuleManager.TipsManager:showHelpTips(params)
    end)
end

function ActBossRushRankUI:_bind()
    self:bind(DataManager.ActBossRushData, "isDirty", function()
        if self.curPage ~= PAGE_TYPE.REWARD then
            self:refreshRankNode()
        else
            self:refreshRewardNode()
        end
        self:refreshMyRankInfo()
        self:refreshPages()
    end)
end

function ActBossRushRankUI:refreshRankNode()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_rank_ui.bg.rank_node"]:setActive(true)
    uiMap["act_boss_rush_rank_ui.bg.reward_node"]:setActive(false)
    local text = I18N.GlobalConst.ACT_BOSS_RUSH_DESC_20
    if self.curPage == PAGE_TYPE.LAST_DAY then
        self.rankList = DataManager.ActBossRushData:getLastRankInfo()
    else
        self.rankList = DataManager.ActBossRushData:getCurRankInfo()
        text = I18N.GlobalConst.ACT_BOSS_RUSH_DESC_21
    end
    uiMap["act_boss_rush_rank_ui.bg.title"]:setText(I18N:getGlobalText(text))
    if not self.topRankCells then
        self.topRankCells = {}
        for i = 1, 3 do
            self.topRankCells[i] = CellManager:addCellComp(uiMap["act_boss_rush_rank_ui.bg.rank_node.top_rank_cell_" .. i], TOP_RANK_CELL)
        end
    end
    for index, cell in ipairs(self.topRankCells) do
        cell:refresh(self.rankList[index])
    end
    local count = #self.rankList - 3
    if count < 0 then
        count = 0
    end
    if self.rankScrollRectComp then
        self.rankScrollRectComp:refillCells(count)
        return
    end

    self.rankScrollRectComp = uiMap["act_boss_rush_rank_ui.bg.rank_node.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.rankScrollRectComp:addInitCallback(function()
		return RANK_CELL
	end)
	self.rankScrollRectComp:addRefreshCallback(function(index, cell)
        local info = self.rankList[index + 3]
		cell:refresh(index + 3, info)
	end)
    self.rankScrollRectComp:clearCells()
    self.rankScrollRectComp:setTotalCount(0)
    self.rankScrollRectComp:refillCells(count)
end

function ActBossRushRankUI:refreshRewardNode()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_rank_ui.bg.rank_node"]:setActive(false)
    uiMap["act_boss_rush_rank_ui.bg.reward_node"]:setActive(true)
    uiMap["act_boss_rush_rank_ui.bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_22))
    if self.rewardScrollRectComp then
        self.rewardScrollRectComp:updateAllCell()
        return
    end

    self.rewardScrollRectComp = uiMap["act_boss_rush_rank_ui.bg.reward_node.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.rewardScrollRectComp:addInitCallback(function()
		return RANK_REWARD_CELL
	end)
	self.rewardScrollRectComp:addRefreshCallback(function(index, cell)
        local info = self.rankRewardList[index]
		cell:refresh(info.ranking1, info.ranking2, info.rewards, self.rankRewardListCount)
	end)
    self.rewardScrollRectComp:clearCells()
    self.rewardScrollRectComp:setTotalCount(0)
    self.rewardScrollRectComp:refillCells(#self.rankRewardList)
end

function ActBossRushRankUI:refreshPages()
    if not self.pageObjs then
        local uiMap = self.root:genAllChildren()
        self.pageObjs = {}
        for i = 1, 3 do
            self.pageObjs[i] = {
                page = uiMap["act_boss_rush_rank_ui.bg.page_" .. i],
                tx1 = uiMap["act_boss_rush_rank_ui.bg.page_" .. i .. ".tx"],
                tx2 = uiMap["act_boss_rush_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(-516.5)
            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(-505.5)
            objs.tx1:setText(I18N:getGlobalText(PAGE_TX[index]))
            objs.tx2:setText(GConst.EMPTY_STRING)
        end
    end

    if DataManager.ActBossRushData:getIsFinalDay() then
        self.pageObjs[2].page:setActive(false)
        self.pageObjs[1].page:setActive(true)
        self.pageObjs[3].page:setActive(true)
        self.pageObjs[1].page:setAnchoredPositionX(-90)
        self.pageObjs[3].page:setAnchoredPositionX(90)
    elseif DataManager.ActBossRushData:getIsFirstDay() then
        self.pageObjs[1].page:setActive(false)
        self.pageObjs[2].page:setActive(true)
        self.pageObjs[3].page:setActive(true)
        self.pageObjs[2].page:setAnchoredPositionX(-90)
        self.pageObjs[3].page:setAnchoredPositionX(90)
    else
        local x = -186
        for index, objs in ipairs(self.pageObjs) do
            objs.page:setAnchoredPositionX(x)
            x = x + 186
            objs.page:setActive(true)
        end
    end

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

function ActBossRushRankUI:switchPage(page)
    if self.curPage == page then
        return
    end
    self.curPage = page

    if self.curPage ~= PAGE_TYPE.REWARD then
        self:refreshRankNode()
    else
        self:refreshRewardNode()
    end

    self:refreshPages()
    self:refreshMyRankInfo()
end

function ActBossRushRankUI:refreshMyRankInfo()
    local info = DataManager.ActBossRushData:getLastSelfInfo()
    local canGot = DataManager.ActBossRushData:canGetRankReward()

    if self.curPage == PAGE_TYPE.TODAY then
        info = DataManager.ActBossRushData:getCurRankSelfInfo()
    end
    if self.curPage == PAGE_TYPE.REWARD and not canGot then
        info = DataManager.ActBossRushData:getCurRankSelfInfo()
    end
    local rank = info and info.rank or 0
    local wave = 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["act_boss_rush_rank_ui.bg.rank_bg.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
        self.selfRewardCells = {}
        for i = 1, 2 do
            self.selfRewardCells[i] = CellManager:addCellComp(uiMap["act_boss_rush_rank_ui.bg.rank_bg.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
        end
    end

    self.playerHeadCell:refresh()
    uiMap["act_boss_rush_rank_ui.bg.rank_bg.name"]:setText(DataManager.PlayerData:getNickname())
    local descObj = uiMap["act_boss_rush_rank_ui.bg.rank_bg.tx_last_day"]
    local rankObj = uiMap["act_boss_rush_rank_ui.bg.rank_bg.tx_rank"]
    if not rank or rank <= 0 then
        rankObj:setText("--")
    else
        rankObj:setText(rank)
    end

    local waveObj = uiMap["act_boss_rush_rank_ui.bg.rank_bg.tx_wave"]
    if not wave then
        waveObj:setText("--")
    else
        waveObj:setText(wave)
    end

    local roundObj = uiMap["act_boss_rush_rank_ui.bg.rank_bg.tx_round"]
    if not round then
        roundObj:setText("--")
    else
        roundObj:setText(round)
    end

    local imgWave = uiMap["act_boss_rush_rank_ui.bg.rank_bg.wave_icon"]
    local imgRound = uiMap["act_boss_rush_rank_ui.bg.rank_bg.round_icon"]
    if self.curPage ~= PAGE_TYPE.REWARD then
        imgWave:setVisible(true)
        imgRound:setVisible(true)
        waveObj:setVisible(true)
        roundObj:setVisible(true)
        for index, cell in ipairs(self.selfRewardCells) do
            cell:getBaseObject():setActive(false)
        end
        if self.curPage == PAGE_TYPE.LAST_DAY then
            descObj:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_20))
        else
            descObj:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_21))
        end
    else
        imgWave:setVisible(false)
        imgRound:setVisible(false)
        waveObj:setVisible(false)
        roundObj:setVisible(false)

        local got = DataManager.ActBossRushData:isGotRankReward()
        local rewards
        if canGot then
            rewards = DataManager.ActBossRushData:getRankRewards(rank)
            descObj:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_20))
        else
            info = DataManager.ActBossRushData:getCurRankSelfInfo()
            rewards = DataManager.ActBossRushData:getRankRewards(info.rank or 0)
            got = false
            descObj:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_21))
        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
end

return ActBossRushRankUI