local BuyVitUI = class("BuyVitUI", BaseUI)

function BuyVitUI:isFullScreen()
	return false
end

function BuyVitUI:showCommonBG()
	return false
end

function BuyVitUI:getPrefabPath()
	return "assets/prefabs/ui/commerce/buy_vit_ui.prefab"
end

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

function BuyVitUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["buy_vit_ui.bg.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	uiMap["buy_vit_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.BUY_ENERGY))
	local constCfg = ConfigManager:getConfig("const")
	local diamondReward = constCfg["stamina_diamond_buy"].reward
	local adReward = constCfg["stamina_ad_buy"].reward
	uiMap["buy_vit_ui.bg.cell_1.num_tx_1"]:setText("x" .. GFunc.getRewardNum(diamondReward))
	uiMap["buy_vit_ui.bg.cell_2.num_tx_1"]:setText("x" .. GFunc.getRewardNum(adReward))

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

	self.buyBtn1 = uiMap["buy_vit_ui.bg.cell_1"]
	self.buyBtn2 = uiMap["buy_vit_ui.bg.cell_2"]
	self.buyBtn1:addClickListener(function()
		local diamondCost = constCfg["stamina_diamond_cost"].reward
		if not GFunc.checkCost(GFunc.getRewardId(diamondCost), GFunc.getRewardNum(diamondCost), true, BIReport.ITEM_GET_TYPE.BUY_VIT) then
			return
		end
		ModuleManager.CommerceManager:onBuyVit(false)
	end)
	self.buyBtn2:addClickListener(function()
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_ENERGY, function ()
			ModuleManager.CommerceManager:onBuyVit(true)
		end)
	end)

	self:bind(DataManager.PlayerData, "vitGemCount", function()
		self:onRefresh()
	end)
	self:bind(DataManager.PlayerData, "vitADCount", function()
		self:onRefresh()
	end)
end

function BuyVitUI:onRefresh()
	local gemBuyCount = DataManager.PlayerData:getVitGemBuyCount()
	local adBuyCount = DataManager.PlayerData:getVitAdBuyCount()
	local maxGemBuy = GFunc.getConstIntValue("stamina_diamond_times")
	local maxAdBuy = GFunc.getConstIntValue("stamina_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["stamina_diamond_cost"].reward
	self.numTx1:setText(tostring(GFunc.getRewardNum(diamondCost)))
	self.numTx2:setText((maxAdBuy - adBuyCount) .. "/" .. GFunc.getConstIntValue("stamina_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 BuyVitUI