

local whb_version = "7"



WHBHuds = WHBHuds or {}



local ww_hudbuilder_disable = CreateConVar("ww_hudbuilder_disable", "0")



net.Receive("whb_sendhud", function()

	local codes = net.ReadTable()

	table.Empty(WHBHuds)

	for usagetarg,code in pairs(codes) do

		WHBHuds[usagetarg] = CompileString(code, "HudBuilder" .. usagetarg .. os.time())

	end

	MsgN("WHB: HUD Updated")

end)



net.Receive("whb_clearhud", function(le, cl)

	table.Empty(WHBHuds)

	MsgN("WHB: HUD Cleared")

end)



local function DetectGamemode()

	if ROLE_TRAITOR then

		return "ttt"

	end

	return "sandbox"

end



local dont_draw_these = {

	"DarkRP_LocalPlayerHUD",

	"WAdvancedTTTStamina",

	"CHudHealth",

	"CHudAmmo",

	"CHudSecondaryAmmo"

}



hook.Add("HUDShouldDraw", "WHB_HUDReplacements", function(hud)

	if table.HasValue(dont_draw_these, hud) and table.Count(WHBHuds) > 0 and not ww_hudbuilder_disable:GetBool() then

		return false

	end

end)



local function DrawFakeCrosshair()

	local wep = LocalPlayer():GetActiveWeapon()

	if IsValid(wep) and wep.DrawCrosshair ~= false then

		local mx, my = ScrW()/2, ScrH()/2

		surface.SetDrawColor(255, 255, 255)

		surface.DrawLine(mx-10, my, mx-6, my)

		surface.DrawLine(mx+6, my, mx+10, my)

		surface.DrawLine(mx, my-10, mx, my-6)

		surface.DrawLine(mx, my+6, mx, my+10)

	end

end



hook.Add("HUDPaint", "WHB_NormalHUDReplacer", function()

	if IsValid(WWHudBuilder) and WWHudBuilder:IsVisible() then return true end



	if ww_hudbuilder_disable:GetBool() then return end

	

	if table.Count(WHBHuds) <= 0 then return end



	local gm = DetectGamemode()



	if gm == "ttt" then

		local client = LocalPlayer()



		GAMEMODE:HUDDrawTargetID()



		MSTACK:Draw(client)

	end



	local hud_found = false



	for usagetarg,hud in pairs(WHBHuds) do

		-- We dun care about the regular hud here

		if usagetarg == "normal" then continue end



		-- func returns true if it is _NOT_ drawn

		local ret = hud()

		if ret ~= true then

			hud_found = true

			break

		end

	end

	if not hud_found and WHBHuds.normal then

		WHBHuds.normal()

		hud_found = true

	end



	if gm == "ttt" then

		local client = LocalPlayer()



		if (not client:Alive()) or client:Team() == TEAM_SPEC then

			--SpecHUDPaint(client)

			return true

		end



		RADAR:Draw(client)

		TBHUD:Draw(client)

		WSWITCH:Draw(client)



		VOICE.Draw(client)

		DISGUISE.Draw(client)



		GAMEMODE:HUDDrawPickupHistory()



		return true

	else

		--DrawFakeCrosshair()

	end



end, 10)



CreateConVar("ww_hudbuilder_src", "", FCVAR_ARCHIVE)



local function OpenBuilder(code)

	if IsValid(WWHudBuilder) then WWHudBuilder:Remove() end

	local frame = vgui.Create("DFrame")

	frame.Paint = function() end



	frame:ShowCloseButton(false)

	frame:SetPos(0, 0)

	frame:SetSize(ScrW(), ScrH())



	local html = frame:Add("DHTML")

	html:SetPos(0, 0)

	html:SetSize(ScrW(), ScrH())



	local src = cvars.String("ww_hudbuilder_src")

	if not src or src == "" then

		src = "http://gmod.icemist.co/whb/editor.php?version=" .. whb_version .. "&hostip=" .. GetConVarString("hostip")

	end

	html:OpenURL(src)



	html:AddFunction( "gmod", "UseLocalHudBuilder", function( param )

		if not file.Exists("whb_scrapped", "DATA") then

			file.CreateDir("whb_scrapped")

		end

		file.Write("whb_scrapped/usedlocal" .. os.time() .. ".txt", param)



		net.Start("whb_sendhud")

		net.WriteTable(util.JSONToTable(param))

		net.WriteBit(true)

		net.SendToServer()

		frame:Close()

	end )

	html:AddFunction( "gmod", "UseHudBuilder", function( param )

		if not file.Exists("whb_scrapped", "DATA") then

			file.CreateDir("whb_scrapped")

		end

		file.Write("whb_scrapped/used" .. os.time() .. ".txt", param)



		net.Start("whb_sendhud")

		net.WriteTable(util.JSONToTable(param))

		net.WriteBit(false)

		net.SendToServer()

		frame:Close()

	end )

	html:AddFunction( "gmod", "CloseHudBuilder", function( param )

		if not file.Exists("whb_scrapped", "DATA") then

			file.CreateDir("whb_scrapped")

		end

		file.Write("whb_scrapped/scrap" .. os.time() .. ".txt", param)

		frame:Close()

	end )

	html:AddFunction( "gmod", "ComputeStoreLua", function( param )

		if not file.Exists("whb_scrapped", "DATA") then

			file.CreateDir("whb_scrapped")

		end

		file.Write("whb_scrapped/luaified" .. os.time() .. ".txt", param)



		net.Start("whb_requestlua")

		net.WriteTable(util.JSONToTable(param))

		net.SendToServer()

		frame:Close()

	end )

	html:AddFunction( "gmod", "OpenURL", function( param )

		gui.OpenURL(param)

	end )



	html:SetFocusTopLevel( true )



	html:SetMouseInputEnabled(true)

	html:SetKeyBoardInputEnabled(true)



	if code then

		code = code:Replace("\"", "\\\"")

		html:Call("loadOld(\"" .. code .. "\")")

	end



	local panicClose = vgui.Create("DButton", frame)

	panicClose:SetText("Force Close")

	panicClose:SetPos(ScrW() - 60, 0)

	panicClose.DoClick = function()

		frame:Close()

	end



	frame:MakePopup()



	WWHudBuilder = html

end





net.Receive("whb_sendhudcode", function(le, cl)

	OpenBuilder(net.ReadString())

end)



net.Receive("whb_requestlua", function(le, cl)

	local code = net.ReadString()

	MsgN("----------------- WYOZI HUD BUILDER LUA CODE ------------------")

	MsgN(code)

	MsgN("----------------- WYOZI HUD BUILDER LUA CODE ------------------")



	if not file.Exists("whb_scrapped", "DATA") then

		file.CreateDir("whb_scrapped")

	end

	file.Write("whb_scrapped/luaified_lua" .. os.time() .. ".txt", code)



	SetClipboardText(code)

end)



concommand.Add("ww_hudbuilder_new", function()

	if not LocalPlayer():IsSuperAdmin() then

		MsgN("Sorry! SuperAdmin only.")

		return

	end

	OpenBuilder()



end)



concommand.Add("ww_hudbuilder_loadold", function(ply, cmd, args)

	if not LocalPlayer():IsSuperAdmin() then

		MsgN("Sorry! SuperAdmin only.")

		return

	end

	local usagetarg = args[1] or "normal"

	if usagetarg == "" then usagetarg = "normal" end



	net.Start("whb_sendhudcode")

	net.WriteString(usagetarg)

	net.SendToServer()

end)



concommand.Add("ww_hudbuilder_loadclient", function(ply, cmd, args)

	if not args[1] then return MsgN("file argument required") end

	local code = file.Read("whb_scrapped/" .. args[1], "DATA")

	if code then

		OpenBuilder(code)

	else

		MsgC(Color(255, 0, 0), " No file found: " .. args[1])

	end

end, function(commandName, args) 

	args = args:sub(1)

	local files = file.Find("whb_scrapped/*.txt", "DATA")

	local ret = {}

	for _, file in pairs(files) do

		table.insert(ret, "ww_hudbuilder_loadclient " .. file)

	end

	return ret

end)