------------------------------------------------------------------------------------------------------
--------------------------------------------- Sequence Player---------------------------------------
---------- joue une séquence de phrase avec les poses des persos et tout ----------------------
------------------------------------------------------------------------------------------------------

.namespace SequencePlayer


g_lastskippedsound = ""
g_currentlyplaying = ""
g_currentseqtype = ""

function OnEnable()

	g_globalcounter = 0
	g_hiddenperso = true
	
	g_refpoint = Load("RefPointDiag","Unit2D")
	
	g_lastskippedsound = "" --pour s'assurer que le son n'est skippé qu'une seule fois

end

--Possibilité d'avoir un dialogue aleatoire dans un tableau donné
function PlayRandomSequence ( tab, unitcallback, insidecall)
	local n = math.random(table.getn(tab))
	PlaySequence(tab[n],unitcallback,insidecall)
end

function PlaySequence( keyword,unitcallback,insidecall, seqtype)
	Cursor.ChangeCursor(14)
	if g_currentlyplaying ~= "" then
		AssertPopup( false, "Dialogue déjà lancé : "..g_currentlyplaying.. " Bug: "..keyword)
		return
	end	
	if keyword == nil or type(keyword) ~= "string" then
		AssertPopup( false, "erreur paramétre")
		return
	end
	if seqtype == "help" then
		SubtitlesBox.SetFrame(3)
	elseif 	seqtype == "transition" then
		SubtitlesBox.Position_X = 105
		SubtitlesBox.SetFrame(2)	
	else
		SubtitlesBox.Position_X = 210
		SubtitlesBox.SetFrame(0)
		if DialData[keyword].att == 2 then
			SubtitlesBox.SetFrame(1)
		end
	end



	if DialData[keyword] == nil or type(DialData[keyword]) ~= "table" then
		AssertPopup( false, "mot clé \""..keyword.."\" pas dans le tableau de dialogues")
		
		if (unitcallback ~= nil and unitcallback.OnSequenceEnd ~= nil) then
			Oplog.Log("mot clé absent onsequence end "..tostring(keyword))
			unitcallback.OnSequenceEnd(keyword)
			Cursor.ChangeCursor(0)
		end
		
		self.Enabled = false
		return
	end
	

	
	local unitname
	if unitcallback then
		unitname = unitcallback.Name
	else
		unitname = ""
	end
	
	OPLog.Log("playsequence "..tostring(keyword).." "..GetVariables("CurrentGamePhase").." callback : "..tostring(unitname))
	

	
	g_currentlyplaying = keyword
	g_currentseqtype = seqtype
	
	g_unitcallback = unitcallback
	
	g_baskeyword = keyword
	
	g_compteur = 0
	
	UpdatePerso()
	
	if insidecall ~= true then
		
		g_oldphase = GetVariables("CurrentGamePhase")
		GlobalFunctions.SwitchToCutscene(keyword)	
	end
		
	
	
	g_speechcounter = 0
	
	g_talking = true
	
	g_curpose ="" --"" , "Angry" ou "Neutral"
	
	g_lastperso = nil
	PlayNextLine()
	
	g_lastskippedsound = ""
	--Enable("SubtitlesName","Sprite")
	
end
function UpdatePerso()
	g_curperso = DialData[g_currentlyplaying].Char
	if DialData[g_currentlyplaying].att==2 then
		--SubtitlesBox.SetFrame(1)
		right_char.Image = "media/graph/dial_character/"..g_curperso.."_full_right.png"
		right_char.Enabled = true
		left_char.Enabled = false
	else
		--SubtitlesBox.SetFrame(0)
		left_char.Image = "media/graph/dial_character/"..g_curperso.."_full_left.png"
		left_char.Enabled = true
		right_char.Enabled = false
	end
end
function UpdateSubtitlesBox()

end

function GetCurPerso()
	return g_curperso,g_hiddenperso
end
 

function PlayNextLine()
	g_lastskippedsound = ""
	if g_compteur == 0 then
		g_currentlyplaying = g_baskeyword
	else
		g_currentlyplaying = g_baskeyword.."_"..g_compteur
	end	
	g_compteur = g_compteur + 1
	UpdatePerso()
	
	if DialData[g_currentlyplaying].att == 2 then
		SubtitlesBox.SetFrame(1)
	elseif g_currentseqtype ~= "transition" and g_currentseqtype ~= "help" then
		SubtitlesBox.SetFrame(0)
	end
	--sécurité
	if DialData[g_currentlyplaying] == nil then
		FinishSequence()
		return
	end
	

	
	--TODO à adapter
	DisplaySimpleText(g_currentlyplaying,DEFAULTDISPLAYTIME,self)

end

function OnSubtitlesEnd()
	OnSoundEnd()
end

function OnSoundEnd()

	if (DialData[g_currentlyplaying].cont == 1) then
		PlayNextLine()
	else
		FinishSequence()
	end

end

function FinishSequence()
	OPLog.Log("fin sequence "..g_baskeyword)
	Cursor.ChangeCursor(0)
	
	GlobalFunctions.FinishCutscene(g_oldphase)	
	g_lastperso = g_curperso
	left_char.Enabled = false
	
	
	local OnSequenceEndReturn = false
	if g_unitcallback ~= nil and g_unitcallback.OnSequenceEnd~=nil then
		OPLog.Log("On Sequence end"..tostring(g_baskeyword))
		OnSequenceEndReturn = g_unitcallback.OnSequenceEnd(g_baskeyword) --si true on ne disable pas le sequence player
	end
	
	if OnSequenceEndReturn == nil or OnSequenceEndReturn == false then
		self.Enabled = false
	end
	
	GlobalFunctions.AddDialog(g_baskeyword)
	
	if SubtitlesDisplay.ParentEnabled then
		SubtitlesDisplay.HideSubtitles(g_currentlyplaying)
	end

end

function OnDisable()
	if g_curperso ~= nil and g_curperso ~= "" then
		Disable("DiagSpr"..g_curperso,"Unit")
	end
	left_char.Enabled = false
	SubtitlesBox.Flags_Visible = false
end



function OnUpdate(dt)
	
	
end

