
.namespace CutscenePlayer
-- ------------------------------------------------------------------------
--  On enable handler
-- ------------------------------------------------------------------------
function OnEnable()
    g_counter = 0
	g_lockedspritetab = {}
end

--appel principal
function StartCutscene(cutscenename, unitToNotify, hideCutsceneMask)
	AssertPopup(type(cutscenename) ==  "string", "prob param dans start cutscene")
	
	OPLog.Log("-- on lance la cutscene --"..cutscenename)
	g_oldphase = GetVariables("CurrentGamePhase")
	
	GlobalFunctions.SwitchToCutscene()	
	--SetVariables("CurrentGamePhase","Cutscene")
	
	g_curphase = 1
	g_curscenename = cutscenename
	g_countingevent = true -- indique c'est un event avec compteur ce qui est pas forcément tout le temps le cas
	g_unittonotify = unitToNotify

	LaunchNextStep()
	
	if hideCutsceneMask == nil or hideCutsceneMask == false then
		CutSceneMask.Enabled = true
	end

	GlobalFunctions.DesactiveInterfaceIG()
	
	local g_visibilitycursor = Load('Cursor',"Cursor").Flags_Visible
	local g_activecursor = Load('Cursor',"Cursor").IsActive
	EnableRemotePointerGen( 1, false )
	
	g_lockedspritetab = {}
end

function LaunchNextStep()
	--OPLog.Log("scene "..g_curscenename)
	
	--OPLog.Log(GlobalFunctions.Display(CUTSCENEDESC[g_curscenename]))

	g_cureventisfinished = false -- va indiquer si on passe à la phase suivante
	g_countingevent = false
	g_counter = DEFAULTEVENTTIME --par défaut on met cela
	
	if (CUTSCENEDESC[g_curscenename][g_curphase] == nil) then
		FinishCutscene()
		return
	end
	
	g_curevent = CUTSCENEDESC[g_curscenename][g_curphase]
	
	AssertPopup(type(g_curevent.event) == "number","prob sur l'identifiant d'event")
	
	if (g_curevent.event == ZOOMEVENT) then -- zoom et dézoom
		DisplayManager.SwitchZoomValue( GetCurrentManager() )
		if DisplayManager.IsZoomActive() then
			Enable(ScrollingManager)
			ScrollingManager.SetGlobalPos(g_curevent.x,g_curevent.y, g_curevent.rotx, g_curevent.roty)
		else
			Disable(ScrollingManager)
		end

		
		g_countingevent = true
		
	elseif g_curevent.event == DIAGEVENT then --dialogue
		Enable("SequencePlayer","Unit").PlaySequence(g_curevent.name,self)
	
	elseif g_curevent.event == FADEOUTEVENT then
		Disable(FadeIn)
		Enable(FadeOut)
		FadeOut.Period = g_curevent.length
		FadeOut.UnitNotify = self
		FadeOut.Start(self)
		
	elseif g_curevent.event == FADEINEVENT then -- fadein
		Disable(FadeOut)
		Enable(FadeIn)
		FadeIn.Period = g_curevent.length
		FadeIn.UnitNotify = self
		FadeIn.Start(self)
			
	elseif g_curevent.event == SWITCHEVENT then
		NavigationManager.SwitchTo(nil,g_curevent.dest,self)
		g_oldphase = OPUnitFactory.GetUnit(g_curevent.dest).ClassName
		
	elseif g_curevent.event == PLAYVIDEO then
		GlobalFunctions.PlayVideo( g_curevent.name, self )
	
	elseif g_curevent.event == PLAYANIMEEVENT then
		Enable(g_curevent.unit, "Animation").Play(self)
		g_lockedspritetab[g_curevent.unit] = true
	
	elseif g_curevent.event == SHOWSPRITEEVENT  then
		Show(OPUnitFactory.GetUnit(g_curevent.unit))
		g_countingevent = true
		g_lockedspritetab[g_curevent.unit] = true
		if g_curevent.length ~= nil then
			g_counter = g_curevent.length * 60
		end
		
	elseif g_curevent.event == HIDESPRITEEVENT then
		Hide(OPUnitFactory.GetUnit(g_curevent.unit))
		g_countingevent = true
		if g_curevent.length ~= nil then
			g_counter = g_curevent.length * 60
		end
	
	elseif g_curevent.event == PAUSEEVENT then
		g_counter = g_curevent.length * 60
		g_countingevent = true
	
	elseif g_curevent.event == PLAYSOUNDEVENT then
		PlaySoundEffect(g_curevent.name)
		g_counter = g_curevent.length * 60
		g_countingevent = true
	elseif g_curevent.event == SHAKEEVENT then
		g_counter = g_curevent.length * 60
		g_countingevent = true
		g_background = {}
		ApplyToChildren(GetCurrentManager(), GetBackgroundPos)
	elseif g_curevent.event == WAITEVENT then
		g_counter = g_curevent.length * 60
		g_countingevent = true
	elseif g_curevent.event == TRAVELING then
		g_background = {}
		ApplyToChildren(GetCurrentManager(), GetBackgroundPos)
		local vec = Vector:new(g_curevent.endx,g_curevent.endy)
		for u,tab in pairs(g_background) do
			local vecunit = Vector:new(tab.ut)
			vecunit = vec + vecunit
			if u == 1 then --on ne met 'levent de fin que pour un seul background, comme cela pas de soucis
				SpriteMover.Move(tab.ut,vecunit,g_curevent.speed,self)
			else
				SpriteMover.Move(tab.ut,vecunit,g_curevent.speed)
			end
		end
		
	elseif g_curevent.event == MOVESPRITE then
		local vec = Vector:new(g_curevent.endx,g_curevent.endy)
		SpriteMover.Move(OPUnitFactory.GetUnit(g_curevent.unit),vec,g_curevent.speed,self)
	elseif g_curevent.event == SETFRAME then
		OPUnitFactory.GetUnit(g_curevent.unit).SetFrame(g_curevent.frame)
		if g_curevent.length ~= nil then
			g_counter = g_curevent.length * 60
		end
		g_countingevent = true
	elseif g_curevent.event == STOPEMITTING then
		local ut = OPUnitFactory.GetUnit(g_curevent.unit)
		if ut.ClassName ==  "Particle2DEmitter" then
			ut.CreationPerSec = 0
		else
			ut.StopEmitter()
		end
		if g_curevent.length ~= nil then
			g_counter = g_curevent.length * 60
		end
		g_countingevent = true
	elseif g_curevent.event == FADESPRITE then
		g_curtreatedunit = OPUnitFactory.GetUnit(g_curevent.unit)
		g_curtreatedunit.Flags_Visible = true
		if g_curevent.fadetype == "visible" then
			g_curtreatedunit.Color.a = 0
		else
			g_curtreatedunit.Color.a = 255
		end
		--on fait rien c'est dans l'onupdate
		--g_counter = g_curevent.length * 60
		--g_countingevent = true
		--g_background = {}
	else
		AssertPopup(false,"erreur , event non trouvé !")
	end
	
	
	--on avance d'une phase quoiqu'il arrive
	g_curphase = g_curphase + 1
end

function IsLockedUnit(name)
	AssertPopup(type(name) == "string","erreur param lockedunit")
	return g_lockedspritetab[name]
end

function OnMoveEnd()
	g_cureventisfinished = true
end

function OnSwitchEnd()
	g_cureventisfinished = true
end

function OnVideoEnd()
	g_cureventisfinished = true
end

function OnAnimationEnd()
	g_cureventisfinished = true
end

function OnFaderEnd()
	if g_curevent.event == FADEINEVENT then --le fade out on le laisse par contre tant qu'on n'a pas appelé le fadein
		Disable(FadeIn)
		FadeIn.Period = DEFAULTFADETIME
		FadeOut.Period = DEFAULTFADETIME
	end
	
	g_cureventisfinished = true
end

function FinishCutscene()
	--SetVariables("CurrentGamePhase",g_oldphase)
	
	GlobalFunctions.FinishCutscene(g_oldphase)	
	Disable(self)

	CutSceneMask.Enabled = false

	--on remet comme c'était avant
	if g_activecursor == true then
		EnableRemotePointerGen( 1, true )
		Load("Cursor",'Cursor').Flags_Visible = g_visibilitycursor
	end
	

	
	OPLog.Log("-- fin cutscene --"..g_curscenename)
	
	if g_unittonotify ~= nil and g_unittonotify~= nil then
		g_unittonotify.OnCutsceneEnd( g_curscenename )
	end
end

function OnSequenceEnd()
	g_cureventisfinished = true
end

function OnUpdate(dt)

	g_counter = g_counter-1
	
	if g_counter <= 0 and g_countingevent then
		g_cureventisfinished = true
	end
		
	if g_curevent.event == SHAKEEVENT then
		local deltax = (math.random()*2-1) * g_curevent.strength
		local deltay = (math.random()*2-1) * g_curevent.strength
		for u,tab in pairs(g_background) do
			tab.ut.Position_X = tab.ut.Position_X + deltax
			tab.ut.Position_Y = tab.ut.Position_Y + deltay 
		end
		
		if g_cureventisfinished then
			for u,tab in pairs(g_background) do
				tab.ut.Position_X = tab.x
				tab.ut.Position_Y = tab.y 
			end
		end
		
	end
	
	if g_curevent.event == FADESPRITE then
		if g_curevent.fadetype == "visible" then
			g_curtreatedunit.Color.a = math.min(255,g_curtreatedunit.Color.a + g_curevent.speed)
			if g_curtreatedunit.Color.a == 255 then
				g_cureventisfinished = true
			end
		else
			g_curtreatedunit.Color.a = math.max(0,g_curtreatedunit.Color.a - g_curevent.speed)
			--OPLog.Log(g_curtreatedunit.Color.a)
			if g_curtreatedunit.Color.a == 0 then
				g_cureventisfinished = true
			end		
		end
	end
	
	if g_cureventisfinished then
		LaunchNextStep()
	end
		
end

function GetBackgroundPos(ut)
	if ut.ClassName == "Background" then
		local tab = {ut = ut, x= ut.Position_X,y=Position_Y}
		table.insert(g_background,tab)
	end	
end





-- ------------------------------------------------------------------------
--  On disable handler
-- ------------------------------------------------------------------------
function OnDisable()
    OPInput.UnregisterListener()
end

-- ------------------------------------------------------------------------
--  On mouse ldown
-- ------------------------------------------------------------------------
function OnMouseLDown(x,y)

end
