
.namespace WBPTimer

function OnEnable()
	self.Started = false
end

function Start()
	--OPLog.Log(" wbptimer "..self.Name)

	self.Started = true
end

-- conditions pour décrementer le compteur
--le blockclick ne bloque que les itneractions, pas las évements, c'est moins fort que cutscene donc
function CondDecrement()

	if (GameVariables.IsEqual("CurrentGamePhase","SearchSite") 
		or GameVariables.IsEqual("CurrentGamePhase","Closeup")
		or GameVariables.IsEqual("CurrentGamePhase","Cutscene")
		or GameVariables.IsEqual("CurrentGamePhase","BlockClick")
		or GameVariables.IsEqual("CurrentGamePhase","Tutorial")

		)
	and self.Started then
		return true
	end

	return false
end

-- conditions oour appeler le ontime out qui sont différentes !!
--si ca ne suffit pas, on précisera les conditons en paramétré supplémentaire
function CondTimeOut()
	if (GameVariables.IsEqual("CurrentGamePhase","SearchSite") 
		or GameVariables.IsEqual("CurrentGamePhase","Closeup")
		or GameVariables.IsEqual("CurrentGamePhase","Tutorial")
		or GameVariables.IsEqual("CurrentGamePhase","BlockClick")
		)
    then
		return true
	end
	
	if GameVariables.IsEqual("CurrentGamePhase","Cutscene") or GameVariables.IsEqual("CurrentGamePhase","BlockClick") and self.Type == "Animation" then
		return true
	end
	
	return false
end

function OnUpdate(dt)
	--OPLog.Log("on update "..self.Name)
	--on décrémente "Time"
	if CondDecrement() then			
		if self.Time - dt <= 0 and CondTimeOut() then

			self.Time = 0
			if self.Loop == true then
				--OPLog.Log("fin loop "..self.InitialDelay)
				self.Time = self.InitialDelay
			else
				self.Started = false
			end
			
			if self.AutoDisable == true then
				self.Enabled = false
			end	
			
			if self.UnitNotify == nil then
				OPLog.Log("attention l'unit n'existe plus "..self.Name)
				OPUnitFactory.CloseUnit(self)
				return
			end
			
			if not self.UnitNotify.ParentEnabled then
				OPLog.Log("pas d'unit notify actif sur ce timer")	
				OPUnitFactory.CloseUnit(self)
				return
			end
			
			AssertPopup(self.UnitNotify.OnTimeOut ~= nil , "OnTimeOut non défini !!! "..self.UnitNotify.Name)
			
			self.UnitNotify.OnTimeOut(self.Name)	
			
		end
		
		self.Time = self.Time - dt
	end
end

function OnDisable()
	--necessaire ?
	--OPUnitFactory.CloseUnit(self)
end
