-- Project load animator

.namespace SplashScreenManager

.define STATE_ADDITIONAL_FADEIN	= -3
.define STATE_ADDITIONAL		= -2
.define STATE_ADDITIONAL_FADEOUT= -1
.define STATE_BIGFISH_FADEIN	= 0
.define STATE_BIGFISH			= 1
.define STATE_BIGFISH_FADEOUT	= 2
.define STATE_IIM_FADEIN	= 3
.define STATE_IIM			= 4
.define STATE_IIM_FADEOUT	= 5
.define STATE_CZP_FADEIN		= 6
.define STATE_CZP				= 7
.define STATE_CZP_FADEOUT		= 8
.define STATE_EXIT				= 9

.define SPEED_FADE				= 500
.define NB_SECONDS_STAY			= 2
.define FADELENGTH 				= 0.5

g_firstframe = true
bulky = nil
meridian = nil
wbp = nil
fader = nil
timer = nil
additionnal = nil

VIDEOSIZEFORMINCONFIG = 130

function OnEnable()
	OPInput.RegisterListener(self , EVENT_MOUSE_LUP )
	self.Flags_AcceptAllInput = true
	

end

function OnWbpMouseLUp()
	if state == STATE_BIGFISH or state == STATE_IIM or state == STATE_CZP or state == STATE_ADDITIONAL then
		timer.Enabled = false
		OnTimeOut()	
		return true
	end
end

function StartSplashScreen()
	OPLog.Log("loading the game?")
		--on charge le profil pour le volume sonore
	local loadprofileok  = SaveManager.loadProfile() -- A revoir dans le cas où il n'y a pas de profil
	SetVariables("loadprofileok",loadprofileok)	
	MenuMusic.Volume = ProfilConfig.Get("VolumeMusic") / 100
	
	local cmd = {}
	if not OPCore.EditorMode() then
		cmd = OPWin.GetCommandLine() 
		OPLog.Log("arguments "..GlobalFunctions.Display(cmd))
	end
--	if OPGraphic.GetVideoMemoryStatistics().total < VIDEOSIZEFORMINCONFIG or  (cmd[1] ~= nil and string.find(cmd[1],"safe")) then
--		GlobalFunctions.SwitchToMinConfig()
--		GlobalFunctions.g_configminihasbeenasked = true
--	else
--		ProfilConfig.Set("UseMinConfig",false)
--		GlobalFunctions.SetResoAuto()
--	end

		-- Charge les options
	if ProfilConfig.Get("Fullscreen") then
		OPGraphic.SetScreenMode({fullscreen=true}) 
	else	
		OPGraphic.SetScreenMode({fullscreen=false}) 
	end
	
	
	if OPCore.EditorMode() then
		SplashScreenManager.Enabled = false
		Menu_Start.Enabled = true
		return
	end
	
	additionnal = OPUnitFactory.CreateUnit("Sprite")
	additionnal.Image = 'media/common/graph/interface/Splash_Screen/additional_publisher.jpg'
	additionnal.Flags_Visible = false
	additionnal.Position_X = (WIDTH_SCREEN - additionnal.Dimension_Width) /2
	additionnal.Position_Y = (HEIGHT_SCREEN - additionnal.Dimension_Height) /2
	
	bulky = OPUnitFactory.CreateUnit("Sprite")
	bulky.Image = 'media/common/graph/interface/Splash_Screen/splash_bfg_'..DebugOptions.curlanguage..'.jpg'
	bulky.Flags_Visible = false

	meridian = OPUnitFactory.CreateUnit("Sprite")
	meridian.Image =  'media/common/graph/interface/Splash_Screen/splash_iim.jpg'
	meridian.Flags_Visible = false

	
	wbp = OPUnitFactory.CreateUnit("Sprite")
	wbp.Image = 'media/common/graph/interface/Splash_Screen/splash_cazap.jpg'
	wbp.Flags_Visible = false

	
	timer = GlobalFunctions.CreateTimerSystem(self.Name,NB_SECONDS_STAY,false,false,true, self)
	
	if DebugOptions.AdditionalSplashScreen == true then
		state = STATE_ADDITIONAL_FADEIN
		additionnal.Flags_Visible = true
		SpriteFader.Fade(additionnal,true,FADELENGTH,self)
	elseif DebugOptions.ShowBigfishScreen == true then 
		state = STATE_BIGFISH_FADEIN
		SpriteFader.Fade(bulky,true,FADELENGTH,self)
		bulky.Flags_Visible = true
	else
		state = STATE_IIM_FADEIN
		SpriteFader.Fade(meridian,true,FADELENGTH,self)
		meridian.Flags_Visible = true
	end
	cpt = 0
	fade_enabled = true
	fade_reverse = false
end

function OnFaderEnd()

	if state == STATE_BIGFISH_FADEIN or state == STATE_IIM_FADEIN or state == STATE_CZP_FADEIN or state == STATE_ADDITIONAL_FADEIN then
		timer.Enabled = true
		timer.Start()
	elseif state == STATE_ADDITIONAL_FADEOUT then
		if DebugOptions.ShowBigfishScreen then
			SpriteFader.Fade(bulky,true,FADELENGTH,self)
			bulky.Flags_Visible = true
		else
			meridian.Flags_Visible = true
			SpriteFader.Fade(meridian,true,FADELENGTH,self)
			state = state + 3 --on skippe le bigfish
		end
	elseif state == STATE_BIGFISH_FADEOUT then
		meridian.Flags_Visible = true
		SpriteFader.Fade(meridian,true,FADELENGTH,self)
	elseif state == STATE_IIM_FADEOUT then
		wbp.Flags_Visible = true
		SpriteFader.Fade(wbp,true,FADELENGTH,self)
	elseif state == STATE_CZP_FADEOUT then
--		self.loadingtext = OPUnitFactory.CreateUnit("Text")
--		if UNICODELANGUAGE[DebugOptions.curlanguage] then
--			self.loadingtext.Text = "Loading ..."
--		else
--			self.loadingtext.Text = GlobalFunctions.GetText("main_loading")
--		end
		-- -- à mettre dans le kit de loc sans doute
--		self.loadingtext.Font = Font_DejaVuSerif
--		self.loadingtext.Layer = LayerCursor
--		self.loadingtext.Position_X = 779
--		self.loadingtext.Position_Y = 547
	end
	state = state +1
end

function OnTimeOut()
	if state == STATE_BIGFISH then
		SpriteFader.Fade(bulky,false,FADELENGTH,self)
	elseif state == STATE_IIM then
		SpriteFader.Fade(meridian,false,FADELENGTH,self)
	elseif state == STATE_CZP then
		SpriteFader.Fade(wbp,false,FADELENGTH,self)
	elseif state == STATE_ADDITIONAL then
		SpriteFader.Fade(additionnal,false,FADELENGTH,self)
	end
	state = state + 1
end

function Finish()
	SpriteFader.StopAll()
	OPUnitFactory.CloseUnit(meridian)
	OPUnitFactory.CloseUnit(bulky)
	OPUnitFactory.CloseUnit(wbp)
	OPUnitFactory.CloseUnit(additionnal)
	OPUnitFactory.CloseUnit(timer)
	SplashScreenManager.Enabled = false
	OPLog.Log("Game loaded")
	Cursor.Flags_Visible = true
	Menu_Start.Enabled = true
	OPUnitFactory.CloseUnit(self.loadingtext)
end

function OnUpdate(dt)

	if g_firstframe then
		g_firstframe = false
		
		StartSplashScreen()
		return
	end
	
	if state == STATE_EXIT then
		Finish()
		state = state + 1
	end
	OPLog.Log(state)
	
end

