local ArenaRankCell = class("ArenaRankCell", BaseCell)

function ArenaRankCell:init()
    local uiMap = self:getUIMap()

    self.txRank = uiMap["arena_rank_cell.tx_rank"]
    self.imgRank = uiMap["arena_rank_cell.img_rank"]
    self.playerHeadCell = CellManager:addCellComp(uiMap["arena_rank_cell.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
    self.txName = uiMap["arena_rank_cell.tx_name"]
    self.cellGrading = uiMap["arena_rank_cell.arena_grading_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.ARENA_GRADING_CELL)
    self.btnFormation = uiMap["arena_rank_cell.btn_formation"]

    self.btnFormation:addClickListener(function()
        if DataManager.ArenaData:hasRankFormation() then
            self:showHeroFormationTips()
        else
            self.clickFormation = true
            ModuleManager.ArenaManager:reqRankHeroes(self.rankInfo.ID)
        end
	end)
    self:bind(DataManager.ArenaData, "isDirty", function()
        if self.clickFormation then
            self:showHeroFormationTips()
        end
	end)
end

function ArenaRankCell:refresh(rank)
    self.rankInfo = DataManager.ArenaData:getRankData(rank)

    self.playerHeadCell:refresh(self.rankInfo.avatar, self.rankInfo.avatar_frame)
    self.txRank:setText(self.rankInfo.rank)
    local name = self.rankInfo.name
    if name == nil or #name == 0 then
        name = I18N:getGlobalText(I18N.GlobalConst.NEW_PLAYER)
    end
    self.txName:setText(name)
    self.cellGrading:refresh(DataManager.ArenaData:getGradingIdFromScore(self.rankInfo.score))

    if rank == 1 then
		self.txRank:setActive(false)
		self.imgRank:setActive(true)
		self.imgRank:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_1")
        self.baseObject:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_bg_1")
	elseif rank == 2 then
		self.txRank:setActive(false)
		self.imgRank:setActive(true)
		self.imgRank:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_2")
        self.baseObject:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_bg_2")
	elseif rank == 3 then
		self.txRank:setActive(false)
		self.imgRank:setActive(true)
		self.imgRank:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_3")
        self.baseObject:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_bg_3")
	else
		self.imgRank:setActive(false)
		self.txRank:setActive(true)
		self.txRank:setText(rank)
        self.baseObject:setSprite(GConst.ATLAS_PATH.ARENA, "arena_ranking_bg_4")
	end
end

function ArenaRankCell:showHeroFormationTips()
    self.clickFormation = false
    local result = DataManager.ArenaData:getRankFormation(self.rankInfo.ID)
    ModuleManager.TipsManager:showHeroFormation(self.btnFormation, GFunc.formatPlayerFormationInfo(result))
end

return ArenaRankCell