-- 竞技场：排行榜
local ArenaRankUI = class("ArenaRankUI", BaseUI)

function ArenaRankUI:isFullScreen()
	return false
end

function ArenaRankUI:showCommonBG()
	return false
end

function ArenaRankUI:getPrefabPath()
	return "assets/prefabs/ui/arena/arena_rank_ui.prefab"
end

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

function ArenaRankUI:ctor()
	ModuleManager.ArenaManager:reqRank()
end

function ArenaRankUI:onCover()
end

function ArenaRankUI:onReshow()
end

function ArenaRankUI:onClose()
end

function ArenaRankUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

	self.txTitle = uiMap["arena_rank_ui.bg.title.tx_title"]
	self.closeBtn = uiMap["arena_rank_ui.bg.close_btn"]
	self.imgRank = uiMap["arena_rank_ui.bg.my_rank.img_rank"]
	self.txRank = uiMap["arena_rank_ui.bg.my_rank.tx_rank"]
	self.playerHeadCell = CellManager:addCellComp(uiMap["arena_rank_ui.bg.my_rank.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
	self.txName = uiMap["arena_rank_ui.bg.my_rank.tx_name"]
	self.cellGrading = uiMap["arena_rank_ui.bg.my_rank.arena_grading_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.ARENA_GRADING_CELL)
	self.scrollRectComp = uiMap["arena_rank_ui.bg.list_rank"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)

	self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_5))
	self.txName:setText(DataManager.PlayerData:getNickname())
	self.cellGrading:refresh(DataManager.ArenaData:getGradingId())
	self.playerHeadCell:refresh()

	self.scrollRectComp:addInitCallback(function()
		return "app/ui/arena/cell/arena_rank_cell"
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(index)
	end)

	self.closeBtn:addClickListener(function()
		self:closeUI()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.ARENA_RANK_SUCCESS, function()
        self:onRefresh()
    end)
end

function ArenaRankUI:onRefresh()
	self.scrollRectComp:clearCells()
	self.scrollRectComp:refillCells(DataManager.ArenaData:getRankCount())

	local rank = DataManager.ArenaData:getRank()
	if rank and rank == 1 then
		self.txRank:setActive(false)
		self.imgRank:setActive(true)
		self.imgRank:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_1")
	elseif rank and rank == 2 then
		self.txRank:setActive(false)
		self.imgRank:setActive(true)
		self.imgRank:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_2")
	elseif rank and rank == 3 then
		self.txRank:setActive(false)
		self.imgRank:setActive(true)
		self.imgRank:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_3")
	elseif rank and rank > 0 then
		self.imgRank:setActive(false)
		self.txRank:setActive(true)
		self.txRank:setText(rank)
	else
		self.imgRank:setActive(false)
		self.txRank:setActive(true)
		self.txRank:setText("--")
	end
end

return ArenaRankUI