skins = {
	{
		name = "mp_f_freemode_01",
		display = "Femme"
	},
	{
		name = "mp_m_freemode_01",
		display = "Homme"
	}
}

RegisterNetEvent("skin_customization:Customization")

function InitMenu()
	ClearMenu()
	Menu.addTitle("Choisir un skin");
   	Menu.addButton(skins[2].display, "SendSkin", skins[2].name);
	Menu.addButton(skins[1].display, "SendSkin", skins[1].name);
end

function SendSkin(skin)
    TriggerServerEvent("skin_customization:ChoosenSkin",skin)
end

--Notification joueur
function Notify(text)
    SetNotificationTextEntry('STRING')
    AddTextComponentString(text)
    DrawNotification(false, false)
end

AddEventHandler("skin_customization:Customization",function(skin)
    ChangeSkin(skin,components)
	Notify("Skin chargée")
	InitDrawMenu()
end)

RegisterNetEvent("skin_customization:updateComponents")
AddEventHandler("skin_customization:updateComponents",function(args)
		ChangeComponent({0,0,args[1],args[2]})-- 1:componentID; 2: page; 3: drawbleID; 4: textureID
		ChangeComponent({2,0,args[3],args[4]})
		ChangeComponent({4,0,args[5],args[6]})
		ChangeComponent({6,0,args[7],args[8]})
		ChangeComponent({11,0,args[9],args[10]})
		ChangeComponent({8,0,args[11],args[12]})
		Notify("Skin et composants chargés")
end)

function InitDrawMenu()
	ClearMenu()
	Menu.addButton("Visage","DrawableChoice",{0,0})
	Menu.addButton("Cheveux","DrawableChoice",{2,0})
	Menu.addButton("Torse","DrawableChoice",{11,0})
	Menu.addButton("T-shirt","DrawableChoice",{8,0})
	Menu.addButton("Pantalon","DrawableChoice",{4,0})
	Menu.addButton("Chaussures","DrawableChoice",{6,0})
end

function DrawableChoice(args)
	ClearMenu()
	local max = GetNumberOfPedDrawableVariations(GetPlayerPed(-1), args[1])

	if max > (args[2]+1)*8 then
		max = (args[2]+1)*8
	end
	if args[2] > 0 then
		Menu.addButton("Last Page","DrawableChoice",{args[1],args[2]-1})
	end
	for i = args[2]*8,max-1 do				
		Menu.addButton("Draw: "..i,"TextureChoice",{args[1],0,i})
	end
	if max <= (args[2]+1)*8 then
		Menu.addButton("Next","DrawableChoice",{args[1],args[2]+1})
	end
	Menu.addButton("Back","InitDrawMenu","")
end

function TextureChoice(args)
	ClearMenu()
	local max = GetNumberOfPedTextureVariations(GetPlayerPed(-1),args[1],args[3])
	if max > (args[2]+1)*8 then
		max = (args[2]+1)*8
	end
	if args[2] > 0 then
		Menu.addButton("Last page","TextureChoice",{args[1],args[2]-1,args[3]})
	end
	for i = args[2]*8,max-1 do
		--if IsPedComponentVariationValid(GetPlayerPed(-1), args[3], i, 2) then
			Menu.addButton("Text: "..i,"ChangeComponent",{args[1],args[2],args[3],i})
		--end
	end
	if max <= (args[2]+1)*8 then
		Menu.addButton("Next page","TextureChoice",{args[1],args[2]+1,args[3]})
	end
	Menu.addButton("Back","DrawableChoice",args)
end

function ChangeComponent(args)-- 1:componentID; 2: page; 3: drawbleID; 4: textureID
	SetPedComponentVariation(GetPlayerPed(-1), args[1], args[3], args[4], 2)
	TriggerServerEvent("skin_customization:SaveComponents",args)
end

--Action lors du spawn du joueur
local firstspawn = 0
AddEventHandler('playerSpawned', function(spawn)
--On verifie que c'est bien le premier spawn du joueur
if firstspawn == 0 then
	TriggerServerEvent("skin_customization:SpawnPlayer")
	firstspawn = 1
end
end)

function ChangeSkin(skin,components)
	--Menu.hidden = true
	-- Get model hash.
	local modelhashed = GetHashKey(skin)
    
    -- Request the model, and wait further triggering untill fully loaded.
	RequestModel(modelhashed)
	while not HasModelLoaded(modelhashed) do 
	    RequestModel(modelhashed)
	    Citizen.Wait(0)
	end
    -- Set playermodel.
	SetPlayerModel(PlayerId(), modelhashed)

	if components == nil then
		local playerPed = GetPlayerPed(-1)
	 	--SET_PED_COMPONENT_VARIATION(Ped ped, int componentId, int drawableId, int textureId, int paletteId)
		 --SetPedComponentVariation(playerPed, 0, 0, 0, 2) --Face
		 --SetPedComponentVariation(playerPed, 2, 11, 4, 2) --Hair 
		 --SetPedComponentVariation(playerPed, 4, 1, 5, 2) -- Pantalon
		 --SetPedComponentVariation(playerPed, 6, 1, 0, 2) -- Shoes
		 --SetPedComponentVariation(playerPed, 11, 7, 2, 2) -- Jacket
		 TriggerServerEvent("skin_customization:ChoosenComponents")
	end
	
	SetModelAsNoLongerNeeded(modelhashed)
end

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		if IsControlJustPressed(1, 167) then -- INPUT_CELLPHONE_DOWN
			InitMenu()                       
			Menu.hidden = not Menu.hidden    
		end
		Menu.renderGUI()
	end
end)