-- 竞技场：最近战报
local ArenaRecentBattleUI = class("ArenaRecentBattleUI", BaseUI)

function ArenaRecentBattleUI:isFullScreen()
	return false
end

function ArenaRecentBattleUI:showCommonBG()
	return false
end

function ArenaRecentBattleUI:getPrefabPath()
	return "assets/prefabs/ui/arena/arena_recent_battle_ui.prefab"
end

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

function ArenaRecentBattleUI:ctor()
	ModuleManager.ArenaManager:reqRecord()
end

function ArenaRecentBattleUI:onCover()
end

function ArenaRecentBattleUI:onReshow()
end

function ArenaRecentBattleUI:onClose()
end

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

	self.txTitle = uiMap["arena_recent_battle_ui.bg.title.tx_title"]
	self.btnClose = uiMap["arena_recent_battle_ui.bg.btn_close"]
	self.txResult = uiMap["arena_recent_battle_ui.bg.tab.tx_result"]
	self.txName = uiMap["arena_recent_battle_ui.bg.tab.tx_name"]
	self.txScore = uiMap["arena_recent_battle_ui.bg.tab.tx_score"]
	self.txNone = uiMap["arena_recent_battle_ui.bg.tx_none"]
	self.records = {}
	for i = 1, 5 do
		self.records[i] = uiMap["arena_recent_battle_ui.bg.records.battle_record_cell_"..i]
	end

	self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_7))
	self.txResult:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_14))
	self.txName:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_15))
	self.txScore:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_16))

	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.ARENA_RECORD_SUCCESS, function()
		self:onRefresh()
    end)
end

function ArenaRecentBattleUI:onRefresh()
	local showCount = 0
	for idx, obj in pairs(self.records) do
		local info = DataManager.ArenaData:getRecentBattleByIdx(idx)
		if info then
			showCount = showCount + 1
			obj:setActive(true)
			self:refreshRecord(obj, info)
		else
			obj:setActive(false)
		end
	end

	if showCount == 0 then
		self.txNone:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_8))
	else
		self.txNone:setText("")
	end
end

function ArenaRecentBattleUI:refreshRecord(obj, info)
	local uiMap = obj:genAllChildren()

	local playerHeadCell = CellManager:addCellComp(uiMap["player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
	playerHeadCell:refresh(info.match_info.avatar, info.match_info.avatar_frame)
	uiMap["player_head_cell"]:addClickListener(function()
		ModuleManager.TipsManager:showHeroFormation(uiMap["player_head_cell"], GFunc.formatPlayerFormationInfo(info.match_info))
	end)
	local name = info.match_info.name
    if name == nil or #name == 0 then
        name = I18N:getGlobalText(I18N.GlobalConst.NEW_PLAYER)
    end
	uiMap["tx_name"]:setText(name)
	if info.win then
		obj:setSprite(GConst.ATLAS_PATH.ARENA,"arena_bg_3")
		uiMap["tx_score"]:setText("<color=#2BFF35>+"..info.incr_score.."</color>")
		uiMap["img_result"]:setSprite(GConst.ATLAS_PATH.ARENA, "arena_dec_2")
		if info.attacker then
			uiMap["tx_result"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_19))
		else
			uiMap["tx_result"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_17))
		end
	else
		obj:setSprite(GConst.ATLAS_PATH.ARENA,"arena_bg_4")
		if info.incr_score == 0 then
			uiMap["tx_score"]:setText("<color=#FF5454>-"..info.incr_score.."</color>")
		else
			uiMap["tx_score"]:setText("<color=#FF5454>"..info.incr_score.."</color>")
		end
		uiMap["img_result"]:setSprite(GConst.ATLAS_PATH.ARENA, "arena_dec_3")
		if info.attacker then
			uiMap["tx_result"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_20))
		else
			uiMap["tx_result"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_18))
		end
	end

	local timeObj = uiMap["tx_time"]
	local time = GFunc.formatTimeStep(info.occur_at)
	timeObj:setText(Time:formatBeforeTimeStr(time))
end

return ArenaRecentBattleUI