local BaseObject = require "app/bf/unity/base_object"

local EffectObject = class("EffectObject", BaseObject)

local BF_EFFECT_HELPER = GConst.TYPEOF_UNITY_CLASS.BF_EFFECT_HELPER
local abs = math.abs

local DEFAULT_DELAY_TIME = 1

function EffectObject:ctor()
	self.timeScale = 1
end

function EffectObject:initWithPrefab(assetPath, prefab)
	BaseObject.initWithPrefab(self, assetPath, prefab)
	self.effectHelper = self:getComponent(BF_EFFECT_HELPER)
	if self.effectHelper == nil then
		self.effectHelper = self:addComponent(BF_EFFECT_HELPER)
	end
	local animatorCount = self.effectHelper.AnimatorCount or 0
	if animatorCount > 0 then
		self.animators = {}
		local animators = prefab:GetComponentsInChildren(GConst.TYPEOF_UNITY_CLASS.ANIMATOR, true)
		if animators then
			local len = animators.Length
			for i = 0, len - 1 do
				table.insert(self.animators, animators[i])
			end
		end
	end
end

function EffectObject:play()
    self.effectHelper:Play()
end

function EffectObject:playComplete(complete, customTime, customCallback)
    self.effectHelper:Play()
	local duration = self:getDuration()
	if self.playCompleteSeq then
		self.playCompleteSeq:Kill()
		self.playCompleteSeq = nil
	end
	self.playCompleteSeq = self:createBindTweenSequence()
	if customTime then
		self.playCompleteSeq:AppendInterval(customTime)
		self.playCompleteSeq:AppendCallback(customCallback)
		duration = duration - customTime
		if duration <= 0 then
			duration = 0.01
		end
	end
	self.playCompleteSeq:AppendInterval(duration)
	self.playCompleteSeq:AppendCallback(complete)
end

-- 延迟播放
function EffectObject:playDelay(delayTime, hideBeforePlay)
	if delayTime <= 0 then
		self:play()
		return
	end
	if self._effectPlayScheduleId then
		self:unscheduleGlobal(self._effectPlayScheduleId)
	end

	if hideBeforePlay then
		self:setActive(false)
	end
	self._effectPlayScheduleId = self:performWithDelayGlobal(function()
		self._effectPlayScheduleId = nil
		if hideBeforePlay then
			self:setActive(true)
		end
		self:play()
	end, delayTime)

	return self._effectPlayScheduleId
end

function EffectObject:clear()
	if self._effectPlayScheduleId then
		self:unscheduleGlobal(self._effectPlayScheduleId)
		self._effectPlayScheduleId = nil
	end
	self.effectHelper:Clear()
end

function EffectObject:stop()
	if self._effectPlayScheduleId then
		self:unscheduleGlobal(self._effectPlayScheduleId)
		self._effectPlayScheduleId = nil
	end
	self.effectHelper:Stop()
end

function EffectObject:stopAndClear()
	if self._effectPlayScheduleId then
		self:unscheduleGlobal(self._effectPlayScheduleId)
		self._effectPlayScheduleId = nil
	end
	self.effectHelper:StopAndClear()
end


function EffectObject:pause()
	if self._effectPlayScheduleId then
		self:unscheduleGlobal(self._effectPlayScheduleId)
		self._effectPlayScheduleId = nil
	end
	self.effectHelper:Pause()
end

function EffectObject:setUIOrder(uiOrder)
	self.effectHelper:SetUIOrder(uiOrder)
end

function EffectObject:setSortingOrder(uiOrder, order)
	self.effectHelper:SetSortingOrder(uiOrder, order)
end

function EffectObject:changeSortingOrderToFudge(order, coefficient)
	self.effectHelper:ChangeSortingOrderToFudge(order, coefficient)
end

function EffectObject:initParticleSystemRendererList()
	self.effectHelper:InitParticleSystemRendererList()
end

function EffectObject:getDuration()
	return self.effectHelper.EffectDuration
end

function EffectObject:setTimeScale(timeScale)
	if abs(self.timeScale - timeScale) < 0.00001 then
		return
	end
	self.timeScale = timeScale
	self.effectHelper:SetTimeScale(timeScale)
	if self.animators then
		for k, v in ipairs(self.animators) do
			v.speed = timeScale
		end
	end
end

function EffectObject:setIsLoop(isLoop)
	self.isLoop = isLoop
end

function EffectObject:getIsLoop()
	return self.isLoop
end

function EffectObject:performDurationDelay(callback, diffDuration)
	if self.scheduleId then
		self:unscheduleGlobal(self.scheduleId)
	end

	local duration = self.effectHelper.EffectDuration
	if duration <= 0 then
		duration = DEFAULT_DELAY_TIME
	end

	duration = duration + (diffDuration or 0)
	duration = math.max(duration, 0)
	self.scheduleId = self:performWithDelayGlobal(function()
		self.scheduleId = nil
		callback()
	end, duration)

	return self.scheduleId
end

function EffectObject:setEulerAngles(x, y, z)
	if self.effectHelper then
		self.effectHelper:SetEulerAngles(x, y, z)
	else
		BaseObject.setEulerAngles(self, x, y, z)
	end
end

function EffectObject:setLocalEulerAngles(x, y, z)
	if self.effectHelper then
		self.effectHelper:SetLocalEulerAngles(x, y, z)
	else
		BaseObject.setLocalEulerAngles(self, x, y, z)
	end
end

function EffectObject:setLocalPosition(x, y, z)
	if self.effectHelper then
		self.effectHelper:SetLocalPosition(x, y, z)
	else
		BaseObject.setLocalPosition(self, x, y, z)
	end
end

function EffectObject:setPosition(x, y, z)
	if self.effectHelper then
		self.effectHelper:SetPosition(x, y, z)
	else
		BaseObject.setPosition(self, x, y, z)
	end
end

function EffectObject:setLocalScale(x, y, z)
	if self.effectHelper then
		self.effectHelper:SetLocalScale(x, y, z)
	else
		BaseObject.setLocalScale(self, x, y, z)
	end
end

function EffectObject:getIsEulerAnglesFlip()
	if self.effectHelper then
		return self.effectHelper.EulerAnglesFlip
	end
	return false
end

function EffectObject:setBattleScheduleId(id)
	self.battleScheduleId = id
end

function EffectObject:getBattleScheduleId()
	return self.battleScheduleId
end

function EffectObject:onDestroy()
	if self.scheduleId then
		self:unscheduleGlobal(self.scheduleId)
		self.scheduleId = nil
	end
	if self._effectPlayScheduleId then
		self:unscheduleGlobal(self._effectPlayScheduleId)
		self._effectPlayScheduleId = nil
	end
	BaseObject.onDestroy(self)
	self.effectHelper = nil
	self.animators = nil
end

return EffectObject