local _weather = nil

local function RequestIplAsync(iplName)
	RequestIpl(iplName)
	Citizen.Wait(0)
end

local function RemoveIplAsync(iplName)
	RemoveIpl(iplName)
	Citizen.Wait(0)
end

RegisterNetEvent('lsv:updateWeather')
AddEventHandler('lsv:updateWeather', function(weather)
	if _weather == weather then
		return
	end

	_weather = weather

	SetWeatherTypeOverTime(_weather, 30.0)
	Citizen.Wait(30000)
	SetWeatherTypeNowPersist(_weather)
end)

Citizen.CreateThread(function()
	World.AddVehicleHandler(function(vehicle)
		if table.ifind(Settings.world.blacklistVehicles, GetEntityModel(vehicle)) then
			if NetworkGetEntityIsNetworked(vehicle) then
				Network.DeleteVehicle(VehToNet(vehicle))
			else
				World.DeleteEntity(vehicle)
			end
		end
	end)

	World.AddVehicleHandler(function(vehicle)
		if NetworkGetEntityIsNetworked(vehicle) and GetVehicleEngineHealth(vehicle) <= -4000 then
			Network.DeleteVehicle(VehToNet(vehicle), 10000)
		end
	end)
end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)

		SetPedDensityMultiplierThisFrame(Settings.density.ped)
		SetScenarioPedDensityMultiplierThisFrame(Settings.density.ped, Settings.density.ped)

		SetParkedVehicleDensityMultiplierThisFrame(Settings.density.vehicle)
		SetRandomVehicleDensityMultiplierThisFrame(Settings.density.vehicle)
		SetVehicleDensityMultiplierThisFrame(Settings.density.vehicle)
	end
end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1000)

		DisablePlayerVehicleRewards(PlayerId())
		NetworkOverrideClockTime(NetworkGetServerTime())
	end
end)

Citizen.CreateThread(function()
	RequestIplAsync('chop_props')
	RequestIplAsync('FIBlobby')
	RemoveIplAsync('FIBlobbyfake')
	RequestIplAsync('FBI_colPLUG')
	RequestIplAsync('FBI_repair')
	RequestIplAsync('v_tunnel_hole')
	RequestIplAsync('TrevorsMP')
	RequestIplAsync('TrevorsTrailer')
	RequestIplAsync('TrevorsTrailerTidy')
	RemoveIplAsync('farm_burnt')
	RemoveIplAsync('farm_burnt_lod')
	RemoveIplAsync('farm_burnt_props')
	RemoveIplAsync('farmint_cap')
	RemoveIplAsync('farmint_cap_lod')
	RequestIplAsync('farm')
	RequestIplAsync('farmint')
	RequestIplAsync('farm_lod')
	RequestIplAsync('farm_props')
	RequestIplAsync('facelobby')
	RemoveIplAsync('CS1_02_cf_offmission')
	RequestIplAsync('CS1_02_cf_onmission1')
	RequestIplAsync('CS1_02_cf_onmission2')
	RequestIplAsync('CS1_02_cf_onmission3')
	RequestIplAsync('CS1_02_cf_onmission4')
	RequestIplAsync('v_rockclub')
	RequestIplAsync('v_janitor')
	RemoveIplAsync('hei_bi_hw1_13_door')
	RequestIplAsync('bkr_bi_hw1_13_int')
	RequestIplAsync('ufo')
	RequestIplAsync('ufo_lod')
	RequestIplAsync('ufo_eye')
	RemoveIplAsync('v_carshowroom')
	RemoveIplAsync('shutter_open')
	RemoveIplAsync('shutter_closed')
	RemoveIplAsync('shr_int')
	RequestIplAsync('csr_afterMission')
	RequestIplAsync('v_carshowroom')
	RequestIplAsync('shr_int')
	RequestIplAsync('shutter_closed')
	RequestIplAsync('smboat')
	RequestIplAsync('smboat_distantlights')
	RequestIplAsync('smboat_lod')
	RequestIplAsync('smboat_lodlights')
	RequestIplAsync('cargoship')
	RequestIplAsync('railing_start')
	RemoveIplAsync('sp1_10_fake_interior')
	RemoveIplAsync('sp1_10_fake_interior_lod')
	RequestIplAsync('sp1_10_real_interior')
	RequestIplAsync('sp1_10_real_interior_lod')
	RemoveIplAsync('id2_14_during_door')
	RemoveIplAsync('id2_14_during1')
	RemoveIplAsync('id2_14_during2')
	RemoveIplAsync('id2_14_on_fire')
	RemoveIplAsync('id2_14_post_no_int')
	RemoveIplAsync('id2_14_pre_no_int')
	RemoveIplAsync('id2_14_during_door')
	RequestIplAsync('id2_14_during1')
	RemoveIplAsync('Coroner_Int_off')
	RequestIplAsync('coronertrash')
	RequestIplAsync('Coroner_Int_on')
	RemoveIplAsync('bh1_16_refurb')
	RemoveIplAsync('jewel2fake')
	RemoveIplAsync('bh1_16_doors_shut')
	RequestIplAsync('refit_unload')
	RequestIplAsync('post_hiest_unload')
	RequestIplAsync('Carwash_with_spinners')
	RequestIplAsync('KT_CarWash')
	RequestIplAsync('ferris_finale_Anim')
	RemoveIplAsync('ch1_02_closed')
	RequestIplAsync('ch1_02_open')
	RequestIplAsync('AP1_04_TriAf01')
	RequestIplAsync('CS2_06_TriAf02')
	RequestIplAsync('CS4_04_TriAf03')
	RemoveIplAsync('scafstartimap')
	RequestIplAsync('scafendimap')
	RemoveIplAsync('DT1_05_HC_REMOVE')
	RequestIplAsync('DT1_05_HC_REQ')
	RequestIplAsync('DT1_05_REQUEST')
	RequestIplAsync('FINBANK')
	RemoveIplAsync('DT1_03_Shutter')
	RemoveIplAsync('DT1_03_Gr_Closed')

	RequestIplAsync('golfflags')
	RequestIplAsync('airfield')
	RequestIplAsync('v_garages')
	RequestIplAsync('v_foundry')
	RequestIplAsync('hei_yacht_heist')
	RequestIplAsync('hei_yacht_heist_Bar')
	RequestIplAsync('hei_yacht_heist_Bedrm')
	RequestIplAsync('hei_yacht_heist_Bridge')
	RequestIplAsync('hei_yacht_heist_DistantLights')
	RequestIplAsync('hei_yacht_heist_enginrm')
	RequestIplAsync('hei_yacht_heist_LODLights')
	RequestIplAsync('hei_yacht_heist_Lounge')

	RequestIplAsync('hei_carrier')
	RequestIplAsync('hei_Carrier_int1')
	RequestIplAsync('hei_Carrier_int2')
	RequestIplAsync('hei_Carrier_int3')
	RequestIplAsync('hei_Carrier_int4')
	RequestIplAsync('hei_Carrier_int5')
	RequestIplAsync('hei_Carrier_int6')
	RequestIplAsync('hei_carrier_LODLights')

	RequestIplAsync('bkr_bi_id1_23_door')
	RequestIplAsync('lr_cs6_08_grave_closed')
	RequestIplAsync('hei_sm_16_interior_v_bahama_milo_')
	RequestIplAsync('CS3_07_MPGates')
	RequestIplAsync('cs5_4_trains')
	RequestIplAsync('v_lesters')
	RequestIplAsync('v_trevors')
	RequestIplAsync('v_michael')
	RequestIplAsync('v_comedy')
	RequestIplAsync('v_cinema')
	RequestIplAsync('V_Sweat')
	RequestIplAsync('V_35_Fireman')

	RequestIplAsync('redCarpet')
	RequestIplAsync('triathlon2_VBprops')
	RequestIplAsync('jetstealtunnel')
	RequestIplAsync('Jetsteal_ipl_grp1')

	RequestIplAsync('v_hospital')
	RemoveIplAsync('RC12B_Default')
	RemoveIplAsync('RC12B_Fixed')
	RequestIplAsync('RC12B_Destroyed')
	RequestIplAsync('RC12B_HospitalInterior')
end)
