
.namespace Profiles

g_profiletodelete = 0
-- Ce script gère les boutons de la popup d'aide
function OnEnable()
end

function OnUpdate(dt)
end

function OnDisable()
end

function OnWbpMouseLUp()
	if not self.PrivateCanClickOn() then
		return
	end
	PlaySoundEffect("int_click")
	self.OnMouseOff()
	-- Bouton nouveau profil
	if self.Name == "Profiles_select_new" then
		curseur.Flags_Visible = false
		--PopupManager.OpenPopupYN("msg_ig_suppression_profil",Profiles_popup)
		if Menu_Start.NbProfilesCount() == 3 then
			PopupManager.OpenPopupInfo("msg_ig_profil_limitemax")
		else
			-- Enlève la popup Profil
			Profiles_popup.Enabled = false
			
			-- Active la fenetre de creation de profil
			New_Profiles_popup.Enabled = true
		
		end
		
		-- Bouton choix du profil
	elseif self.Name == "Profiles_select_ok" or self.Name == "Profiles_Area_quit" then
		curseur.Flags_Visible = false
		--charge le profils selectionné
		
		-- Enlève la popup Profil
		Profiles_popup.Enabled = false
		
		
		-- Active les boutons du menus principal
		Menu_StartGame_Parent.Enabled = true
		--On rafraichis le menu start
		Menu_Start.OnEnable()
		
		--bouton supprimer sauvegarde 1
	elseif self.Name == "Delete_1_Profiles" then
		if Menu_Start.NbProfilesCount() == 1 then
			return
		end
		curseur.Flags_Visible = false
		Profiles_popup.g_profiletodelete = 1
		
		-- Enlève la popup Profil
		--Profiles_popup.Enabled = false
		
		-- Active la popup de suppression de profils
		--Delete_Profiles_popup.Enabled = true
		PopupManager.OpenPopupYN("msg_ig_suppression_profil",Profiles_popup)
		
		
		
		--bouton supprimer sauvegarde 2
	elseif self.Name == "Delete_2_Profiles" then
		if Menu_Start.NbProfilesCount() == 1 then
			return
		end
		curseur.Flags_Visible = false
		Profiles_popup.g_profiletodelete = 2
		
		-- Enlève la popup Profil
		--Profiles_popup.Enabled = false
		
		-- Active la popup de suppression de profils
		--Delete_Profiles_popup.Enabled = true
		PopupManager.OpenPopupYN("msg_ig_suppression_profil",Profiles_popup)
		
		
		
		--bouton supprimer sauvegarde 3
	elseif self.Name == "Delete_3_Profiles" then
		if Menu_Start.NbProfilesCount() == 1 then
			return
		end
		curseur.Flags_Visible = false
		Profiles_popup.g_profiletodelete = 3
		-- Enlève la popup Profil
		--Profiles_popup.Enabled = false
		-- Active la popup de suppression de profils
		--Delete_Profiles_popup.Enabled = true
		PopupManager.OpenPopupYN("msg_ig_suppression_profil",Profiles_popup)
		
		--bouton d'annulation de suppression de profils
		--elseif self.Name == "Delete_Profiles_popup_Cancel_area" then
		--	curseur.Flags_Visible = false
		--	g_profiletodelete = 0
			-- Enlève la popup de suppression de Profil
		--	Delete_Profiles_popup.Enabled = false
			-- Active la fenetre de profil
		--	Profiles_popup.Enabled = true
			
		
		--bouton de validation de suppression de profils

		
		
		--fermer la fenetre de creation de profils
	elseif self.Name == "New_Profiles_cancel" then
		curseur.Flags_Visible = false
		
		
		-- Enlève la popup de creation de nouveau Profil
		New_Profiles_popup.Enabled = false
		
		-- Active la fenetre de profil
		Profiles_popup.Enabled = true
		
		
		--creation d'un nouveau profil
	elseif self.Name == "New_Profiles_ok" then
			local nbprofiles = Menu_Start.NbProfilesCount()
			local size = string.len(New_Profiles_popup.g_profilname)
			if New_Profiles_popup.VerifyUniqueName() == false then
				PopupManager.OpenPopupInfo("msg_ig_profil_nom_existant")
			elseif size > 0  and nbprofiles  < 3 then
			
			curseur.Flags_Visible = false
			currentprofil = 1
				--sauvegarde le nouveau profils
				for i=1,3 do
					if ProfilConfig.Get("Profil"..i) ~= "Created" then
						currentprofil = i
						break
					end
				end
					-- Initialise les données du profil
					ProfilConfig.Set("Profil"..currentprofil,"Created")	
					ProfilConfig.Set("Profil"..currentprofil.."_name",New_Profiles_popup.g_profilname)
					ProfilConfig.Set("Profil"..currentprofil.."_lastscreen","")
					
					ProfilConfig.Set("CurrentProfil",currentprofil)
				-- Sauvegarde
				SaveManager.saveProfile()
				SaveManager.saveGame("Profil"..currentprofil)

				-- Enlève la popup de creation de nouveau Profil
				New_Profiles_popup.Enabled = false
				
				-- Active la fenetre de profil
				Profiles_popup.Enabled = true
			end
		elseif self.Name == "Select_1_Profiles" then
--			Bg_1_Profiles.SetFrame(1)
--			Bg_2_Profiles.SetFrame(0)
--			Bg_3_Profiles.SetFrame(0)
			ProfilConfig.Set("CurrentProfil",1)
			-- Sauvegarde
			SaveManager.saveProfile()
			SaveManager.saveGame("Profil1")
			Profiles_popup.OnEnable()
		elseif self.Name == "Select_2_Profiles" then
--			Bg_1_Profiles.SetFrame(0)
--			Bg_2_Profiles.SetFrame(1)
--			Bg_3_Profiles.SetFrame(0)
			ProfilConfig.Set("CurrentProfil",2)
			-- Sauvegarde
			SaveManager.saveProfile()
			SaveManager.saveGame("Profil2")
			Profiles_popup.OnEnable()
		elseif self.Name == "Select_3_Profiles" then
--			Bg_1_Profiles.SetFrame(0)
--			Bg_2_Profiles.SetFrame(0)
--			Bg_3_Profiles.SetFrame(1)
			ProfilConfig.Set("CurrentProfil",3)
			-- Sauvegarde
			SaveManager.saveProfile()
			SaveManager.saveGame("Profil3")
			Profiles_popup.OnEnable()
		end
end

function OnMouseOn()
	if not self.PrivateCanClickOn() then
		return
	end	
	if self.Name == "Profiles_select_new" 
	or self.Name == "Profiles_select_ok" 
	or self.Name == "New_Profiles_cancel" 
	or self.Name == "New_Profiles_ok" then
	self.SetFrame(1)	
	end
	
	return true
end

function OnMouseOff()
if self.Name == "Profiles_select_new" or self.Name == "Profiles_select_ok" or self.Name == "New_Profiles_cancel" or self.Name == "New_Profiles_ok" then
	self.SetFrame(0)
end	
	return true
end

function PrivateCanClickOn()
	 
	if  IsEqualVariables("CurrentGamePhase","Document") then
		return false
	end
	if self.Name == "New_Profiles_ok" and Enter_Name.Enabled == false then
		return false
	end
	if (self.Name == "Delete_1_Profiles" or self.Name == "Select_1_Profiles") and  Menu_Start.NbProfilesCount() == 1 then
		return false
	end
	
	return true
end