if (not CLIENT) then return end
local Created = false
CreateConVar("zrms_cl_highlight", "1", {FCVAR_ARCHIVE})
CreateConVar("zrms_cl_highlight_refreshrate", "0.1", {FCVAR_ARCHIVE})
CreateConVar("zrms_cl_pickaxe_help", "1", {FCVAR_ARCHIVE})
CreateConVar("zrms_cl_FillIndicator", "1", {FCVAR_ARCHIVE})

local function retrominerssettings(CPanel)
	Created = true

	local ObjectHighLighting = CPanel:AddControl("CheckBox", {
		Label = "Enable Indicator Halos (GPU Intensive)",
		Command = "zrms_cl_highlight"
	})

	ObjectHighLighting.OnChange = function(panel, bVal)
		if (not Created) then
			if ((bVal and 1 or 0) == cvars.Number("zrms_cl_highlight")) then return end
			RunConsoleCommand("zrms_cl_highlight", tostring(bVal))
		end
	end

	local ObjectHighLightRefreshRate = CPanel:NumSlider("Highlight/Info RefreshRate (Seconds)", "zrms_cl_highlight_refreshrate", 0.01, 3, 2)

	ObjectHighLightRefreshRate.OnChange = function(panel, bVal)
		if (not Created) then
			RunConsoleCommand("zrms_cl_highlight_refreshrate", tostring(bVal))
		end
	end

	local PickaxeHelp = CPanel:AddControl("CheckBox", {
		Label = "Enable Pickaxe Help (On Screen Top)",
		Command = "zrms_cl_pickaxe_help"
	})

	PickaxeHelp.OnChange = function(panel, bVal)
		if (not Created) then
			if ((bVal and 1 or 0) == cvars.Number("zrms_cl_pickaxe_help")) then return end
			RunConsoleCommand("zrms_cl_pickaxe_help", tostring(bVal))
		end
	end

	local FillIndicator = CPanel:AddControl("CheckBox", {
		Label = "Enable Fill Indicators (Ore Icons)",
		Command = "zrms_cl_FillIndicator"
	})

	FillIndicator.OnChange = function(panel, bVal)
		if (not Created) then
			if ((bVal and 1 or 0) == cvars.Number("zrms_cl_FillIndicator")) then return end
			RunConsoleCommand("zrms_cl_FillIndicator", tostring(bVal))
		end
	end

	timer.Simple(0.1, function()
		if (ObjectHighLighting) then
			ObjectHighLighting:SetValue(GetConVar("zrms_cl_highlight"):GetInt())
		end

		if (ObjectHighLightRefreshRate) then
			ObjectHighLightRefreshRate:SetValue(GetConVar("zrms_cl_highlight_refreshrate"):GetFloat())
		end

		if (PickaxeHelp) then
			PickaxeHelp:SetValue(GetConVar("zrms_cl_pickaxe_help"):GetInt())
		end

		if (FillIndicator) then
			FillIndicator:SetValue(GetConVar("zrms_cl_FillIndicator"))
		end

		Created = false
	end)
end

local function retromineradmin(CPanel)
	CPanel:AddControl("label", {
		Text = "-------------------------------------------------------------------"
	})

	CPanel:AddControl("label", {
		Text = "This lets you Save the Ore Spawns"
	})

	CPanel:Button("Save Ore Spawns", "zrmine_spawn")

	CPanel:AddControl("label", {
		Text = "This lets you Save the public Melter and MineEntrace"
	})

	CPanel:Button("Save Public Entities", "zrmine_savepublicentities")

	CPanel:AddControl("label", {
		Text = "-------------------------------------------------------------------"
	})

	CPanel:AddControl("label", {
		Text = "This lets you Save,Load and Remove the PipeLine build by the Admin who presses the Button"
	})

	CPanel:AddControl("label", {
		Text = "A Pipeline can be made out of Crushers, Conveyorbelts, Refinerys, Inserters, Splitters"
	})

	CPanel:Button("Save PipeLine", "zrmine_pipeline_save")
	CPanel:Button("Rebuild PipeLine", "zrmine_pipeline_load")
	CPanel:Button("Delete PipeLine", "zrmine_pipeline_delete")

	CPanel:AddControl("label", {
		Text = "-------------------------------------------------------------------"
	})
end

hook.Add("PopulateToolMenu", "PopulateZRMSMenus", function()
	spawnmenu.AddToolMenuOption("Options", "Retro Miner", "ZRMSSettings", "Settings", "", "", retrominerssettings)
	spawnmenu.AddToolMenuOption("Options", "Retro Miner", "ZRMSAdmin", "Admin", "", "", retromineradmin)
end)

hook.Add("AddToolMenuCategories", "CreateZRMSCategories", function()
	spawnmenu.AddToolCategory("Options", "Retro Miner", "Retro Miner")
end)
