local PowerToastManager = class("PowerToastManager", BaseModule)

function PowerToastManager:ctor()
end

function PowerToastManager:clear()
	self:clearSeq()
end

function PowerToastManager:hideToast()
	self:showEffect(false)
	UIManager:hidePowerToast()
end

function PowerToastManager:showToast(before, after, posY)
	self.before = before
	self.after = after
	self.posY = posY or 243
	self.power = self.before
	self.diffVal = self.after - self.before
	self:clearSeq()
	self.isNew = true
	UIManager:getPowerToast(function(prefabObject)
		prefabObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS).enabled = true

		local uiMap = prefabObject:genAllChildren()
		self.bg = uiMap["power_change_toast.bg"]
		self.powerTx = uiMap["power_change_toast.bg.power_tx"]
		self.powerChange = uiMap["power_change_toast.bg.power_change_tx"]
		-- self.powerSpine = uiMap["power_change_toast.bg.ui_spine_obj"]
		-- self.effect1 = uiMap["power_change_toast.content.vfx_ui_zhanli_b01"]
		-- self.effect2 = uiMap["power_change_toast.content.vfx_ui_zhanli_b03"]

		self:showEffect(false)
		self:showModuleUnlockAppearAnim()
		-- self.powerSpine:playAnim("idle", false, true)
	end)
end

-- 出现
function PowerToastManager:showModuleUnlockAppearAnim()
	self.bg:setAnchoredPositionY(self.posY)
	self.bg:setLocalScale(1, 1, 1)

	-- if self.animEffect == nil then
	-- 	self.animEffect = self.effect1:createBindTweenSequence()
	-- 	self.animEffect:AppendInterval(0.3)
	-- 	self.animEffect:AppendCallback(function()
	-- 		self:showEffect(true)
	-- 	end)
	-- 	self.animEffect:AppendInterval(1)
	-- 	self.animEffect:AppendCallback(function()
	-- 		self:showEffect(false)
	-- 	end)
	-- else
	-- 	self.animEffect:Restart()
	-- end

	if self.animAppear == nil then
		self.animAppear = self.bg:createBindTweenSequence()
		local tween = DOTweenManager:createDOTweenTo(
			function()
				return self.power
			end,
			function(value)
				local str = GFunc.num2Str3(math.floor(value))
				local str2 = GConst.EMPTY_STRING
				local color = GConst.COLOR_CODE.LIGHT_GREEN
				if self.diffVal > 0 then
					str2 = "+" .. GFunc.num2Str3(self.diffVal)
				else
					str2 = "-" .. GFunc.num2Str3(math.abs(self.diffVal))
					color = "#FF77D7"
				end
				self.powerTx:setText(str)
				self.powerChange:setText(GFunc.colorText(color, str2))
			end,
			self.after, 1.5)
		self.animAppear:Append(tween)
		-- self.animAppear:Insert(1.45, self.bg:getTransform():DOScale(1.15, 0.2):SetLoops(2, CS.DG.Tweening.LoopType.Yoyo))
		self.animAppear:SetAutoKill(true)
		self.animAppear:OnComplete(function()
			self.isNew = false
			self:performWithDelayGlobal(function()
				if self.isNew then
					return
				end
				UIManager:hidePowerToast()
			end, 0.5)
		end)
	else
		self.animAppear:Restart()
	end
end

function PowerToastManager:clearSeq()
	if self.animEffect then
		self.animEffect:Kill()
		self.animEffect = nil
	end
	if self.animAppear then
		self.animAppear:Kill()
		self.animAppear = nil
	end
end

function PowerToastManager:showEffect(show)
	if self.effect1 then
		self.effect1:setActive(false)
	end
	if self.effect2 then
		self.effect2:setActive(false)
	end
	if not show then
		return
	end
	if self.effect1 == nil or self.effect2 == nil then
		return
	end

	self.effect1:setActive(true)
	self.effect2:setActive(true)
	self.effect1:play()
	self.effect2:play()
	self.effect1:setSortingOrder(28400, 0)
	self.effect2:setSortingOrder(28400, 20)
end

return PowerToastManager