-- 竞技场：购买入场券
local ArenaBuyTicketUI = class("ArenaBuyTicketUI", BaseUI)

function ArenaBuyTicketUI:isFullScreen()
	return false
end

function ArenaBuyTicketUI:showCommonBG()
	return false
end

function ArenaBuyTicketUI:getPrefabPath()
	return "assets/prefabs/ui/arena/arena_buy_ticket_ui.prefab"
end

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

function ArenaBuyTicketUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["arena_buy_ticket_ui.bg.btn_close"]:addClickListener(function()
		self:closeUI()
	end)

	uiMap["arena_buy_ticket_ui.bg.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_32))
	local constCfg = ConfigManager:getConfig("const")
	local diamondReward = constCfg["arena_notes_diamond_buy"].reward
	local adReward = constCfg["arena_notes_ad_buy"].reward
	uiMap["arena_buy_ticket_ui.bg.cell_1.num_tx_1"]:setText("x" .. GFunc.getRewardNum(diamondReward))
	uiMap["arena_buy_ticket_ui.bg.cell_2.num_tx_1"]:setText("x" .. GFunc.getRewardNum(adReward))

	self.descTx1 = uiMap["arena_buy_ticket_ui.bg.cell_1.desc_tx"]
	self.descTx2 = uiMap["arena_buy_ticket_ui.bg.cell_2.desc_tx"]
	self.numTx1 = uiMap["arena_buy_ticket_ui.bg.cell_1.num_tx_2"]
	self.numTx2 = uiMap["arena_buy_ticket_ui.bg.cell_2.num_tx_2"]
	self.checkImg1 = uiMap["arena_buy_ticket_ui.bg.cell_1.check_img"]
	self.checkImg2 = uiMap["arena_buy_ticket_ui.bg.cell_2.check_img"]
	self.iconImg1 = uiMap["arena_buy_ticket_ui.bg.cell_1.icon_img"]
	self.iconImg2 = uiMap["arena_buy_ticket_ui.bg.cell_2.icon_img"]

	self.buyBtn1 = uiMap["arena_buy_ticket_ui.bg.cell_1"]
	self.buyBtn2 = uiMap["arena_buy_ticket_ui.bg.cell_2"]
	self.buyBtn1:addClickListener(function()
		local diamondCost = constCfg["arena_notes_diamond_cost"].reward
		if not GFunc.checkCost(GFunc.getRewardId(diamondCost), GFunc.getRewardNum(diamondCost), true) then
			return
		end
		ModuleManager.CommerceManager:onBuyArenaTicket(false)
	end)
	self.buyBtn2:addClickListener(function()
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.ARENA_TICKET, function ()
			ModuleManager.CommerceManager:onBuyArenaTicket(true)
		end)
	end)

	self:bind(DataManager.PlayerData, "arenaTicketGemCount", function()
		self:onRefresh()
	end)
	self:bind(DataManager.PlayerData, "arenaTicketADCount", function()
		self:onRefresh()
	end)
end

function ArenaBuyTicketUI:onRefresh()
	local gemBuyCount = DataManager.PlayerData:getArenaTicketGemBuyCount()
	local adBuyCount = DataManager.PlayerData:getArenaTicketAdBuyCount()
	local maxGemBuy = GFunc.getConstIntValue("arena_notes_diamond_times")
	local maxAdBuy = GFunc.getConstIntValue("arena_notes_ad_times")
	if gemBuyCount < maxGemBuy then
		self.buyBtn1:setTouchEnable(true)
		self.checkImg1:setVisible(false)
		self.iconImg1:setVisible(true)
		self.numTx1:setVisible(true)
	else
		self.buyBtn1:setTouchEnable(false)
		self.checkImg1:setVisible(true)
		self.iconImg1:setVisible(false)
		self.numTx1:setVisible(false)
	end

	GFunc.setAdsSprite(self.iconImg2, false)
	if adBuyCount < maxAdBuy then
		self.buyBtn2:setTouchEnable(true)
		self.checkImg2:setVisible(false)
		self.iconImg2:setVisible(true)
		self.numTx2:setVisible(true)
	else
		self.buyBtn2:setTouchEnable(false)
		self.checkImg2:setVisible(true)
		self.iconImg2:setVisible(false)
		self.numTx2:setVisible(false)
	end

	self.descTx1:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, maxGemBuy - gemBuyCount))
	self.descTx2:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, maxAdBuy - adBuyCount))

	local constCfg = ConfigManager:getConfig("const")
	local diamondCost = constCfg["arena_notes_diamond_cost"].reward
	self.numTx1:setText(tostring(GFunc.getRewardNum(diamondCost)))
	self.numTx2:setText((maxAdBuy - adBuyCount) .. "/" .. GFunc.getConstIntValue("arena_notes_ad_times"))
	if not self.adjustIconAndNum then
		self.adjustIconAndNum = true
		GFunc.centerImgAndTx(self.iconImg1, self.numTx1)
		GFunc.centerImgAndTx(self.iconImg2, self.numTx2)
	end
end

return ArenaBuyTicketUI