Citizen.CreateThread(function()
	Items = exports.spyoo_inventory:getItems()
	Objects = {}
	Wait(1000)
	TriggerServerEvent("spyoo_object:getObjects")
end)

Citizen.CreateThread(function()
	for key, value in pairs(Config.Object) do
		for k,v in pairs(value.spawnPoint) do
			local blip = AddBlipForRadius(v.coords.x, v.coords.y, v.coords.z , v.range) -- you can use a higher number for a bigger zone

			SetBlipHighDetail(blip, true)
			SetBlipColour(blip, 3)
			SetBlipAlpha (blip, 100)
			SetBlipDisplay(blip, 3)

			local blip = AddBlipForCoord(v.coords.x, v.coords.y, v.coords.z)
			SetBlipSprite(blip, 148)
			SetBlipScale(blip, 0.3)
			SetBlipColour(blip, 3)
			SetBlipAsShortRange(blip, true)
			SetBlipDisplay(blip, 3)

			BeginTextCommandSetBlipName("STRING")
			AddTextComponentString(value.label)
			EndTextCommandSetBlipName(blip)
		end
	end
end)

RegisterNetEvent("spyoo_object:createObjects")
AddEventHandler("spyoo_object:createObjects", function(srvObjects)
	for item, value in pairs(srvObjects) do
		Objects[item] = {}
		for key, info in pairs(value) do
			local modelHash = Config.Object[item].objectName[math.random(1, #Config.Object[item].objectName)]
			if not HasModelLoaded(modelHash) then
				RequestModel(modelHash)
				while not HasModelLoaded(modelHash) do
					Citizen.Wait(1)
				end
			end

			
		
			obj = CreateObjectNoOffset(modelHash, info.coords, false, true)
			PlaceObjectOnGroundProperly(obj)
			FreezeEntityPosition(obj, true)
			table.insert(Objects[item], {item = item, health = info.health, coords = info.coords, breakable = info.breakable, object = obj})
		end
	end
end)

RegisterNetEvent("spyoo_object:respawnObject")
AddEventHandler("spyoo_object:respawnObject", function(srvObject, object, pos)
	local modelHash = Config.Object[object].objectName[math.random(1, #Config.Object[object].objectName)]
	if not HasModelLoaded(modelHash) then
		RequestModel(modelHash)
		while not HasModelLoaded(modelHash) do
			Citizen.Wait(1)
		end
	end

	obj = CreateObjectNoOffset(modelHash, srvObject.coords, true)
	PlaceObjectOnGroundProperly(obj)
	FreezeEntityPosition(obj, true)

	Objects[object][pos].object = obj
	Objects[object][pos].health = srvObject.health
	Objects[object][pos].coords = srvObject.coords
	Objects[object][pos].breakable = true
end)

RegisterNetEvent("spyoo_object:updateObject")
AddEventHandler("spyoo_object:updateObject", function(breakObject, tablePos, key, value)
	Objects[breakObject][tablePos][key] = value
end)

Citizen.CreateThread(function()
	while true do
		Wait(1)
		for key, value in pairs(Objects) do
			for k,v in pairs(value) do
				if GetDistanceBetweenCoords(pCoords, v.coords, true) < 10 and v.breakable then
					marker(v.coords, Config.Object[v.item].label)
				end
			end
		end
	end
end)

RegisterNetEvent("spyoo_object:use")
AddEventHandler("spyoo_object:use", function(item, data, posItem)
	local coords = GetEntityCoords(GetPlayerPed(-1))
	local findBreakItem, findBreakItemPos = false
	for breakItem, value in pairs(Config.Tool[item]) do
		for k,v in pairs(Objects[breakItem]) do
			if GetDistanceBetweenCoords(coords, v.coords, true) < 8.0 and v.breakable then
				findBreakItem = breakItem
				findBreakItemPos = k
				break
			end
		end
	end

	if findBreakItem then
		TriggerServerEvent("spyoo_object:timeToBreak", item, posItem, findBreakItem, findBreakItemPos, data)
	else
		TriggerEvent("spyoo_notif:notif", { notif = "Il n'y a rien à casser à proximité avec cet outil" })
	end
end)

RegisterNetEvent("spyoo_object:break")
AddEventHandler("spyoo_object:break", function(item, posItem, findBreakItem, findBreakItemPos, data, timer)
	FreezeEntityPosition(player, true)
	Wait(timer)
	FreezeEntityPosition(player, false)
	TriggerServerEvent("spyoo_object:damageObject", findBreakItem, findBreakItemPos, item)
	local newDura = data.durability - Config.Tool[item][findBreakItem].durabilityLose
	if newDura <= 0 then
		TriggerServerEvent("spyoo_inventory:removeItem", "me", item, 1, posItem, true)
	else
		TriggerServerEvent("spyoo_inventory:setData", item, posItem, "durability", newDura)
	end
end)

RegisterNetEvent("spyoo_object:deleteObject")
AddEventHandler("spyoo_object:deleteObject", function(object, tablePos)
	DeleteObject(Objects[object][tablePos].object)
	Objects[object][tablePos].breakable = false
end)