.namespace New_CreditsText

STARTPOSY = 800 -- Position de départ des textes
STARTPOSX = 215 -- Position de départ des textes
ENDPOSY = 25 -- Position finale des textes
FADEINPOSY = 780 -- Postion à prtir de laquelle le texte commence a être visible
FADEOUTPOSY = 50 -- Position à partir de laquelle le texte commence a devenir invisible

SPEED = 25 -- Vitesse de défilement
SPEEDALPHA = 200 -- Vitesse de fade du texte

TITLESPACING = 90 -- Espace pour les titres
SUBTITLESSPACING = 30 -- Espace pour les sous titre
NAMESPACING = 17 -- Espace pour les noms

NORMALFONT = "Font_Arial22"
BOLDFONT = "Font_Arial22_Bold"

g_currenttext = 1
g_currentsoustext = 0

g_currenttitle = 1
g_currentsubtitle = 0
g_currentname = 0

function StartCredits()
	OPLog.Log("Start")
	

	g_currenttitle = 1
	g_currentsubtitle = 0
	g_currentname = 0
	
	ShowText("credits_"..g_currenttitle.."_"..g_currentsubtitle.."_"..g_currentname,BOLDFONT)	
end

function ShowText(id,font,transitionchar)
	-- OPLog.Log("id = "..id)
	local text = OPUnitFactory.CreateUnit("TTFText")
	text.AliasName = "New_CreditsText"
	-- text.Name = "credittext_"..g_currenttext.."_"..g_currentsoustext
	text.Name = "credittext_"..g_currenttitle.."_"..g_currentsubtitle.."_"..g_currentname
	--text.Parent = Menu_options_credits_parent
	text.Parent = textparent
	
	-- Gestion des exeptions pour mettre en gras
	if text.Name == "credittext_1_0_0" or text.Name == "credittext_2_0_0" or text.Name == "credittext_3_0_0" then
		text.CharSize_Height=24
		text.CharSize_Width=24
		text.Font="media/common/graph/fonts/TrajanPro-Regular.ttf"
	elseif text.Name == "credittext_1_1_0" or text.Name == "credittext_1_2_0" or text.Name == "credittext_1_3_0" or text.Name == "credittext_1_4_0"
			or text.Name == "credittext_1_5_0" or text.Name == "credittext_1_6_0" or text.Name == "credittext_1_7_0" or text.Name == "credittext_1_8_0"
			or text.Name == "credittext_1_9_0" or text.Name == "credittext_1_10_0" or text.Name == "credittext_1_11_0" or text.Name == "credittext_1_12_0" or text.Name == "credittext_1_13_0" 
			or text.Name == "credittext_2_1_0" or text.Name == "credittext_2_2_0" or text.Name == "credittext_3_1_0" or text.Name == "credittext_3_2_0" or text.Name == "credittext_2_3_0" then
	text.CharSize_Height=20
		text.CharSize_Width=20
		text.Font="media/common/graph/fonts/amrtypen.ttf"
	else
		text.CharSize_Height=16
		text.CharSize_Width=16
		text.Font="media/common/graph/fonts/amrtypen.ttf"
	end
	
	text.Alignment = 1
	text.Color.r = 255
	text.Color.g = 255
	text.Color.b = 255
	text.Color.a = 0
	text.Dimension_Width = 850
	text.AutoDimension_X = false
	
	if transitionchar ~= nil then
		text.Text = transitionchar.."\\n"..GlobalFunctions.GetText(id)
	else
		text.Text = GlobalFunctions.GetText(id)
	end	
	
	if transitionchar ~= nil then
		text.Text = transitionchar.."\\n"..GlobalFunctions.GetText(id)
	else
		text.Text = GlobalFunctions.GetText(id)
	end
	
	text.Position_X = STARTPOSX
	text.Position_Y = STARTPOSY
	
	text.Enabled = true	
end

function OnUpdate(dt)
	self.Position_Y = self.Position_Y - (SPEED * dt)
	
	-- FadeIn des textes
	if self.Position_Y + self.Dimension_Height < FADEINPOSY and self.Position_Y > FADEOUTPOSY and self.Color.a < 255 then
		local alpha = self.Color.a + (SPEEDALPHA * dt)
		if alpha > 255 then
			alpha = 255
		end
		self.Color.a = alpha
	-- FadeOut des textes
	elseif self.Position_Y < FADEOUTPOSY and self.Color.a > 0 then
		local alpha = self.Color.a - (SPEEDALPHA * dt)
		if alpha < 0 then
			alpha = 0
		end
		self.Color.a = alpha
	end
	
	-- Détruit les units de texte
	if self.Position_Y < ENDPOSY then
		self.Enabled = false
		
		-- Revient au menu options quand les crédits sont terminés
		if self.Name == "credittext_12_5_1" then
			Credits_Menu_Start_area.OnWbpMouseLUp()
		else
			OPUnitFactory.CloseUnit(self)
		end		
	end
	
	-- Si c'est la dernière unit créée
	if self.Name == "credittext_"..g_currenttitle.."_"..g_currentsubtitle.."_"..g_currentname then
		-- Si le texte suivant est un nom
		if GlobalFunctions.GetText(tostring("credits_"..g_currenttitle.."_"..g_currentsubtitle.."_"..(g_currentname+1)),true) ~= nil
		and (self.Position_Y + self.Dimension_Height + NAMESPACING) < STARTPOSY 
		then
			g_currentname = g_currentname + 1		
			ShowText("credits_"..g_currenttitle.."_"..g_currentsubtitle.."_"..g_currentname)
		-- Si le texte suivant est un soustitre
		elseif GlobalFunctions.GetText(tostring("credits_"..g_currenttitle.."_"..(g_currentsubtitle+1).."_0"),true) ~= nil
		and (self.Position_Y + self.Dimension_Height + SUBTITLESSPACING) < STARTPOSY 
		then
			g_currentname = 0
			g_currentsubtitle = g_currentsubtitle + 1
			ShowText("credits_"..g_currenttitle.."_"..g_currentsubtitle.."_"..g_currentname)
		-- Si le texte suivant est un titre
		elseif GlobalFunctions.GetText(tostring("credits_"..(g_currenttitle+1).."_0_0"),true) ~= nil 
		and (self.Position_Y + self.Dimension_Height + TITLESPACING) < STARTPOSY 
		then
			g_currentname = 0
			g_currentsubtitle = 0
			g_currenttitle = g_currenttitle + 1
			ShowText("credits_"..g_currenttitle.."_"..g_currentsubtitle.."_"..g_currentname,BOLDFONT)
		end
	
	end	
end

-- Le OnDisable bug
-- function OnDisable()
	-- OPLog.Log("Disable : "..self.Name)
	-- OPLog.Log("closing = "..tostring(self.closing))
	-- if self.closing == false then
		-- OPLog.Log("KILL : "..self.Name)
		-- self.closing = true
		-- OPUnitFactory.CloseUnit(self)
	-- end
-- end
