-----------------------------------------------------------------------------------------------------------------------------------------
-- VARIABLES
-----------------------------------------------------------------------------------------------------------------------------------------
local Blips = {}
local InitSounds = {}
local ScannerTable = {}
local SoundScanner = 999
local InitScanner = false
-----------------------------------------------------------------------------------------------------------------------------------------
-- SCANNERCOORDS
-----------------------------------------------------------------------------------------------------------------------------------------
local ScannerCoords = {
	{ -1811.94, -968.5,  1.72,  357.17 },
	{ -1788.29, -958.0,  3.35,  328.82 },
	{ -1756.9,  -942.98, 6.91,  311.82 },
	{ -1759.97, -910.12, 7.58,  334.49 },
	{ -1791.44, -904.11, 5.12,  39.69 },
	{ -1827.87, -900.32, 2.48,  68.04 },
	{ -1840.76, -882.39, 2.81,  51.03 },
	{ -1793.4,  -819.9,  7.73,  328.82 },
	{ -1737.69, -791.3,  9.44,  317.49 },
	{ -1714.6,  -784.61, 9.82,  306.15 },
	{ -1735.88, -757.92, 10.11, 2.84 },
	{ -1763.22, -764.65, 9.49,  65.2 },
	{ -1786.23, -782.4,  8.71,  99.22 },
	{ -1809.5,  -798.29, 7.89,  104.89 },
	{ -1816.35, -827.68, 6.44,  141.74 },
	{ -1833.23, -856.58, 3.52,  147.41 },
	{ -1842.88, -869.45, 2.98,  144.57 },
	{ -1865.34, -858.4,  2.12,  99.22 },
	{ -1868.01, -835.58, 3.0,   51.03 },
	{ -1860.76, -810.59, 4.04,  8.51 },
	{ -1848.85, -790.99, 6.3,   348.67 },
	{ -1834.31, -767.03, 8.17,  337.33 },
	{ -1819.3,  -742.04, 8.85,  331.66 },
	{ -1804.76, -713.39, 9.76,  331.66 },
	{ -1805.32, -695.22, 10.23, 348.67 },
	{ -1824.32, -685.97, 10.23, 36.86 },
	{ -1849.16, -699.25, 9.45,  85.04 },
	{ -1868.9,  -716.3,  8.86,  110.56 },
	{ -1890.07, -736.57, 6.27,  124.73 },
	{ -1909.8,  -759.44, 3.52,  130.4 },
	{ -1920.19, -782.25, 2.8,   144.57 },
	{ -1939.84, -765.34, 1.99,  85.04 },
	{ -1932.96, -746.47, 3.05,  8.51 },
	{ -1954.69, -722.8,  3.03,  28.35 },
	{ -1954.53, -688.85, 4.06,  11.34 },
	{ -1939.8,  -651.94, 8.74,  351.5 },
	{ -1926.48, -627.37, 10.67, 348.67 },
	{ -1920.73, -615.67, 10.95, 340.16 },
	{ -1924.48, -596.03, 11.56, 51.03 },
	{ -1952.53, -597.0,  11.02, 70.87 },
	{ -1972.12, -609.32, 8.73,  102.05 },
	{ -1989.01, -629.48, 5.21,  124.73 },
	{ -2002.97, -659.48, 3.03,  141.74 },
	{ -2028.03, -658.61, 1.82,  107.72 },
	{ -2045.57, -637.91, 2.02,  65.2 },
	{ -2031.42, -618.65, 3.23,  0.0 },
	{ -2003.74, -603.38, 6.3,   328.82 },
	{ -1982.79, -588.43, 10.01, 317.49 },
	{ -1968.4,  -565.72, 11.42, 323.15 },
	{ -1980.98, -545.43, 11.58, 5.67 },
	{ -1996.36, -552.72, 11.68, 17.01 },
	{ -2013.33, -573.78, 8.95,  102.05 },
	{ -2030.05, -604.62, 3.93,  133.23 },
	{ -2035.79, -626.99, 3.0,   150.24 },
	{ -2053.05, -626.67, 2.31,  113.39 },
	{ -2062.58, -596.05, 3.03,  45.36 },
	{ -2096.8,  -579.13, 2.75,  53.86 },
	{ -2116.49, -559.09, 2.29,  48.19 },
	{ -2093.8,  -539.57, 3.74,  22.68 },
	{ -2067.11, -526.37, 6.98,  328.82 },
	{ -2049.71, -516.4,  9.13,  308.98 },
	{ -2029.87, -507.17, 11.49, 300.48 },
	{ -2049.27, -492.94, 11.17, 11.34 },
	{ -2073.38, -483.05, 9.13,  42.52 },
	{ -2102.99, -470.71, 6.52,  56.7 },
	{ -2119.62, -451.87, 6.67,  48.19 },
	{ -2134.43, -460.37, 5.24,  93.55 },
	{ -1805.06, -936.1,  2.53,  189.93 },
	{ -1786.4,  -932.99, 4.38,  269.3 },
	{ -1744.87, -926.96, 7.65,  269.3 },
	{ -1763.18, -925.72, 6.99,  104.89 },
	{ -1773.65, -895.76, 7.35,  357.17 },
	{ -1750.28, -883.7,  7.75,  277.8 },
	{ -1733.24, -862.29, 8.17,  311.82 },
	{ -1703.01, -838.56, 9.03,  300.48 },
	{ -1720.85, -834.19, 8.95,  31.19 },
	{ -1747.12, -839.86, 8.39,  138.9 },
	{ -1764.27, -856.95, 7.75,  147.41 },
	{ -1776.27, -868.44, 7.78,  119.06 },
	{ -1803.86, -872.72, 5.34,  93.55 },
	{ -1744.12, -901.55, 7.7,   79.38 },
	{ -1765.09, -808.12, 8.58,  31.19 },
	{ -1773.17, -728.07, 10.01, 8.51 },
	{ -1849.14, -729.09, 8.85,  136.07 },
	{ -1866.65, -758.66, 6.96,  150.24 },
	{ -1886.42, -794.12, 3.25,  158.75 },
	{ -1795.97, -748.07, 9.17,  297.64 },
	{ -1915.8,  -682.79, 8.0,   62.37 },
	{ -1911.86, -651.71, 10.26, 0.0 },
	{ -1899.29, -623.49, 11.34, 345.83 },
	{ -1847.11, -670.69, 10.45, 17.01 },
	{ -1874.69, -647.34, 10.92, 39.69 },
	{ -1958.9,  -629.78, 8.34,  73.71 },
	{ -1951.39, -575.59, 11.53, 343.0 },
	{ -1991.28, -569.55, 10.72, 164.41 },
	{ -2056.68, -569.32, 4.57,  99.22 },
	{ -2088.29, -560.62, 3.27,  87.88 },
	{ -2042.45, -542.51, 8.46,  308.98 },
	{ -2020.91, -528.58, 11.12, 306.15 },
	{ -2092.91, -499.58, 5.37,  79.38 },
	{ -2113.6,  -521.59, 2.27,  147.41 },
	{ -2139.09, -496.06, 2.27,  48.19 },
	{ -2122.44, -486.23, 3.56,  280.63 },
	{ -2034.27, -577.42, 6.74,  294.81 },
	{ -2003.72, -536.62, 11.78, 320.32 },
	{ -2023.41, -551.31, 9.59,  255.12 },
	{ -2014.0,  -626.04, 3.76,  189.93 },
	{ -1967.67, -656.53, 5.24,  255.12 },
	{ -1878.77, -672.36, 9.76,  257.96 },
	{ -1827.96, -702.26, 9.67,  240.95 },
	{ -1855.87, -771.67, 6.94,  164.41 },
	{ -1846.08, -830.98, 3.79,  175.75 },
	{ -1830.72, -804.5,  6.67,  334.49 },
	{ -1770.76, -835.92, 7.84,  311.82 },
	{ -1724.61, -812.2,  9.25,  303.31 },
	{ -1828.9,  -719.11, 9.3,   65.2 },
	{ -1893.81, -707.65, 7.73,  110.56 },
	{ -1921.84, -716.82, 5.22,  212.6 },
	{ -1891.7,  -756.03, 4.95,  218.27 },
	{ -1890.51, -818.0,  2.95,  303.31 },
	{ -1806.69, -770.32, 8.29,  306.15 },
	{ -1763.57, -747.01, 9.81,  303.31 },
	{ -1980.27, -691.34, 3.02,  189.93 }
}
-----------------------------------------------------------------------------------------------------------------------------------------
-- INVENTORY:UPDATESCANNER
-----------------------------------------------------------------------------------------------------------------------------------------
RegisterNetEvent("inventory:updateScanner")
AddEventHandler("inventory:updateScanner", function(Status)
	InitScanner = Status
	SoundScanner = 999
end)
-----------------------------------------------------------------------------------------------------------------------------------------
-- THREADSCANNER
-----------------------------------------------------------------------------------------------------------------------------------------
CreateThread(function()
    while true do
        local TimeDistance = 999

        if InitScanner then
            local Ped = PlayerPedId()
            if not IsPedInAnyVehicle(Ped) then
                local Coords = GetEntityCoords(Ped)

                for k, v in pairs(ScannerTable) do
                    local Distance = #(Coords - vec3(v[1], v[2], v[3]))
                    if Distance <= 7.25 then
                        SoundScanner = 1000

                        if not InitSounds[k] then
                            InitSounds[k] = true
                        end

                        if Distance <= 1.25 then
                            TimeDistance = 1
                            SoundScanner = 250

                            if IsControlJustPressed(1, 38) then
                                TriggerEvent("inventory:MakeProducts", "scanner")

                                local Rand = math.random(#ScannerCoords)
                                ScannerTable[k] = ScannerCoords[Rand]
                                InitSounds[k] = nil
                                SoundScanner = 999
                            end
                        end
                    else
                        if InitSounds[k] then
                            InitSounds[k] = nil
                            SoundScanner = 999
                        end
                    end
                end
            end
        end

        Wait(TimeDistance)
    end
end)
-----------------------------------------------------------------------------------------------------------------------------------------
-- THREADSCANNERSOUND
-----------------------------------------------------------------------------------------------------------------------------------------
CreateThread(function()
	while true do
		if InitScanner and (SoundScanner == 1000 or SoundScanner == 250) then
			PlaySoundFrontend(-1, "MP_IDLE_TIMER", "HUD_FRONTEND_DEFAULT_SOUNDSET")
		end

		Wait(SoundScanner)
	end

	local AmountCoords = 0
	repeat
		AmountCoords = AmountCoords + 1
		local Rand = math.random(#ScannerCoords)
		ScannerTable[AmountCoords] = ScannerCoords[Rand]
		Wait(1)
	until AmountCoords == 25
end)
-----------------------------------------------------------------------------------------------------------------------------------------
-- INVENTORY:SCANNERBLIPS
-----------------------------------------------------------------------------------------------------------------------------------------
RegisterNetEvent("inventory:ScannerBlips")
AddEventHandler("inventory:ScannerBlips", function()
	if json.encode(Blips) ~= "[]" then
		for _, v in pairs(Blips) do
			if DoesBlipExist(v) then
				RemoveBlip(v)
			end
		end

		Blips = {}

		TriggerEvent("Notify", "amarelo", "Marcações desativadas.", "Scanner", 5000)
	else
		for k, v in pairs(ScannerCoords) do
			Blips[k] = AddBlipForCoord(v[1], v[2], v[3])
			SetBlipSprite(Blips[k], 119)
			SetBlipDisplay(Blips[k], 4)
			SetBlipAsShortRange(Blips[k], true)
			SetBlipColour(Blips[k], 41)
			SetBlipScale(Blips[k], 0.3)
			BeginTextCommandSetBlipName("STRING")
			AddTextComponentString("Tesouro")
			EndTextCommandSetBlipName(Blips[k])
		end

		TriggerEvent("Notify", "verde", "Marcações ativadas.", "Scanner", 5000)
	end
end)
