----------------------------
-- CakeScript (March 29, 2008)
-- by LuaBanana
--
-- Alpha Version
-- cl_hud.lua
----------------------------

LocalPlayer().MyModel = "" -- Has to be blank for the initial value, so it will create a spawnicon in the first place.

surface.CreateFont( "ChatFont", 22, 100, true, false, "PlInfoFont" );

local function DrawTime()

	draw.DrawText( GetGlobalString("time"), "PlInfoFont",  ScrW()/8*1, 12, Color( 255,255,255,255), 0);
	
end

--[[local function DrawPlayerInfo()

	local PlayerInfoBox = draw.RoundedBox( 8, 10, 10, 250, 125, GUIcolor_trans)
	
	-- Check to see if player model has changed
	if(LocalPlayer().MyModel != LocalPlayer():GetModel()) then
	
		if( PlayerIcon ) then
		
			PlayerIcon:Remove();
			PlayerIcon = nil;
		
		end
		
		PlayerIcon = vgui.Create( "SpawnIcon", PlayerInfoBox);
		PlayerIcon:SetPos( 15, 15 );
		PlayerIcon:SetModel( LocalPlayer():GetModel() );
		
		LocalPlayer().MyModel = LocalPlayer():GetModel();
		
	end
	
	draw.DrawText( "Citizen Identification Card", "Default", 166, 15, Color( 255, 255, 255, 255 ), 1)
	
	local cid = LocalPlayer():GetNWString("info.cid")
	
	local newcid = string.sub(cid, 1, 2) .. "-" .. string.sub(cid, 3, 6) .. "-" .. string.sub(cid, 7, 10)
	
	draw.DrawText( newcid, "ChatFont", 166, 35, Color( 255, 255, 255, 255 ), 1)
	draw.DrawText( LocalPlayer():Nick(), "ChatFont", 166, 50, Color( 255,255,255,255), 1);
	draw.DrawText( "Title: " .. LocalPlayer():GetNWString("info.job"), "Default", 15, 84, Color( 255,255,255,255), 0);
	draw.DrawText( "Assosciation: " .. team.GetName( LocalPlayer():Team() ), "Default", 15, 100, Color( 255,255,255,255), 0);
	draw.DrawText( newcid, "DefaultVerySmall", 255, 120, Color( 255, 255, 255, 255 ), 2)
	
end]]

function DrawTargetInfo()
	
	local tr = LocalPlayer():GetEyeTrace()
	
	if(LocalPlayer():GetNWBool("seeall")) then
		for k, v in pairs(player.GetAll()) do
			if(v != LocalPlayer()) then
				if(v:Alive() and v:GetNWInt("ko") == 0 and !v:GetNWBool("cloaked")) then
					local alpha = 0
					local position = v:GetPos()
					local position = Vector(position.x, position.y, position.z + 75)
					local screenpos = position:ToScreen()
					local dist = position:Distance(LocalPlayer():GetPos())
					local dist = dist / 2
					local dist = math.floor(dist)
					
					if(dist > 160 and !LocalPlayer():GetNWBool("seeall")) then
						alpha = 255 - (dist-160)*5
					else
						alpha = 255
					end
					
					if(alpha > 255) then
					
						alpha = 255
						
					elseif(alpha < 0) then
					
						alpha = 0
						
					end
					
					--draw.DrawText( v:Nick(), "DefaultSmall", screenpos.x, screenpos.y, Color(255, 255, 255, alpha), 1)
					draw.SimpleTextOutlined( v:Nick(), "DefaultSmall", screenpos.x , screenpos.y, Color(255,255,255,alpha), 1, 0, 2, Color(0,0,0,alpha) );
					--draw.DrawText( team.GetName(v:Team()), "DefaultSmall", screenpos.x, screenpos.y + 10, Color(255, 255, 255, alpha), 1) -- seems silly to me.
					--draw.DrawText( v:GetNWString("info.job"), "DefaultSmall", screenpos.x, screenpos.y + 20, Color(255, 255, 255, alpha), 1)
					draw.SimpleTextOutlined( v:GetNWString("info.job"), "DefaultSmall", screenpos.x , screenpos.y + 20, Color(255,255,255,alpha), 1, 0, 2, Color(0,0,0,alpha) );
					
					--[[if(v:GetNWInt("chatopen") == 1) then
						draw.DrawText( "Typing..", "ChatFont", screenpos.x, screenpos.y - 50, Color(255, 255, 255, alpha), 1)
					end]]
					
				end
				
			end
		end
	end
	
	if(tr.HitWorld or !tr.Entity:IsValid()) then return; end
	
	if(tr.Entity:GetClass() == "item_prop" and tr.Entity:GetPos():Distance(LocalPlayer():GetPos()) < 100) then
	
		local screenpos = tr.Entity:GetPos():ToScreen()
		--draw.DrawText( tr.Entity:GetNWString("Name"), "ChatFont", screenpos.x + 2, screenpos.y + 2, Color(0, 0, 0, 255), 1);	
		draw.SimpleTextOutlined( tr.Entity:GetNWString("Name"), "ChatFont", screenpos.x, screenpos.y, Color(255, 255, 255, 255), 0, 0, 2, Color(0, 0, 0, 255) );
		--draw.DrawText( tr.Entity:GetNWString("Description"), "ChatFont", screenpos.x + 2, screenpos.y + 22, Color(0, 0, 0, 255), 1);	
		draw.SimpleTextOutlined( tr.Entity:GetNWString("Description"), "ChatFont", screenpos.x, screenpos.y + 20, Color(255, 255, 255, 255), 0, 0, 2, Color(0, 0, 0, 255) );
		
	elseif( CAKE.IsDoor(tr.Entity) && tr.Entity:GetPos():Distance(LocalPlayer():GetPos()) < 100) then
		
		-- if owned
		if ( tr.Entity:GetNWString("owner") != "" ) then
			draw.SimpleTextOutlined( tr.Entity:GetNWString("title"), "ChatFont", ScrW()/2 , ScrH()/2, Color(255,255,255,255), 1, 1, 2, Color(0, 0, 0, 255) );
		-- otherwise
		else
			draw.SimpleTextOutlined( "Unowned", "ChatFont", ScrW()/2, ScrH()/2, Color(237,88,0,255), 1, 1, 2, Color(0, 0, 0, 255) );
		end
		
	elseif(!LocalPlayer():GetNWBool("seeall") and tr.Entity:GetClass() == "player" and tr.Entity:Alive() and tr.Entity:GetNWInt("ko") == 0 and !tr.Entity:GetNWBool("cloaked")) then
		local alpha = 0
		--local position = tr.Entity:GetPos()
		--local position = Vector(position.x, position.y, position.z + 75)
		local position = tr.Entity:GetShootPos();
		local screenpos = position:ToScreen()
		local dist = position:Distance(LocalPlayer():GetPos())
		local dist = dist / 2
		local dist = math.floor(dist)

		if(dist > 160 and !LocalPlayer():GetNWBool("seeall")) then
			alpha = 255 - (dist-160)*5
		else
			alpha = 255
		end
		if(alpha > 255) then
			alpha = 255
		elseif(alpha < 0) then
			alpha = 0
		end

		if(alpha ~= 0) then
			--draw.DrawText( v:Nick(), "DefaultSmall", screenpos.x, screenpos.y, Color(255, 255, 255, alpha), 1)
			draw.SimpleTextOutlined( tr.Entity:Nick(), "DefaultSmall", screenpos.x , screenpos.y, Color(255,255,255,alpha), 1, 0, 2, Color(0,0,0,alpha) );
			--draw.DrawText( team.GetName(v:Team()), "DefaultSmall", screenpos.x, screenpos.y + 10, Color(255, 255, 255, alpha), 1) -- seems silly to me.
			--draw.DrawText( v:GetNWString("info.job"), "DefaultSmall", screenpos.x, screenpos.y + 20, Color(255, 255, 255, alpha), 1)
			draw.SimpleTextOutlined( tr.Entity:GetNWString("info.job"), "DefaultSmall", screenpos.x , screenpos.y + 20, Color(255,255,255,alpha), 1, 0, 2, Color(0,0,0,alpha) );
		end
	end
end

function DrawDeathMeter()
	local timeleft = LocalPlayer():GetNWInt("deathmoderemaining");
	local w = (timeleft / 120) * 198
	
	draw.RoundedBox( 8, ScrW() / 2 - 100, 5, 200, 50, Color(GUIcolor_trans));
	draw.RoundedBox( 8, ScrW() / 2 - 98, 7, w, 46, Color(255, 0, 0, 255));
	
	draw.DrawText( "Time Left (Type !ad to respawn)", "ChatFont", ScrW() / 2 - 93, 25 - 5, Color( 255,255,255,255), 0);
	
end

function DrawPlayerTyping()
	for _,v in pairs(player.GetAll()) do
		if(v != LocalPlayer() and v:Alive() and v:GetNWInt("chatopen") and v:GetNWInt("ko") == 0 and !v:GetNWBool("cloaked")) then
			local tracedata = {}
			tracedata.start = LocalPlayer():GetShootPos();
			tracedata.endpos = v:GetShootPos();
			tracedata.filter = LocalPlayer();
			local trace = util.TraceLine(tracedata);
			
			if(trace.Entity == v) then
				local alpha = 0
				--local position = v:GetPos()
				--local position = Vector(position.x, position.y, position.z + 75)
				local position = v:GetShootPos();
				local screenpos = position:ToScreen()
				local dist = position:Distance(LocalPlayer():GetPos())
				local dist = dist / 2
				local dist = math.floor(dist)
					
				if(dist > 160 and !LocalPlayer():GetNWBool("seeall")) then
					alpha = 255 - (dist-160)*5
				else
					alpha = 255
				end

				if(alpha > 255) then
					alpha = 255
				elseif(alpha < 0) then
					alpha = 0
				end

				if(alpha ~= 0) then
					draw.DrawText( "Typing..", "ChatFont", screenpos.x, screenpos.y - 50, Color(255, 255, 255, alpha), 1)
				end
			end
		end
	end
end

--[[function DrawPlayerInfo()

	for k, v in pairs(player.GetAll()) do	
	
		if(v != LocalPlayer()) then
		
			if(v:Alive() and v:GetNWInt("ko") == 0 and !v:GetNWBool("cloaked")) then
			
				local alpha = 0
				local position = v:GetPos()
				local position = Vector(position.x, position.y, position.z + 75)
				local screenpos = position:ToScreen()
				local dist = position:Distance(LocalPlayer():GetPos())
				local dist = dist / 2
				local dist = math.floor(dist)
				
				if(dist > 160 and !LocalPlayer():GetNWBool("seeall")) then
				
					alpha = 255 - (dist-160)*5
					
				else
				
					alpha = 255
					
				end
				
				if(alpha > 255) then
				
					alpha = 255
					
				elseif(alpha < 0) then
				
					alpha = 0
					
				end
				
				--draw.DrawText( v:Nick(), "DefaultSmall", screenpos.x, screenpos.y, Color(255, 255, 255, alpha), 1)
				draw.SimpleTextOutlined( v:Nick(), "DefaultSmall", screenpos.x , screenpos.y, Color(255,255,255,alpha), 1, 0, 2, Color(0,0,0,alpha) );
				--draw.DrawText( team.GetName(v:Team()), "DefaultSmall", screenpos.x, screenpos.y + 10, Color(255, 255, 255, alpha), 1) -- seems silly to me.
				--draw.DrawText( v:GetNWString("info.job"), "DefaultSmall", screenpos.x, screenpos.y + 20, Color(255, 255, 255, alpha), 1)
				draw.SimpleTextOutlined( v:GetNWString("info.job"), "DefaultSmall", screenpos.x , screenpos.y + 20, Color(255,255,255,alpha), 1, 0, 2, Color(0,0,0,alpha) );
				
				if(v:GetNWInt("chatopen") == 1) then
					draw.DrawText( "Typing..", "ChatFont", screenpos.x, screenpos.y - 50, Color(255, 255, 255, alpha), 1)
				end
				
			end
			
		end
		
	end
	
end]]

function DrawKO()
	draw.RoundedBox( 0, 0, 0, ScrW(), ScrH(), Color(0,0,0,222));
	draw.DrawText( "Knocked Out", "ChatFont", ScrW() / 2, 5, Color( 255,255,255,255), 1);
	
	if( LocalPlayer():GetNWInt("ko") > 1 ) then
		draw.DrawText( "Time remaining: " .. LocalPlayer():GetNWInt("ko")-1, "ChatFont", ScrW() / 2, 20, Color( 255,255,255,255), 1);
	else
		draw.DrawText( "To wakeup type /wakeup", "ChatFont", ScrW() / 2, 20, Color(255,255,255,255),1);
	end
	draw.RoundedBox( 0, ScrW()/2-105, 40, 210, 20, Color(0,0,0,255));
	draw.RoundedBox( 0, ScrW()/2-100, 45, (31-LocalPlayer():GetNWInt("ko")) /30 * 200, 10, Color(130,205,255,255) );
	
end

-- Used TS code for status area.
function DrawHudInfo()

	surface.SetFont( "PlInfoFont" );
	
	local titlew, titleh = surface.GetTextSize( LocalPlayer():GetNWString( "info.job" ) );
	titlew = titlew + 10;
	titlew = math.Clamp( titlew - 220, 0, ScrW() );

	-- complaints that TS is u-g-l-y, redoing it @.@
	--draw.RoundedBox( 4, 10, ScrH() - 90, 220 + titlew, 70, Color( 50, 50, 50, 200 ) );
	
	-- its two boxes?! wtf!!! and diff colors?! and offset?! OMGWTFHAX!!!
	draw.RoundedBox( 6, 25, ScrH() - 90, 240 + titlew, 60, Color(100, 100, 100, 100));
	draw.RoundedBox( 6, 25, ScrH() - 90, 240 + titlew, 60, Color(0, 0, 0, 200));
	--local tex=surface.GetTextureID("milkHud/1titlebar"); -- Gets the txture id 
	--surface.SetTexture(tex);
	--surface.SetDrawColor(70,70,70,255); -- modifies the color multipliers
	--surface.DrawTexturedRect( 25, ScrH() - 130, 240 + titlew, 100 );
	
	-- draws their title/money, 200 alpha is arbitrary
	draw.SimpleTextOutlined( LocalPlayer():GetNWString( "info.job" ), "PlInfoFont", 245 + titlew, ScrH() - 80, Color( 255, 255, 255, 200 ), 2, 0, 2, Color( 0, 0, 0, 200 ) );
	draw.SimpleTextOutlined( LocalPlayer():GetNWString( "money" ) .. " credits", "PlInfoFont", 245 + titlew, ScrH() - 60, Color( 255, 255, 255, 200 ), 2, 0, 2, Color( 0, 0, 0, 200 ) );
	
	--local PrimaryAmmo = LocalPlayer():GetActiveWeapon():Clip1()
	local HealthPlayer = LocalPlayer():Health()
	local ArmorPlayer = LocalPlayer():Armor()
	if( LocalPlayer():Health() > 100 ) then
			HealthPlayer = 100
			--draw.RoundedBox( Number Bordersize, Number X, Number Y, Number Width, Number Height, Table Colour )
			draw.DrawText( "+  " .. LocalPlayer():Health() - 100 .. "%", "budgetlabel", 345, ScrH() / 8	 * 7 -26, Color(255, 0, 0, 255))
	end
		--draw.RoundedBox( 2	, 48, 48, 500, 110, Color(200,200,200,70))
		--draw.DrawText( "PRIMARY AMMO RESERVES: " .. PrimaryAmmo .. " SHOTS LEFT IN MAGAZINE", "BudgetLabel", 50, 50, Color(200,200,200,255) )
		-- HEALTH	
		draw.RoundedBox( 0, 6, ScrH() / 8 * 7 - 28, 303, 9, Color(0,0,0,255))
		draw.RoundedBox( 0, 8, ScrH() / 8 * 7 -26, HealthPlayer * 3, 3,  Color(255,0,0,255))
		draw.RoundedBox( 0, 8, ScrH() / 8 * 7 -23, HealthPlayer * 3, 3,  Color(200,0,0,255))
		
		-- SPRINT
		draw.RoundedBox( 0, 6, ScrH() / 8 * 7 - 8, 303, 9, Color(0,0,0,255))
		draw.RoundedBox( 0, 8, ScrH() / 8 * 7 - 6, LocalPlayer():GetNWFloat("sprint") * 3, 3, Color(255,255,0,255))
		draw.RoundedBox( 0, 8, ScrH() / 8 * 7 - 3, LocalPlayer():GetNWFloat("sprint") * 3, 3, Color(200,200,0,255))
		-- ARMOR
		
		
			if( ArmorPlayer > 100) then
			ArmorPlayer = 100
			draw.DrawText( "+  " .. LocalPlayer():Armor() - 100 .. "%", "budgetlabel", 345, ScrH() / 8 * 7 - 16, Color(0, 0, 255, 255))
		end
		if( ArmorPlayer > 1 ) then
		draw.RoundedBox( 0, 6, ScrH() / 8 * 7 - 18, 303, 9, Color(0,0,0,255))
		draw.RoundedBox( 0, 8, ScrH() / 8 * 7 - 16, ArmorPlayer * 3, 3, Color(0,0,255,255))
		draw.RoundedBox( 0, 8, ScrH() / 8 * 7 - 13, ArmorPlayer * 3, 3, Color(0,0,200,255))
		end
		--[[
		local barpos = 0
		local x,y,w,h

		if barpos == 0 then x,y,w,h = 10,40,ScrW()-20,20
		elseif barpos == 1 then x,y,w,h = 35,ScrH()-120,200,20 
		end

		local exp = LocalPlayer():GetNWInt("Exp")
		local cur = LocalPlayer():GetNWInt("CurLevel")
		local new = LocalPlayer():GetNWInt("NextLevel")
		local lvl = LocalPlayer():GetNWInt("LevelNum")

		surface.SetDrawColor(0,0,0,255)
		surface.DrawOutlinedRect(x,y,w,h)
	
		surface.SetDrawColor(200,0,0,255)
		surface.DrawRect(x+1.2,y+1.2,w*((exp-cur)/(new-cur))-2,h-2)
	
		draw.DrawText("Level "..lvl,"ScoreboardText",x+w/2,y-20,Color(255,255,255,255),1)
		draw.DrawText(exp.."/"..new,"ScoreboardText",x+w/2,y+1,Color(0,0,0,255),1)
		]]--

	
	-- Check for less than one to not draw to prevent a bug of drawing a rounded box of size 0.
	
	--if( LocalPlayer():Health() > 1 ) then
		-- HP bar
		--draw.RoundedBox( 8, 10, 10, ScrW()/3 * LocalPlayer():Health() / 100 , 18, Color(185,85,85) ); -- background
		--draw.RoundedBox( 8, 12, 12, (ScrW()/3 - 4) * LocalPlayer():Health() / 100, 14, Color(190,0,35) ); -- color bar
		--draw.SimpleTextOutlined( LocalPlayer():Health() .. " / 100 ", "BudgetLabel", 20 + ScrW()/6 * LocalPlayer():Health() / 100 , 19, Color(255,255,255), 1, 1, 0, Color(0,0,0) ); -- HP # label on bar
		
		--tex=surface.GetTextureID("milkHud/healthcapleft");
		--surface.SetTexture(tex);
		--surface.SetDrawColor(255,255,255,255); -- Makes sure the image draws correctly
		--surface.DrawTexturedRect( 8, 12, 12, 14 );
		
		--tex=surface.GetTextureID("milkHud/healthstretch");
		--surface.SetTexture(tex);
		--surface.SetDrawColor(255,255,255,255); -- Makes sure the image draws correctly
		--s-urface.DrawTexturedRect( 20, 12, (ScrW()/4) * LocalPlayer():Health() / 100, 14 );
		
		---tex=surface.GetTextureID("milkHud/healthcapright");
		--surface.SetTexture(tex);
		--surface.SetDrawColor(255,255,255,255); -- Makes sure the image draws correctly
		--surface.DrawTexturedRect( 20 + (ScrW()/4) * LocalPlayer():Health() / 100 , 12, 12, 14 ); 	
	--end
	
	--if( LocalPlayer():GetNWFloat("sprint") > 1 ) then
		-- Sprint bar
		--draw.RoundedBox( 8, 10, 30, ScrW()/3 * LocalPlayer():GetNWFloat("sprint") / 100 , 18, Color(223,152,62) ); -- background
		--draw.RoundedBox( 8, 12, 32, (ScrW()/3 - 4) * LocalPlayer():GetNWFloat("sprint") / 100, 14, Color(253,128,0) ); -- color bar
		--draw.SimpleTextOutlined( LocalPlayer():GetNWFloat("sprint") .. " / 100 ", "BudgetLabel", 20 + ScrW()/6 * LocalPlayer():Health() / 100 , 19, Color(255,255,255), 1, 1, 0, Color(0,0,0) ); -- HP # label on bar
	
		--tex=surface.GetTextureID("milkHud/sprintcapleft");
		--surface.SetTexture(tex);
		--surface.SetDrawColor(255,255,255,255); -- Makes sure the image draws correctly
		--surface.DrawTexturedRect( 8, 26, 12, 14 );
		
		--tex=surface.GetTextureID("milkHud/sprintstretch");
		--surface.SetTexture(tex);
		--surface.SetDrawColor(255,255,255,255); -- Makes sure the image draws correctly
		--surface.DrawTexturedRect( 20, 26, (ScrW()/4) * LocalPlayer():GetNWFloat("sprint") / 100, 14 );
		
		--tex=surface.GetTextureID("milkHud/sprintcapright");
		--surface.SetTexture(tex);
		--surface.SetDrawColor(255,255,255,255); -- Makes sure the image draws correctly
		--surface.DrawTexturedRect( 20 + (ScrW()/4) * LocalPlayer():GetNWFloat("sprint") / 100 , 26, 12, 14 );
		
	--end
	
end


-- Taken from TS, tyvm
function DrawPropInfo()
	if( ValidEntity( LocalPlayer() ) ) then
		if( LocalPlayer():KeyDown( IN_USE ) ) then

			local trace = { }
			trace.start = LocalPlayer():EyePos();
			trace.endpos = trace.start + LocalPlayer():GetAimVector() * 300;
			trace.filter = LocalPlayer();
		
			local tr = util.TraceLine( trace );
		
			if( ValidEntity( tr.Entity ) and tr.Entity:GetNWString( "CreatorName" ) != "" ) then
		
				draw.DrawText( tr.Entity:GetNWString( "CreatorName" ) .. " ( " .. tr.Entity:GetNWString( "CreatorSteam" ) .. " ) " .. "created this.", "ChatFont", ScrW()/2, ScrH()/2, Color( 255, 0, 0, 255 ), 1 );
		
			end
		
		end
	end
end

function GM:HUDPaint()
	
	if(LocalPlayer():GetNWInt("deathmode") == 1) then
	
		DrawDeathMeter()
		
	end
	
	DrawTime();
	DrawPlayerTyping();
	--DrawPlayerInfo();
	DrawTargetInfo();
	DrawHudInfo();
	
	DrawPropInfo();
	
	drawFadedChatFrame();

	if(LocalPlayer():GetNWInt("ko") > 0) then
		DrawKO();
	end
	
	-- placeholder for now
	if(LocalPlayer():GetNWInt("tiedup") == 1) then
		draw.DrawText( "Tied up.", "ChatFont", ScrW() / 2, 70, Color(255,255,255,255),1 );
	end

end