local SummonRewardUI = class("SummonRewardUI", BaseUI)
local DG = CS.DG
local DOTween = DG.Tweening.DOTween

local REWARD_CELL = "app/ui/summon/cell/summon_reward_cell"

function SummonRewardUI:isFullScreen()
	return false
end

function SummonRewardUI:ctor(params)
	params = params or {}
	self.rewards = params.rewards or {}
	self.isWish = params.isWish or false
	self.callback = params.callback
	self.tweenEnd = params.tweenEnd
	self.unlockList = params.newForce
	self.page = params.page
end

function SummonRewardUI:getPrefabPath()
	return "assets/prefabs/ui/summon/summon_reward_ui.prefab"
end

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

	self.btnGet = uiMap["summon_reward_ui.bg.btn_get"]
	self.btnGet:addClickListener(function()
		self:closeUI()
	end)
	self.goOne = uiMap["summon_reward_ui.bg.one"]
	self.rewardOneCell = uiMap["summon_reward_ui.bg.one.cell_node.summon_reward_cell"]:addLuaComponent(REWARD_CELL)
	self:setCellEffectOrder(self.rewardOneCell )
	self.goTen = uiMap["summon_reward_ui.bg.ten"]
	self.rewardTenCells = {}
	for i = 1, 10 do
		local cell = uiMap["summon_reward_ui.bg.ten.cell_node_" .. i .. ".summon_reward_cell"]:addLuaComponent(REWARD_CELL)
		table.insert(self.rewardTenCells, cell)
		self:setCellEffectOrder(cell)
	end
	self.tranBg = uiMap["summon_reward_ui.bg"]
	self.imgBg = uiMap["summon_reward_ui.bg"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE)
	self.imgBg.color = BF.Color(0, 0, 0, 0)
	CS.DG.Tweening.DOTween.To(
		function()
			return 0
		end,
		function(x)
			self.imgBg.color = BF.Color(0, 0, 0, x)
		end,
	0.98, 0.3)
	
	self.btnSummonTen = uiMap["summon_reward_ui.bg.btn_summon_ten"]
	self.txTen = uiMap["summon_reward_ui.bg.btn_summon_ten.tx_desc_ten"]
	self.txNumTen = uiMap["summon_reward_ui.bg.btn_summon_ten.cost.tx_num_ten"]
	self.imgSummonTenIcon = uiMap["summon_reward_ui.bg.btn_summon_ten.cost.img_icon_1"]

	self.btnSummonOne = uiMap["summon_reward_ui.bg.btn_summon_one"]
	self.txOne = uiMap["summon_reward_ui.bg.btn_summon_one.tx_desc_one"]
	self.txNumOne = uiMap["summon_reward_ui.bg.btn_summon_one.cost.tx_num_one"]
	self.imgSummonOneIcon = uiMap["summon_reward_ui.bg.btn_summon_one.cost.img_icon_1"]
	self.btnSummonFree = uiMap["summon_reward_ui.bg.btn_summon_free"]
	self.btnSummonFreeTx = uiMap["summon_reward_ui.bg.btn_summon_free.text_free"]
	self.btnSummonAd = uiMap["summon_reward_ui.bg.btn_summon_ad"]
	self.btnSummonAdTx = uiMap["summon_reward_ui.bg.btn_summon_ad.desc_tx"]

	self.txTen:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_TEN))
	self.txOne:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_ONE))
	self.btnSummonFreeTx:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC_1))
	self.btnSummonAdTx:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_1))

	self.btnSummonTen:addClickListener(function()
		self:closeUI()
		self.callback(10)
	end)
	self.btnSummonFree:addClickListener(function()
		self:closeUI()
		self.callback(1)
	end)
	self.btnSummonOne:addClickListener(function()
		self:closeUI()
		self.callback(1)
	end)
	self.btnSummonAd:addClickListener(function()
		self:closeUI()
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.SUMMON_AD, function()
			self.callback(1, true)
		end)
	end)
end
function SummonRewardUI:setCellEffectOrder(cell)
	local effects = cell:getEffectQlts()
	for j, effect in pairs(effects) do
		-- self:addEffect(effect, nil, 10)
		effect:setSortingOrder(self:getUIOrder(), 10)
	end
end
function SummonRewardUI:onClose()
	if self.tweenEnd ~= nil then
		self.tweenEnd()
	end
end

function SummonRewardUI:refreshSummonBtn()
	local freeCount = DataManager.SummonData:getSummonFreeCount(self.page)
	local adCount = DataManager.SummonData:getSummonAdCount(self.page)
	local itemCost = DataManager.SummonData:getSummonItemCost(self.page)
	local costId = GFunc.getRewardId(itemCost)
	local costNum = GFunc.getRewardNum(itemCost)
	local count = DataManager.BagData.ItemData:getItemNumById(costId)
	local obj = DataManager.BagData.ItemData:getItemById(costId)
	local cost = DataManager.SummonData:getSummonGemCost(self.page)
	self.btnSummonAd:setActive(adCount > 0)
	self.btnSummonFree:setActive(freeCount > 0)
	self.btnSummonOne:setActive(freeCount <= 0)
	self.btnSummonTen:setActive(true)
	if freeCount <= 0 then
		if count >= 1 then
			self.txNumOne:setText(1)
			self.btnSummonOne:addRedPoint(100, 30, 1)
		else
			self.btnSummonOne:removeRedPoint()
			if cost then
				local costGemNum = GFunc.getRewardNum(cost)
				if count == 0 and GFunc.checkCost(GConst.ItemConst.ITEM_ID_GEM, costGemNum, false) then
					self.txNumOne:setText(costGemNum)
					obj = DataManager.BagData.ItemData:getItemById(GConst.ItemConst.ITEM_ID_GEM)
				else
					self.txNumOne:setText("<color=#EC3636>1</color>")
				end  
			else
				self.txNumOne:setText("<color=#EC3636>1</color>")
			end
		end
		self.imgSummonOneIcon:setSprite(obj:getIconRes())
	end

	obj = DataManager.BagData.ItemData:getItemById(costId)
	if count >= 10 then
		self.txNumTen:setText(10)
		self.btnSummonTen:addRedPoint(100, 30, 1)
	else
		self.btnSummonTen:removeRedPoint()
		if cost then
			local costGemNum = GFunc.getRewardNum(cost) * 10
			if count == 0 and GFunc.checkCost(GConst.ItemConst.ITEM_ID_GEM, costGemNum, false) then
				self.txNumTen:setText(costGemNum)
				obj = DataManager.BagData.ItemData:getItemById(GConst.ItemConst.ITEM_ID_GEM)
			else
				self.txNumTen:setText("<color=#EC3636>10</color>")
			end
		else
			self.txNumTen:setText("<color=#EC3636>10</color>")
		end
	end
	self.imgSummonTenIcon:setSprite(obj:getIconRes())
end

--@region 主界面刷新
function SummonRewardUI:onRefresh()
	local offsetTime = 0
	self.btnGet:setActive(false)
	self.btnSummonOne:setActive(false)
	self.btnSummonTen:setActive(false)
	self.btnSummonAd:setActive(false)
	self.btnSummonFree:setActive(false)
	self.tranBg:removeClickListener()
	if #self.rewards == 1 then
		self.goOne:setActive(true)
		self.goTen:setActive(false)
		self:setCellEffectOrder(self.rewardOneCell)

		self.rewardOneCell:refresh(self.rewards[1], 0, self)
		local qlt = DataManager.HeroData:getHeroQlt(self.rewards[1].item.id)
		if qlt == 4 or qlt == 5 then
			offsetTime = offsetTime + 0.5
			self:doUITweenShake(0.7)
		end
		CS.DG.Tweening.DOVirtual.DelayedCall(0.3 + offsetTime, function()
			self:checkUnlockMap()
			self.btnGet:setActive(true)
			if not self.isWish then
				self:refreshSummonBtn()
			end
			self.tranBg:addClickListener(function()
				self:closeUI()
			end)
			self.tranBg:setClickAnimation(false)
		end)
	else
		self.goOne:setActive(false)
		self.goTen:setActive(true)
		for i, cell in pairs(self.rewardTenCells) do
			self:setCellEffectOrder(cell)
		end
		for i = 1, 10 do
			if self.rewards[i] then
				local delayTime = (i - 1) * 0.06 + offsetTime
				local qlt = DataManager.HeroData:getHeroQlt(self.rewards[i].item.id)
				if qlt == 4 or qlt == 5 then
					offsetTime = offsetTime + 1
				end
				self.rewardTenCells[i]:getBaseObject():setActive(false)
				self:performWithDelayGlobal(function()
					self.rewardTenCells[i]:refresh(self.rewards[i], i == 3 or i == 7, self)
					if qlt == 4 or qlt == 5 then
						self:doUITweenShake(0.3)
					end
				end, delayTime)
			end
		end
		CS.DG.Tweening.DOVirtual.DelayedCall(1 + offsetTime, function()
			self:checkUnlockMap()
			self.btnGet:setActive(true)
			if not self.isWish then
				self:refreshSummonBtn()
			end
			self.tranBg:addClickListener(function()
				self:closeUI()
			end)
			self.tranBg:setClickAnimation(false)
		end)
	end
end

function SummonRewardUI:doUITweenShake(delayTime)
	if self.shakeSeq then
		self.shakeSeq:Kill()
	end
	local duration = 1  -- 震动持续时间（秒）
	local strength = BF.Vector2(20, 20)  -- 震动强度
	local vibrato = 60  -- 震动频率
	local randomness = 90  -- 震动随机性
	local fadeOut = true  -- 是否在结束时减弱震动
	self.shakeSeq = DOTweenManager:createSeqWithIntId()
	self.shakeSeq:AppendInterval(delayTime)
	self.shakeSeq:Append(self.tranBg:getTransform():DOShakeAnchorPos(duration, strength,vibrato, randomness, fadeOut))
end
function SummonRewardUI:checkUnlockMap()
	if self.unlockList == nil then
		return
	end
	if #self.unlockList <= 0 then
		return
	end
	if #self.unlockList > 0 then
		ModuleManager.ForceManager:showForceUnlockUI(self.unlockList)
	end
end

return SummonRewardUI
