local BattleReviveUI = class("BattleReviveUI", BaseUI)

function BattleReviveUI:isFullScreen()
	return false
end

function BattleReviveUI:ctor(params)
	params = params or {}
	self.callback = params.callback
	self.battleType = params.battleType
end

function BattleReviveUI:getPrefabPath()
	return "assets/prefabs/ui/battle/battle_revive_ui.prefab"
end

function BattleReviveUI:onLoadRootComplete()
	self:_display()
	self:_addListeners()
end

function BattleReviveUI:_display()
	local uiMap = self.root:genAllChildren()
	self.title1Tx = uiMap["battle_revive_ui.bg.title_text"]
	self.descTx = uiMap["battle_revive_ui.bg.tx_desc"]

	self.costBtn = uiMap["battle_revive_ui.bg.cost_btn"]
	self.costTx = uiMap["battle_revive_ui.bg.cost_btn.tx"]
	self.adBtn = uiMap["battle_revive_ui.bg.ad_btn"]
	self.adIcon = uiMap["battle_revive_ui.bg.ad_btn.icon"]
	self.adTx = uiMap["battle_revive_ui.bg.ad_btn.tx"]
	self.adLimitTx = uiMap["battle_revive_ui.bg.limit_tx"]
	self.closeBtn = uiMap["battle_revive_ui.bg.close_btn"]
	self.cancelBtn = uiMap["battle_revive_ui.bg.cancel_btn"]
	self.cancelTx = uiMap["battle_revive_ui.bg.cancel_btn.tx"]

	self.title1Tx:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_7))
	self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_9))
	-- self.title2Tx:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_8))
	if self.battleType == GConst.BattleConst.BATTLE_TYPE.STAGE then
		self.adLimitTx:setText(DataManager.ChapterData:getReReviveCount() .. "/" .. GFunc.getConstIntValue("ads_revive_limit"))
	elseif self.battleType == GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE then
		self.adLimitTx:setText(DataManager.DailyChallengeData:getReReviveCount() .. "/" .. GFunc.getConstIntValue("daily_challenge_ads_revive_limit"))
	end

	self.adTx:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
	self.cancelTx:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_10))

	GFunc.setAdsSprite(self.adIcon)
	local cost = GFunc.getConstReward("cost_revive")
	local costNum = cost.num
	local costEnough = GFunc.checkCost(cost.id, costNum, false)
	self.costTx:setText(costEnough and costNum or ("<color=#FF0000>".. costNum.. "</color>"))

	-- self.spineObj = uiMap["battle_revive_ui.ui_spine_obj"]
	-- self.spineObj:playAnimComplete("born", false, true, function()
	-- 	self.spineObj:playAnim("idle", true, true)
	-- end)

	-- self.fx = uiMap["battle_revive_ui.vfx_b13_ui_revive_b01"]
	-- self.fx:setActive(false)

	-- 根据复活次数更新按钮位置
	local canAdRevive = false
	if self.battleType == GConst.BattleConst.BATTLE_TYPE.STAGE then
		canAdRevive = DataManager.ChapterData:canAdRevive()
	elseif self.battleType == GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE then
		canAdRevive = DataManager.DailyChallengeData:canAdRevive()
	end
	if not canAdRevive then
		self.costBtn:setActive(true)
		self.adBtn:setActive(false)
		self.adLimitTx:setActive(false)
		self.costBtn:setAnchoredPositionX(0)
	else
		self.costBtn:setActive(true)
		self.adBtn:setActive(true)
		self.adLimitTx:setActive(true)
		self.costBtn:setAnchoredPositionX(-144)
	end
	
	-- 免广告卡模块
	local noAdNode = uiMap["battle_revive_ui.no_ad_node"]
	local noAdTitleTx = uiMap["battle_revive_ui.no_ad_node.tx_ad"]
	local noAdDescTx = uiMap["battle_revive_ui.no_ad_node.tx_desc"]
	local noAdBuyBtn = uiMap["battle_revive_ui.no_ad_node.btn_buy"]
	local noAdBuyTx = uiMap["battle_revive_ui.no_ad_node.btn_buy.tx_desc"]

	if DataManager.PrivilegeCardData:getIsAdCardOpen() and not DataManager.PrivilegeCardData:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD) then
		noAdNode:setActive(true)
		noAdTitleTx:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_15))
		noAdDescTx:setText(I18N:getGlobalText(I18N.GlobalConst.ADS_DESC_16))
		noAdBuyBtn:addClickListener(function()
			ModuleManager.PrivilegeCardManager:buyAdCard()
		end)
		noAdBuyTx:setText(DataManager.PlayerData:getNoAdPrice())
	else
		noAdNode:setActive(false)
	end
end

function BattleReviveUI:_addListeners()
	self.costBtn:addClickListener(function()
		local cost
		if self.battleType == GConst.BattleConst.BATTLE_TYPE.STAGE then
			cost = GFunc.getConstReward("cost_revive")
		elseif self.battleType == GConst.BattleConst.BATTLE_TYPE.DAILY_CHALLENGE then
			cost = GFunc.getConstReward("daily_challenge_cost_revive")
		end
		if not cost then
			return
		end
		if GFunc.checkCost(GConst.ItemConst.ITEM_ID_GEM, cost.num, true) then
			self:doRevive(false)
		end
	end)
	self.adBtn:addClickListener(function()
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.BATTLE_REVIVE, function()
			self:doRevive(true)
		end, true)
	end)
	self.cancelBtn:addClickListener(function()
		self:cancelRevive()
	end)
	self.closeBtn:addClickListener(function()
		self:cancelRevive()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.BATTLE_REVIVE, function()
		self:onRevive()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.BATTLE_REVIVE_FAILED, function()
		self:onReviveFailed()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.NO_AD_ACTIVE, function()
		self:_display()
	end)
end

function BattleReviveUI:doRevive(isAd)
	ModuleManager.BattleManager:reqFightRevive(isAd)
end

function BattleReviveUI:onRevive()
	self:closeUI()
	if self.callback then
		self.callback(true)
	end
end

function BattleReviveUI:onReviveFailed()
	self:closeUI()
	if self.callback then
		self.callback(false)
	end
end

function BattleReviveUI:cancelRevive()
	self:closeUI()
	if self.callback then
		self.callback(false)
	end
end

return BattleReviveUI