if not SERVER then return end
util.AddNetworkString("zrms_AddConnectHint")
util.AddNetworkString("zrms_RemoveAllConnectHint")

local function zrmine_sv_AddIndicator(ent, ply, iType)
	net.Start("zrms_AddConnectHint")
	net.WriteEntity(ent)
	net.WriteFloat(iType)
	net.Send(ply)
end

local function zrmine_sv_buildhelper(ply, ent)
	local ents = ents.GetAll()

	-- This adds Place Indicators if we pick a resource up
	if (string.sub(ent:GetClass(), 1, 13) == "zrms_resource") then
		for k, v in pairs(ents) do
			if (v:GetClass() == "zrms_crusher") then
				zrmine_sv_AddIndicator(v, ply, 3)
			end
		end
		-- This adds Place Indicators if we pick a basket up
	elseif (string.sub(ent:GetClass(), 1, 11) == "zrms_basket" and ent:GetResourceAmount() < zrmine.config.ResourceCrates_Capacity) then
		for k, v in pairs(ents) do
			if (zrmine.config.ResourceCrates_Sharing) then
				if (zrmine.f.IsOwner(ply, v) and string.sub(v:GetClass(), 1, 12) == "zrms_refiner" and v.connected_Basket == nil and v.connected_Parent ~= nil) then
					zrmine_sv_AddIndicator(v, ply, 2)
				end
			else
				if (string.sub(v:GetClass(), 1, 12) == "zrms_refiner" and v.connected_Basket == nil and v.connected_Parent ~= nil) then
					zrmine_sv_AddIndicator(v, ply, 2)
				end
			end
		end
	elseif (string.sub(ent:GetClass(), 1, 13) == "zrms_splitter") then
		-- This adds Building Indicators if we pick a splitter
		for k, v in pairs(ents) do
			if (zrmine.f.IsOwner(ply, v) and string.sub(v:GetClass(), 1, 17) == "zrms_conveyorbelt" and v.connected_module == nil and IsValid(v.connected_Parent)) then
				zrmine_sv_AddIndicator(v, ply, 1)
			end
		end
	elseif (string.sub(ent:GetClass(), 1, 11) == "zrms_sorter") then
		-- This adds Building Indicators if we pick a splitter
		for k, v in pairs(ents) do
			if (zrmine.f.IsOwner(ply, v) and string.sub(v:GetClass(), 1, 17) == "zrms_conveyorbelt" and v.connected_module == nil and IsValid(v.connected_Parent)) then
				zrmine_sv_AddIndicator(v, ply, 1)
			end
		end
	elseif (string.sub(ent:GetClass(), 1, 13) == "zrms_inserter") then
		-- This adds Building Indicators if we pick a inserter
		for k, v in pairs(ents) do
			if (zrmine.f.IsOwner(ply, v)) then
				--[[&& v:GetCurrentState() == "WAITING_FOR_ORDER"]]
				if (string.sub(v:GetClass(), 1, 17) == "zrms_conveyorbelt" and v.connected_module == nil) then
					if (IsValid(v.connected_Parent)) then
						zrmine_sv_AddIndicator(v, ply, 1)
					end
				elseif (string.sub(v:GetClass(), 1, 12) == "zrms_refiner" and v ~= ent) then
					--[[ && v:GetCurrentState() == "WAITING_FOR_ORDER"]]
					if (IsValid(v.connected_Parent) and v.connected_module == nil) then
						zrmine_sv_AddIndicator(v, ply, 1)
					end
				elseif (v:GetClass() == "zrms_crusher") then
					--[[&& v:GetCurrentState() == "WAITING_FOR_ORDER"]]
					if (v.connected_module == nil) then
						zrmine_sv_AddIndicator(v, ply, 1)
					end
				end
			end
		end
	elseif (string.sub(ent:GetClass(), 1, 12) == "zrms_refiner") then
		-- This adds Building Indicators if we pick a refiner
		for k, v in pairs(ents) do
			if (zrmine.f.IsOwner(ply, v)) then
				if (string.sub(v:GetClass(), 1, 17) == "zrms_conveyorbelt" and v.connected_module == nil and IsValid(v.connected_Parent)) then
					zrmine_sv_AddIndicator(v, ply, 1)
				end

				if (string.sub(v:GetClass(), 1, 12) == "zrms_refiner" and v.connected_module == nil and IsValid(v.connected_Parent)) then
					zrmine_sv_AddIndicator(v, ply, 1)
				end
			end
		end
	elseif (string.sub(ent:GetClass(), 1, 17) == "zrms_conveyorbelt") then
		-- This adds Building Indicators if we pick a Conveyorbelt up
		for k, v in pairs(ents) do
			if (zrmine.f.IsOwner(ply, v)) then
				if (v:GetClass() == "zrms_crusher" and v.connected_module == nil) then
					zrmine_sv_AddIndicator(v, ply, 1)
				end

				if (string.sub(v:GetClass(), 1, 17) == "zrms_conveyorbelt" and v.connected_module == nil and v:GetBuildMode() == false) then
					zrmine_sv_AddIndicator(v, ply, 1)
				end

				if (string.sub(v:GetClass(), 1, 13) == "zrms_splitter" and IsValid(v.connected_Parent) and (v.connected_module01 == nil or v.connected_module02 == nil)) then
					zrmine_sv_AddIndicator(v, ply, 1)
				end

				if (string.sub(v:GetClass(), 1, 11) == "zrms_sorter" and IsValid(v.connected_Parent) and (v.connected_module01 == nil or v.connected_module02 == nil)) then
					zrmine_sv_AddIndicator(v, ply, 1)
				end

				if (string.sub(v:GetClass(), 1, 12) == "zrms_refiner" and v.connected_module == nil and IsValid(v.connected_Parent)) then
					zrmine_sv_AddIndicator(v, ply, 1)
				end
			end
		end
	end
end

hook.Add("GravGunOnPickedUp", "zrmine_sv_buildhelper_gravpick", zrmine_sv_buildhelper)

hook.Add("GravGunOnDropped", "zrmine_sv_buildhelper_gravdrop", function(ply, ent)
	net.Start("zrms_RemoveAllConnectHint")
	net.Send(ply)
end)
