-------------------------------------------------------------------------------------------------------
--------------------------------------------- Sous-titres---------------------------------------------
------------------------------------------------------------------------------------------------------

.namespace SubtitlesDisplay


g_oldphase = ""
g_IsActive = false
g_keyword = ""
g_skipnextonmouselup = false

g_soundlength = 0

CORRESPONDANCEFRAMECHAR =
{
	Guy = 1,
	Girl = 0,
	GirlThinking =2,
	Journal = 3 ,
	GirlSmiling = 4,
}

	
function OnEnable()
	self.Flags_AcceptAllInput = true
	OPInput.RegisterListener(self,EVENT_MOUSE_LDOWN + EVENT_MOUSE_LUP)
	OPInput.SetListenerPriority(self,LISTENER_HIGH_PRIORITY)
end


function OnWbpMouseLUp()
	--je commente le IsPlayed, c'était uniquement sur archaeo
	if g_skipnextonmouselup == false then --or GlobalFunctions.IsPlayed(g_baskeyword) then
		--on est en phase document (popup ou lettre, on prend pas en compte ce click)
		if PopupManager.active then
			return false
		end
		
		OPLog.Log("on skipe "..g_keyword)
		g_timespent = g_soundlength + 1 --on accélére le compteur , moins de code à faire en cas de -1, ca ne skippe pas donc
		
	end
	
	g_skipnextonmouselup = false

end

-- function OnMouseLUp()
	-- if g_skipnextonmouselup == true then
		-- g_skipnextonmouselup = false
		-- return true
	-- end
	-- return false
-- end

-- fonction principale à appeler
--soundname : nom du son en cours, length, longueur du son ou du texte
--text : si précisé c'est ceui ci qui est utilisé (pour des display info)
function DisplaySubtitlesForSound(SoundName, length ,text, isDirectString,utnotify)
	
	OPLog.Log("on affiche soustitre "..tostring(SoundName).." de longueur "..length.. " ou le texte "..tostring(text))
	
	AssertPopup((type(SoundName) == "string" or type(text) == "string") and type(length) == "number" ,"Erreur params")

	if not GetUserData("Subtitles") and text == nil then
		return
	end
	
	-- on ne bloque que si l'affichage contient un unitnotify, sinon on affiche juste
	if (IsEqualVariables("CurrentGamePhase","SearchSite") or IsEqualVariables("CurrentGamePhase","Closeup")) and utnotify ~= nil then
		g_oldphase = GetVariables("CurrentGamePhase")
		SetVariables("CurrentGamePhase","BlockClick")
	end
	
	--sécurité
	SubtitlesText.Enabled = true

	
	g_utnotify = utnotify
	
	g_keyword = text
	if text == nil then
		SetUnicode(SoundName)
	elseif isDirectString ~= nil and isDirectString == true then
		SubtitlesText.Text = text
		SubtitlesBox.Flags_Visible = true

	else
		SubtitlesText.Text = GlobalFunctions.GetText(text)
		SubtitlesBox.Flags_Visible = true

	end
	
	SubtitlesBox.Color.a = 255

	SubtitlesText.Font = Font_DejaVuSerif
	
	if IsEqualVariables("VideoMode",true) then
		-- SubtitlesText.Position_X = VIDEOSUBTITLESPOSX
		-- SubtitlesText.Position_Y = VIDEOSUBTITLESPOSY
		-- SubtitlesText.Dimension_Width = VIDEOSUBTITLESWIDTH
		-- ---SubtitlesText.CharSpacing = VIDEOCHARSPACING
		-- SubtitlesText.Font = Font_Arial22
		
		-- SubtitlesBox.SetFrame(2)
		-- SubtitlesBox.Position_Y = SUBTITLESBOXPOS.VIDEO
		-- SubtitlesBox.Color.a = 0
		
		VideoText.Enabled = true		
		VideoText.Text = text
		SubtitlesBox.Enabled = false
	else
		SubtitlesBox.Enabled = true
		VideoText.Enabled = false
	end

	g_IsActive = true

	CurrentSubtitle = 0
	SubtitleTimer = nil
	
	-- MARGE_ERREUR mis en place pour eviter que le texte soit rogné en bas
	g_soundlength = length
	
	g_soundname = SoundName
	g_timespent = 0
	
	-- si on est en tutorial, on fait clignoter le box highlight
	if IsEqualVariables("CurrentGamePhase","Tutorial") then
		SubtitlesBoxHighlight.Flags_Visible = true
		self.compteurfade = NBFADEHIGHLIGHT
		self.curfadeout = true
		SpriteFader.Fade(SubtitlesBoxHighlight,self.curfadeout,FADEHIGHTLIGHTLENGTH,self)
	end
	
	if DialData[g_keyword] ~= nil and CORRESPONDANCEFRAMECHAR[DialData[g_keyword].Char] ~= nil then
		--Search_Polaroid.SetFrame(CORRESPONDANCEFRAMECHAR[DialData[g_keyword].Char])
	else
		--Search_Polaroid.SetFrame(0)
	end
	
end



function OnFaderEnd()
	self.compteurfade = self.compteurfade -1	
	if self.compteurfade >=0 then
		self.curfadeout = not self.curfadeout
		SpriteFader.Fade(SubtitlesBoxHighlight,self.curfadeout,FADEHIGHTLIGHTLENGTH,self)
	else
		SubtitlesBoxHighlight.Flags_Visible = false
	end
end


function SetUnicode(SoundName)
	local txt = GlobalFunctions.GetText(SoundName,true)
	
	if txt ~= nil then
		SubtitlesText.Text = GlobalFunctions.GetText(SoundName)
		SubtitlesBox.Enabled = true
	end
end

function OnUpdate(dt)
	if g_soundlength == -1 then
		--on ne fait rien c'est considére comme un temps infini
		return
	end

	if g_IsActive and not IsEqualVariables("CurrentGamePhase","Transition") then
		
		--on fait progresser le temps que si on est en searchsite (pas bloqué donc)
		if IsEqualVariables("CurrentGamePhase","SearchSite") then
			g_timespent = g_timespent + dt
		end
		
		local coeff = g_timespent  / g_soundlength
		
		if coeff > 1 then --and not IsEqualVariables("CurrentGamePhase","Cutscene") then --si on est en cutscene, on laisse le sequence player s'en occuper TODO à revérifier !!!!!
			if IsEqualVariables("CurrentGamePhase","BlockClick") then
				OPLog.Log("on remet la phase à "..g_oldphase.. " avant "..GameVariables.Get("CurrentGamePhase"))
				SetVariables("CurrentGamePhase",g_oldphase)
			end
			HideSubtitles()
			if g_utnotify ~= nil then
				g_utnotify.OnSubtitlesEnd(g_keyword)
			end
		elseif not IsEqualVariables("CurrentGamePhase","Cutscene") then
			--todo à améliorer
			SubtitlesBox.Flags_Visible = true
		end

	end
end


function HideSubtitles( voicename)
  
	--si voicename est défini on ne cache les sous titre que si c'est le même
	
	if (voicename ~= nil and voicename ~= g_soundname) then
		OPLog.Log("on intercepte hidsubtitles "..voicename.." "..tostring(g_soundname))
		return
	end
	
	SubtitlesBox.Flags_Visible = false

	--local SubtitlesMask = Load('SubtitlesMask', 'Sprite')
	--SubtitlesMask.Enabled = false
	--StopMove2D(g_subtitlestext)
	CurrentSubtitle = nil
	GUITextSubtitles = nil
	TranslatedSubtitle = nil
	g_IsActive = false
	
	self.Enabled = false
	
	--g_hasmove = false
	--g_timespent = 0
	--g_Coeff
  
end
