local TournArenaRankCell = class("TournArenaRankCell", BaseCell)

local RANK_IMG = {"act_ranking_1", "act_ranking_2", "act_ranking_3"}
local BG = {"common_bg_8", "common_bg_9", "common_bg_10", "common_bg_11"}

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

    self.txRank = uiMap["tourn_arena_rank_cell.tx_rank"]
    self.imgRank = uiMap["tourn_arena_rank_cell.img_rank"]
    self.playerHeadCell = uiMap["tourn_arena_rank_cell.player_head_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
    self.txName = uiMap["tourn_arena_rank_cell.tx_name"]
    self.txScore = uiMap["tourn_arena_rank_cell.tx_score"]
    self.rewards = uiMap["tourn_arena_rank_cell.rewards"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
    self.rewardCells = {}
    for i = 1, 3 do
        table.insert(self.rewardCells, uiMap["tourn_arena_rank_cell.rewards.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
    end
end

function TournArenaRankCell:refresh(rank, info)
    self.playerHeadCell:refresh(info.avatar, info.avatar_frame)
    local name = info.name
    if name == nil or #name == 0 then
        name = I18N:getGlobalText(I18N.GlobalConst.NEW_PLAYER)
    end
    self.txName:setText(name)
    self.txScore:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_16)..":"..info.score)

    if BG[rank] then
        self.baseObject:setSprite(GConst.ATLAS_PATH.COMMON, BG[rank])
    else
        self.baseObject:setSprite(GConst.ATLAS_PATH.COMMON, BG[4])
    end

    if RANK_IMG[rank] then
        self.imgRank:setActive(true)
        self.imgRank:setSprite(GConst.ATLAS_PATH.ACT_COMMON, RANK_IMG[rank])
        self.txRank:setText(GConst.EMPTY_STRING)
    else
        self.imgRank:setActive(false)
        if info.score > 0 then
            self.txRank:setText(rank)
        else
            self.txRank:setText("--")
        end
    end

    local rewards = DataManager.TournArenaData:getRewardByRank(rank)
    for index, cell in ipairs(self.rewardCells) do
        if rewards[index] then
            cell:setVisible(true)
            cell:refreshByConfig(rewards[index])
        else
            cell:setVisible(false)
        end
    end
    self.rewards:RefreshLayout()
end

return TournArenaRankCell