.namespace Profiles_popup
g_profiletodelete = 0

t_profiles = {}
function OnEnable()
	-------Localization-------
	Profiles_title.Text = GlobalFunctions.GetText("menu_profiles")
	desc_profiles.Text = GlobalFunctions.GetText("menu_profiles_desc")
	Delete_1_txt.Text = GlobalFunctions.GetText("menu_profiles_delete")
	Delete_2_txt.Text = GlobalFunctions.GetText("menu_profiles_delete")
	Delete_3_txt.Text = GlobalFunctions.GetText("menu_profiles_delete")
	Profiles_select_new_txt.Text = GlobalFunctions.GetText("menu_profiles_new")
	Profiles_select_ok_txt.Text = GlobalFunctions.GetText("btn_ok")
	--------------------------
	local nbprofils = Menu_Start.NbProfilesCount()
	AssertPopup(nbprofils <= 3,"Erreur : trop de profil créés")
	
			-- if numprofil ~= ProfilConfig.Get("CurrentProfil") then
				-- ProfilConfig.Set("CurrentProfil",numprofil)
				-- Menu_titre.g_needloadgame = true
			-- end
	OPLog.Log("nb profils = "..nbprofils)
	j=0
	Bg_1_Profiles.Enabled = false
	Bg_2_Profiles.Enabled = false
	Bg_3_Profiles.Enabled = false
	for i=1,3 do
		OPLog.Log("Profil"..i)
		if ProfilConfig.Get("Profil"..i) == "Created" then
			j=j+1
			OPUnitFactory.GetUnit("Bg_"..j.."_Profiles").Enabled = true
			OPUnitFactory.GetUnit("Bg_"..j.."_Profiles").SetFrame(0)
			OPUnitFactory.GetUnit("Name_"..i.."_Profiles").Color.r = 160
			OPUnitFactory.GetUnit("Name_"..i.."_Profiles").Color.g = 160
			OPUnitFactory.GetUnit("Name_"..i.."_Profiles").Color.b = 160
			OPUnitFactory.GetUnit("Delete_"..i.."_Profiles").SetFrame(1)
			OPUnitFactory.GetUnit("Delete_"..i.."_Profiles").SetFrame(1)
			OPUnitFactory.GetUnit("Delete_"..i.."_Profiles").SetFrame(1)
			if i == ProfilConfig.Get("CurrentProfil") then
				OPUnitFactory.GetUnit("Bg_"..i.."_Profiles").SetFrame(1)
				OPUnitFactory.GetUnit("Name_"..i.."_Profiles").Color.r = 255
				OPUnitFactory.GetUnit("Name_"..i.."_Profiles").Color.g = 255
				OPUnitFactory.GetUnit("Name_"..i.."_Profiles").Color.b = 255
				OPUnitFactory.GetUnit("Delete_"..i.."_Profiles").SetFrame(0)
			end
			OPUnitFactory.GetUnit("Name_"..j.."_Profiles").Text = ProfilConfig.Get("Profil"..i.."_name")
			t_profiles[j] = i
			OPLog.Log("Profil chargé : "..ProfilConfig.Get("Profil"..i.."_name"))
		end
		
		
	end
	if nbprofils == 1 then
		Delete_1_Profiles.Flags_Visible = false
	else
		Delete_1_Profiles.Flags_Visible = true	
	end
end

function OnPopupYNLeave(popupansw, message)
	if message == "msg_ig_suppression_profil" and popupansw then
				curseur.Flags_Visible = false
			local nbprofiles = Menu_Start.NbProfilesCount()
			
			if g_profiletodelete ~= nbprofiles then
				for i=g_profiletodelete,nbprofiles do
					OPLog.Log("On décale")
					ProfilConfig.Set("Profil"..(i),ProfilConfig.Get("Profil"..(i+1)))
					ProfilConfig.Set("Profil"..(i).."_lastscreen",ProfilConfig.Get("Profil"..(i+1).."_lastscreen"))
					ProfilConfig.Set("Profil"..(i).."_name",ProfilConfig.Get("Profil"..(i+1).."_name"))
				end
			else			
				ProfilConfig.Set("Profil"..(Profiles_popup.t_profiles[g_profiletodelete]),"None")
				ProfilConfig.Set("Profil"..(Profiles_popup.t_profiles[g_profiletodelete]).."_lastscreen","")
				ProfilConfig.Set("Profil"..(Profiles_popup.t_profiles[g_profiletodelete]).."_name","")
			end
			ProfilConfig.Set("CurrentProfil",1)
			-- Sauvegarde
			SaveManager.saveGame("Profil1")
			SaveManager.saveProfile()
			-- Enlève la popup de suppression de Profil
			g_profiletodelete = 0
			-- Recharge la fenetre de profil
			Profiles_popup.OnEnable()
	end
end	