if (not CLIENT) then return end
local activeIndicators = {}

net.Receive("zrms_AddConnectHint", function()
	local rEnt = net.ReadEntity()
	local rType = net.ReadFloat()

	table.insert(activeIndicators, {
		ent = rEnt,
		atype = rType
	})
end)

net.Receive("zrms_RemoveAllConnectHint", function()
	table.Empty(activeIndicators)
end)

local zrms_material_indicator = Material("materials/zerochain/zrms/ui/zrms_build_identicator.png", "smooth")

local function draw_Circle(x, y, radius, seg)
	local cir = {}

	table.insert(cir, {
		x = x,
		y = y,
		u = 0.5,
		v = 0.5
	})

	for i = 0, seg do
		local a = math.rad((i / seg) * -360)

		table.insert(cir, {
			x = x + math.sin(a) * radius,
			y = y + math.cos(a) * radius,
			u = math.sin(a) / 2 + 0.5,
			v = math.cos(a) / 2 + 0.5
		})
	end

	local a = math.rad(0) -- This is needed for non absolute segment counts

	table.insert(cir, {
		x = x + math.sin(a) * radius,
		y = y + math.cos(a) * radius,
		u = math.sin(a) / 2 + 0.5,
		v = math.cos(a) / 2 + 0.5
	})

	surface.DrawPoly(cir)
end

local function build_indicator2d()
	local ply = LocalPlayer()

	if IsValid(ply) and ply:Alive() then
		for k, v in pairs(activeIndicators) do
			local pos
			local ent = v.ent
			local iType = v.atype

			if (IsValid(ent)) then
				if (ply:GetPos():Distance(ent:GetPos()) > 800) then return end

				if (string.sub(ent:GetClass(), 1, 12) == "zrms_refiner") then
					if (iType == 1) then
						pos = ent:GetPos() + ent:GetForward() * -50 + ent:GetUp() * 7
					elseif (iType == 2) then
						pos = ent:GetPos() + ent:GetUp() * 5 + ent:GetRight() * 45
					end
				elseif (ent:GetClass() == "zrms_crusher") then
					pos = ent:GetPos() + ent:GetForward() * -50 + ent:GetUp() * 7
				elseif (string.sub(ent:GetClass(), 1, 17) == "zrms_conveyorbelt") then
					pos = ent:GetAttachment(ent:LookupAttachment("output")).Pos
				elseif (string.sub(ent:GetClass(), 1, 13) == "zrms_splitter") then
					pos = ent:GetPos() + ent:GetRight() * 15 + ent:GetUp() * 7
				elseif (string.sub(ent:GetClass(), 1, 11) == "zrms_sorter") then
					pos = ent:GetPos() + ent:GetRight() * 15 + ent:GetUp() * 7
				end

				if (v.atype == 3) then
					pos = ent:GetAttachment(ent:LookupAttachment("input")).Pos
					if (ply:GetPos():Distance(ent:GetPos()) > 200) then return end
				end

				pos = pos:ToScreen()

				if (v.atype == 1) then
					--This Color is for Build Indicators
					surface.SetDrawColor(Color(0, 255, 0))
				elseif (v.atype == 2) then
					--This Color is for Basket Indicators
					surface.SetDrawColor(Color(255, 255, 0))
				elseif (v.atype == 3) then
					--This Color is for Resource Crusher Indicators
					surface.SetDrawColor(Color(255, 125, 0))
				else
					-- This is the error color if we dont have a type defined
					surface.SetDrawColor(Color(255, 255, 255))
				end

				surface.SetMaterial(zrms_material_indicator)
				draw_Circle(pos.x, pos.y, 50, 25)
			end
		end
	end
end

hook.Add("HUDPaint", "zrms_buildhints_indicator2d", build_indicator2d)
