-- 竞技场：赛季奖励
local ArenaSeasonRewardUI = class("ArenaSeasonRewardUI", BaseUI)
local MAX_SCROLL_SHOW_COUNT = 10

function ArenaSeasonRewardUI:isFullScreen()
	return false
end

function ArenaSeasonRewardUI:showCommonBG()
	return false
end

function ArenaSeasonRewardUI:getPrefabPath()
	return "assets/prefabs/ui/arena/arena_season_reward_ui.prefab"
end

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

function ArenaSeasonRewardUI:ctor()
end

function ArenaSeasonRewardUI:onCover()
end

function ArenaSeasonRewardUI:onReshow()
end

function ArenaSeasonRewardUI:onClose()
	if self.seasonChangeSid then
		self:unscheduleGlobal(self.seasonChangeSid)
		self.seasonChangeSid = nil
	end
	if self.spineGrading then
        self.spineGrading:destroy()
		self.spineGrading = nil
    end
end

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

	self.txTitle = uiMap["arena_season_reward_ui.bg.title.tx_title"]
	self.closeBtn = uiMap["arena_season_reward_ui.bg.close_btn"]
	-- 赛季奖励
	self.seasonReward = uiMap["arena_season_reward_ui.bg.season_reward"]
	self.txDesc = uiMap["arena_season_reward_ui.bg.season_reward.tx_desc"]
	self.txTime = uiMap["arena_season_reward_ui.bg.season_reward.countdown.tx_time"]
	self.seasonRewardComp = uiMap["arena_season_reward_ui.bg.season_reward.reward_list"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	-- 领取奖励
	self.getReward = uiMap["arena_season_reward_ui.bg.last_reward"]
	self.txSeason = uiMap["arena_season_reward_ui.bg.last_reward.tx_season"]
	self.spineRoot = uiMap["arena_season_reward_ui.bg.last_reward.spine_root"]
	self.txGrading = uiMap["arena_season_reward_ui.bg.last_reward.img_grading.tx_grading"]
	self.rewardList = uiMap["arena_season_reward_ui.bg.last_reward.rewards"]
	self.getRewardComp = self.rewardList:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.btnGet = uiMap["arena_season_reward_ui.bg.last_reward.btn_get"]
	self.txGet = uiMap["arena_season_reward_ui.bg.last_reward.btn_get.tx_get"]

	self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_11))

	self.closeBtn:addClickListener(function()
		self:closeUI()
	end)
	self.btnGet:addClickListener(function()
		ModuleManager.ArenaManager:reqLastSeasonReward()
	end)
	self:bind(DataManager.ArenaData, "isDirty", function()
        self:closeUI()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.ARENA_SEASON_END, function()
        self:closeUI()
    end)
end

function ArenaSeasonRewardUI:onRefresh()
	if DataManager.ArenaData:hasSeasonReward() then
		self:showGetReward()
	else
		self:showSeasonReward()
	end
end

-- 显示赛季奖励
function ArenaSeasonRewardUI:showSeasonReward()
	self.seasonReward:setActive(true)
	self.getReward:setActive(false)

	self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_12, DataManager.ArenaData:getSeason()))

	if self.seasonChangeSid then
		self:unscheduleGlobal(self.seasonChangeSid)
	end
	self:refreshCountdown()
	self.seasonChangeSid = self:scheduleGlobal(function()
		self:refreshCountdown()
	end, 1)

	local ids = DataManager.ArenaData:getGradingIdList()
	table.sort(ids, function(a, b) return a > b end)
	self.seasonRewardComp:addInitCallback(function()
		return "app/ui/arena/cell/arena_season_reward_cell"
	end)
	self.seasonRewardComp:addRefreshCallback(function(index, cell)
		cell:refresh(ids[index])
	end)
	self.seasonRewardComp:clearCells()
	self.seasonRewardComp:refillCells(#ids)
end

function ArenaSeasonRewardUI:refreshCountdown()
    self.txTime:setText(Time:formatNumTimeStr(DataManager.ArenaData:getRemainSeasonTime()))
end

-- 显示领取奖励
function ArenaSeasonRewardUI:showGetReward()
	self.seasonReward:setActive(false)
	self.getReward:setActive(true)

	local lastId = DataManager.ArenaData:getLastGradingId()
	
	self.txSeason:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_13, DataManager.ArenaData:getSeason() - 1))
	self.txGrading:setText(DataManager.ArenaData:getGradingName(lastId))
	self.txGet:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
	if self.spineGrading then
        self.spineGrading:destroy()
		self.spineGrading = nil
    end
	SpineManager:loadUISpineWidgetAsync(DataManager.ArenaData:getGradingIconName(lastId).."_spine", self.spineRoot, function(spineObject)
		self.spineGrading = spineObject
		self.spineGrading:setSkin(DataManager.ArenaData:getGradingNumName(lastId))
		self.spineGrading:playAnimComplete("born", false, false, function()
			self.spineGrading:playAnim("idle", true, false)
		end)
	end)

	self.btnGet:addRedPoint(110, 35, 0.6)

	local rewards = DataManager.ArenaData:getSeasonRewards(lastId)
	local rewardCount = #rewards
    self.getRewardComp:addInitCallback(function()
		return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
	end)
	self.getRewardComp:addRefreshCallback(function(index, cell)
		cell:refreshByConfig(rewards[index])
	end)
	self.getRewardComp:clearCells()

	if rewardCount > MAX_SCROLL_SHOW_COUNT then
        local comp = self.rewardList:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SCROLL_RECT)
		comp.movementType = CS.UnityEngine.UI.ScrollRect.MovementType.Elastic
        self.getRewardComp:setPerLineNum(5)
        self.rewardList:setSizeDeltaX(560)
	else
        local comp = self.rewardList:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SCROLL_RECT)
        comp.movementType = CS.UnityEngine.UI.ScrollRect.MovementType.Clamped
        if rewardCount >= 5 then
            self.getRewardComp:setPerLineNum(5)
            self.rewardList:setSizeDeltaX(560)
        elseif rewardCount <= 0 then
            self.getRewardComp:setPerLineNum(1)
            self.rewardList:setSizeDeltaX(560)
        else
            self.getRewardComp:setPerLineNum(rewardCount)
            self.rewardList:setSizeDeltaX(112 * rewardCount)
        end
	end
    self.getRewardComp:refillCells(rewardCount)
end

return ArenaSeasonRewardUI