local Locked = {
	
}

RegisterCommand(Config.LockCommand, function(source, args, rawCommand)
		local unlock = false

		for _,v in ipairs(Config.Doors) do -- Loop through doors
			local x,y,z = table.unpack(GetEntityCoords(GetPlayerPed(-1)))
			local closest = GetClosestObjectOfType(x, y, z, 2.5, v, false, false, false)

			for index,value in ipairs(Locked) do
				local closest2 = GetClosestObjectOfType(value[2], value[3], value[4], 2.5, value[1], false, false, false)
				local distance = Vdist2(GetEntityCoords(GetPlayerPed(-1)), GetEntityCoords(closest2))

				if distance < 2.5 and value[1] == v then
					print('Unlocking')
					unlock = true
					TriggerEvent('MOE:AdvancedNotification', 'CHAR_PROPERTY_BAR_UNBRANDED', 4, 'Method of Entry', 'Infinity Studios', 'Door Unlocked!')
					TriggerServerEvent('MOE:UnlockServer', v, x, y, z, index)
					break
				end
			end

			if IsEntityAnObject(closest) and not unlock then
				
				TriggerEvent('MOE:AdvancedNotification', 'CHAR_PROPERTY_BAR_UNBRANDED', 4, 'Method of Entry', 'Infinity Studios', 'Door Locked!')
				TriggerServerEvent('MOE:LockServer', v, x, y, z)

			end
			
		end
end, false)

	----------------
	--[[ Events ]]--
	----------------

RegisterNetEvent('MOE:LockClient')
AddEventHandler('MOE:LockClient', function(object, x, y, z)

	local closest = GetClosestObjectOfType(x, y, z, 2.5, object, false, false, false)

	SetEntityHeading(closest, 180.0)
	FreezeEntityPosition(closest, true)

	table.insert(Locked, {object, x, y, z})

end)

RegisterNetEvent('MOE:UnlockClient')
AddEventHandler('MOE:UnlockClient', function(object, x, y, z, ind)

	local closest = GetClosestObjectOfType(x, y, z, 2.5, object, false, false, false)

	SetEntityHeading(closest, 180.0)
	FreezeEntityPosition(closest, false)

	table.remove(Locked, ind)

end)

RegisterNetEvent('MOE:EnforcerClient')
AddEventHandler('MOE:EnforcerClient', function(object, x, y, z, ind)

	local closest = GetClosestObjectOfType(x, y, z, 2.5, object, false, false, false)

	PlaySoundFromEntity(GetSoundId(), "CRASH", closest, "PAPARAZZO_03A", 0, 0)
	Citizen.Wait(500)
	PlaySoundFromEntity(GetSoundId(), "CRASH", closest, "PAPARAZZO_03A", 0, 0)
	Citizen.Wait(750)
	FreezeEntityPosition(closest, false)

	table.remove(Locked, ind)

end)



	--------------------------------
	--[[ Enforcer Functionality ]]--
	--------------------------------

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


	  local doorNear
	  local doorInd

	  	--[[ Lock Doors in range ]]--
		for k,v in pairs(Locked) do

			local closest = GetClosestObjectOfType(v[2], v[3], v[4], 2.5, v[1], false, false, false)

			--[[ If explosion in area... ]]--
			if IsExplosionInSphere(-1, v[2], v[3], v[4], 1.5) then
				TriggerServerEvent('MOE:UnlockServer', v[1], v[2], v[3], v[4], k)
			end

			local distance = Vdist2(GetEntityCoords(GetPlayerPed(-1)), GetEntityCoords(closest))

			if distance < 2.0 then
				SetEntityHeading(closest, 180.0)
				FreezeEntityPosition(closest, true)
				doorNear = v
				doorInd = k
			end
		end
	  
	  	--[[ If the enforcer is equipped... ]]--
	  	if GetSelectedPedWeapon(GetPlayerPed(-1)) == GetHashKey(Config.EnforcerModel) then


			DisableControlAction(0,24,true)
			DisableControlAction(0,25,true)
			DisableControlAction(0,140,true)

			if IsDisabledControlJustPressed(0, 24) and GetLastInputMethod( 0 ) then -- If left clicked

				if doorNear then
					-- Break down door!
					TriggerEvent('MOE:AdvancedNotification', 'CHAR_PROPERTY_BAR_UNBRANDED', 4, 'Method of Entry', 'Infinity Studios', 'Hitting Door...')
					TriggerServerEvent('MOE:EnforcerServer', doorNear[1], doorNear[2], doorNear[3], doorNear[4], doorInd)
					Citizen.Wait(1250)
					TriggerEvent('MOE:AdvancedNotification', 'CHAR_PROPERTY_BAR_UNBRANDED', 4, 'Method of Entry', 'Infinity Studios', 'Door Opened.')
				end

			end


		end

		--[[ If the shotgun is equipped... ]]--
		if GetSelectedPedWeapon(GetPlayerPed(-1)) == GetHashKey(Config.BreachingShotgun) and doorNear ~= nil then

			if IsPlayerFreeAiming(PlayerId()) then -- If player is aiming
				local _, entityAim = GetEntityPlayerIsFreeAimingAt(PlayerId())
				if IsPedShooting(GetPlayerPed(-1)) then
					if GetEntityModel(entityAim) == GetHashKey(doorNear[1]) then
						-- Break down door!
						TriggerServerEvent('MOE:EnforcerServer', doorNear[1], doorNear[2], doorNear[3], doorNear[4], doorInd)
						TriggerEvent('MOE:AdvancedNotification', 'CHAR_PROPERTY_BAR_UNBRANDED', 4, 'Method of Entry', 'Infinity Studios', 'Door Opened.')
					end
				end
			end

		end
  	end
end)

	-------------------
	--[[ Functions ]]--
	-------------------

RegisterNetEvent("MOE:AdvancedNotification")
AddEventHandler("MOE:AdvancedNotification", function(icon, type, sender, title, text)
    Citizen.CreateThread(function()
        Wait(1)
        SetNotificationTextEntry("STRING")
        AddTextComponentString(text)
        SetNotificationMessage(icon, icon, true, type, sender, title, text)
        DrawNotification(false, true)
        Citizen.Wait(1000)
    end)
end)

function tablelength(T)
	local count = 0
	for _ in pairs(T) do count = count + 1 end
	return count
end