local BattleBoxOpenUI = class("BattleBoxOpenUI", BaseUI)

function BattleBoxOpenUI:isFullScreen()
	return false
end

function BattleBoxOpenUI:getPrefabPath()
	return "assets/prefabs/ui/battle/battle_box_open_ui.prefab"
end

function BattleBoxOpenUI:ctor(params)
	self.rewards = params.rewards
	self.callback = params.callback
end

function BattleBoxOpenUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	if not self.rewardCell then
		self.rewardCell = CellManager:addCellComp(uiMap["battle_box_open_ui.title_bg.reward_cell"], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end

	self.adBtn = uiMap["battle_box_open_ui.ad_btn"]
	self.adBtnImg = uiMap["battle_box_open_ui.ad_btn.ad"]
	self.adBtnTx = uiMap["battle_box_open_ui.ad_btn.tx"]
	self.okBtn = uiMap["battle_box_open_ui.ok_btn"]
	self.okBtnTx = uiMap["battle_box_open_ui.ok_btn.tx"]
	self.adBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_20))
	self.okBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK))

	local reward = self.rewards
	if reward then
		self.rewardCell:refreshByConfig(reward)
	end

	self.aniOver = false
	uiMap["battle_box_open_ui.mask"]:addClickListener(function()
		if not self.aniOver then
			return
		end
		self:closeUI()
		if self.callback then
			self.callback()
		end
	end)
	self.okBtn:addClickListener(function()
		self:closeUI()
		if self.callback then
			self.callback()
		end
	end)

	if self.seqAni then
		self.seqAni:Kill()
		self.seqAni = nil
	end

	AudioManager:playEffect(AudioManager.EFFECT_ID.BATTLE_BOX_OPEN)
	local canvasGroup = uiMap["battle_box_open_ui.title_bg"]:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
	canvasGroup.alpha = 0
	uiMap["battle_box_open_ui.title_bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_11))
	uiMap["battle_box_open_ui.ui_spine_obj"]:playAnim("born", false, true)
	self.seqAni = self.root:createBindTweenSequence()
	self.seqAni:Insert(0.75, canvasGroup:DOFade(1, 0.5))
	self.seqAni:AppendCallback(function()
		self.aniOver = true
	end)

	self:addEventListener(EventManager.CUSTOM_EVENT.SKILL_REFRESH_SUCC, function(parmas)
		if parmas and parmas.isDoubleBox then
			self:getDouble()
		end
	end)
end

function BattleBoxOpenUI:onRefresh()
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.AD, true) then
		self.adBtn:setActive(false)
		self.okBtn:setAnchoredPositionX(0)
		return
	end
	local battleController = ModuleManager.BattleManager.battleController
	if battleController and battleController.battleData then
		local count = battleController.battleData:getDoubleBoxCount()
		local cfgAdCount = GFunc.getConstIntValue("ads_mystery_box_limit")
		if cfgAdCount > count then
			self.adBtn:setActive(true)
			self.adBtn:setAnchoredPositionX(-100)
			self.okBtn:setAnchoredPositionX(100)
			GFunc.setAdsSprite(self.adBtnImg)
			self.adBtn:addClickListener(function()
				SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.BATTLE_DOUBLE_BOX, function()
					ModuleManager.BattleManager:reqSkillRefresh(nil, nil, true)
				end)
			end)
		else
			self.adBtn:setActive(false)
			self.okBtn:setAnchoredPositionX(0)
		end
	else
		self.adBtn:setActive(false)
		self.okBtn:setAnchoredPositionX(0)
	end
end

function BattleBoxOpenUI:getDouble()
	local battleController = ModuleManager.BattleManager.battleController
	if battleController then
		battleController.doubleMystery = true
	end
	self.rewardCell:showRewardNum(self.rewards.num * 2)
	self.okBtn:setActive(true)
	self.adBtn:setActive(false)
	self.okBtn:setAnchoredPositionX(0)
end

return BattleBoxOpenUI