Citizen.CreateThread(function()
	Items = exports.spyoo_inventory:getItems()
	Objects = {}
	for item, value in pairs(Config.Object) do
		Objects[item] = {}
		for k, v in pairs(value.spawnPoint) do
			for i = 1, v.amount do
				local pointX = v.coords.x
				local pointY = v.coords.y
				if (math.random(2) == 1) then
					pointX = pointX + math.random(v.range);
				else
					pointX = pointX - math.random(v.range);
				end
				if (math.random(2) == 1) then
					pointY = pointY + math.random(v.range);
				else
					pointY = pointY - math.random(v.range);
				end
				local randomPoint = vector3(pointX, pointY, v.coords.z)
				local health = value.objectHealth[math.random(1, #value.objectHealth)]
				table.insert(Objects[item], { item = item, health = health, coords = randomPoint, spawnPoint = k, breakable = true })
			end
		end
	end
end)

RegisterServerEvent("spyoo_object:getObjects")
AddEventHandler("spyoo_object:getObjects", function()
	local _source = source
	TriggerClientEvent("spyoo_object:createObjects", _source, Objects)
end)

RegisterServerEvent("spyoo_object:damageObject")
AddEventHandler("spyoo_object:damageObject", function(breakObject, tablePos, itemUse)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	Objects[breakObject][tablePos].health = Objects[breakObject][tablePos].health - Config.Tool[itemUse][breakObject].damage
	if Objects[breakObject][tablePos].health == 0 then
		local skill = Config.Tool[itemUse][breakObject].skill
		if skill then
			skillInfo = exports.spyoo_skill:getSkillInfo(skill)
			boostInfo = exports.spyoo_skill:getBoost(identifier, skill)
		end
		for k,v in pairs(Config.Object[breakObject].win) do
			local random = math.random(0, 1000)
			local firstPerc = v.perc[1]
			local secondPerc = v.perc[2]
			if skillInfo[v.item] and skillInfo[v.item].dropChance then
				local add = boostInfo[skillInfo[v.item].dropChance]
				firstPerc = firstPerc - (add / 2)
				secondPerc = secondPerc + (add / 2)
			end

			if random >= firstPerc and random <= secondPerc then
				local quantity = math.random(v.min, v.max)
				if skillInfo[v.item] and skillInfo[v.item].lootBonus then
					local add = boostInfo[skillInfo[v.item].lootBonus]
					quantity = quantity + math.random(add[1], add[2])
				end				
				TriggerEvent("spyoo_inventory:addItem", _source, v.item, quantity, v.data, true)
			end
		end
		local spawnPoint = Objects[breakObject][tablePos].spawnPoint
		Objects[breakObject][tablePos].breakable = Config.Object[breakObject].spawnPoint[spawnPoint].timeRespawn[math.random(1, #Config.Object[breakObject].spawnPoint[spawnPoint].timeRespawn)]
		TriggerClientEvent("spyoo_object:deleteObject", -1, breakObject, tablePos)
	
		if Config.Object[breakObject].skill then
			for k,v in pairs(Config.Object[breakObject].skill) do
				TriggerEvent("spyoo_skill:earnExp", identifier, v.skill, math.random(v.rewardMin, v.rewardMax))
			end
		end
	
	else
		TriggerClientEvent("spyoo_object:updateObject", -1, breakObject, tablePos, "health", Objects[breakObject][tablePos].health)
	end
end)

Citizen.CreateThread(function()
	while true do
		Wait(1000)
		for item, value in pairs(Objects) do
			for k, info in pairs(value) do
				if info.breakable ~= true then
					Objects[item][k].breakable = Objects[item][k].breakable - 1000
					if Objects[item][k].breakable == 0 then
						respawnObject(item, k)
					end
				end
			end
		end
	end
end)

function respawnObject(object, pos)
	local spawnPoint = Objects[object][pos].spawnPoint
	local coords = Config.Object[object].spawnPoint[spawnPoint].coords
	local range = Config.Object[object].spawnPoint[spawnPoint].range
	local pointX = coords.x
	local pointY = coords.y
	if (math.random(2) == 1) then
		pointX = pointX + math.random(range);
	else
		pointX = pointX - math.random(range);
	end
	if (math.random(2) == 1) then
		pointY = pointY + math.random(range);
	else
		pointY = pointY - math.random(range);
	end
	local randomPoint = vector3(pointX, pointY, coords.z)
	local health = Config.Object[object].objectHealth[math.random(1, #Config.Object[object].objectHealth)]
	Objects[object][pos].health = health
	Objects[object][pos].coords = randomPoint
	Objects[object][pos].breakable = true
	TriggerClientEvent("spyoo_object:respawnObject", -1, Objects[object][pos], object, pos)
end

RegisterServerEvent("spyoo_object:timeToBreak")
AddEventHandler("spyoo_object:timeToBreak", function(item, posItem, findBreakItem, findBreakItemPos, data)
	local _source = source
	local identifier = exports.spyoo_base:getSteamId(_source)
	local skill = Config.Tool[item][findBreakItem].skill
	local baseWait = Config.Object[findBreakItem].timeToBreak
	local boost = exports.spyoo_skill:getBoost(identifier, skill)
	local timer = baseWait - boost.speedBreaking
	
	TriggerClientEvent("spyoo_object:break", _source, item, posItem, findBreakItem, findBreakItemPos, data, timer)
end)