
local death_notices = {};

surface.CreateFont("Apple Kid", ScreenScale(24), 500, true, false, "UCText");
surface.CreateFont("Twoson", ScreenScale(12), 500, true, false, "UCTextPly");
surface.CreateFont("Twoson", ScreenScale(6), 500, true, false, "UCTextPlySmall");

local sweatbead = Material("chimerahunt/hud_sweat_bead");

local sprintbar = surface.GetTextureID("chimerahunt/hud_sprint_bar");
local ucsprintbar = surface.GetTextureID("chimerahunt/hud_sprint_bar_UC");

local mat_xhair = surface.GetTextureID("chimerahunt/hud_pig_crosshair");

local mat_chimera_death = surface.GetTextureID("chimerahunt/killicons/chimera");
local mat_pig_death = surface.GetTextureID("chimerahunt/killicons/pig");
local mat_pig_suicide = surface.GetTextureID("chimerahunt/killicons/pigsuicide");


local pigranks = {};
pigranks["ensign"] = {};
pigranks["captain"] = {};
pigranks["major"] = {};
pigranks["colonel"] = {};

local pigmoods = {"rest", "walk", "run", "stand", "inair", "dead", "sprint"};
local ranks = {"ensign", "captain", "major", "colonel"};

for k, v in pairs(pigranks) do

	for j, c in pairs(pigmoods) do
	
		v[c] = surface.GetTextureID("chimerahunt/" .. k .. "/hud_pig_" .. c);
		
	end
	
	/*v.tbl = {
		{name = "rest", tex = surface.GetTextureID("chimerahunt/" .. v.name .. "/hud_pig_rest")},
		{name = "walk", tex = surface.GetTextureID("chimerahunt/" .. v.name .. "/hud_pig_walk")},
		{name = "run", tex = surface.GetTextureID("chimerahunt/" .. v.name .. "/hud_pig_run")},
		{name = "dead", tex = surface.GetTextureID("chimerahunt/" .. v.name .. "/hud_pig_dead")},
		{name = "stand", tex = surface.GetTextureID("chimerahunt/" .. v.name .. "/hud_pig_stand")},
		{name = "inair", tex = surface.GetTextureID("chimerahunt/" .. v.name .. "/hud_pig_inair")}
	};*/
	
end

local function ConvertRank(ply)
	local rank = ply:GetNWInt("PigRank", 1);
	local rtn = "Ensign";
	if (rank == 2) then
		rtn = "Captain";
	end
	if (rank == 3) then
		rtn = "Major";
	end
	if (rank == 4) then
		rtn = "Colonel";
	end
	if (ply:GetNWBool("IsChimera")) then
		rtn = "Ultimate Chimera";
	end
	return rtn;
end

local function TextSize(font, txt)
	surface.SetFont(font);
	local w, h = surface.GetTextSize(txt);
	return {w, h};
end


/*local pig_rest = surface.GetTextureID("chimerahunt/hud_pig_rest");
local pig_walk = surface.GetTextureID("chimerahunt/hud_pig_walk");
local pig_run = surface.GetTextureID("chimerahunt/hud_pig_run");
local pig_dead = surface.GetTextureID("chimerahunt/hud_pig_dead");
local pig_stand = surface.GetTextureID("chimerahunt/hud_pig_stand");
local pig_inair = surface.GetTextureID("chimerahunt/hud_pig_inair");

local pigmoods = {};
pigmoods["rest"] = pig_rest;
pigmoods["walk"] = pig_walk;
pigmoods["run"] = pig_run;
pigmoods["stand"] = pig_stand;
pigmoods["inair"] = pig_inair;*/

local sw, sh = ScrW(), ScrH();

local function DrawShadowedText(txt, font, x, y, clr, alignx, aligny, dis, alpha)
	
	local tbl = {};
	tbl.pos = {};
	tbl.pos[1] = x;
	tbl.pos[2] = y;
	tbl.color = clr;
	tbl.text = txt;
	tbl.font = font;
	tbl.xalign = alignx;
	tbl.yalign = aligny;
	
	draw.TextShadow(tbl, dis, alpha);
	
end


local function ShowMessage(txt)

	local ply = LocalPlayer();
	ply.UCAlpha = (ply.UCAlpha || 0);
	ply.UCAlpha = math.Approach(ply.UCAlpha, ply.UCAlphaGoal, (FrameTime() * 250));
	
	local a = ply.UCAlpha;
	DrawShadowedText(txt, "UCText", (sw * .5), 0, Color(255, 255, 255, a), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP, 2, a);
	
end


local function DrawSprintMeter()
	
	local ply = LocalPlayer();
	local a = ply.SprintAlpha;
	local tex = ucsprintbar;
	
	local x, y, w, h = ply.SprintX, ply.SprintY, ply.SprintW, ply.SprintH;
	
	w = (w * ply:GetNWInt("UCSprint"));
	w = math.Clamp(w, 2, (sh * .3));
	
	draw.RoundedBox(0, (x - 2), (y - 2), ((sh * .3) + 4), ((sh * .02) + 4), Color(25, 25, 25, a));
	
	if (!ply:GetNWBool("IsChimera")) then
		draw.RoundedBox(0, (x + ((sh * .3) * .25)), y, ((sh * .3) - ((sh * .3) * .25)), (sh * .02), Color(155, 155, 155, a));
		draw.RoundedBox(0, x, y, ((sh * .3) * .25), (sh * .02), Color(125, 125, 125, a));
		local rank = ConvertRank(LocalPlayer()):lower();
		tex = pigranks[rank]["sprint"];
	else
		draw.RoundedBox(0, x, y, (sh * .3), (sh * .02), Color(155, 155, 155, a));
	end

	
	surface.SetTexture(tex);
	surface.SetDrawColor(255, 255, 255, a);
	surface.DrawTexturedRect(x, y, w, h);
		
end


local function GetStopSprint(um)
	LocalPlayer().UCSprinting = false;
end
usermessage.Hook("UCStopSprint", GetStopSprint);

local function Get2ndDelay(um)
	LocalPlayer().SecondDelay = (CurTime() + GAMEMODE.UCRoarDelay);
end
usermessage.Hook("UC2ndDelay", Get2ndDelay);


local function DrawPigMask()
	
	local ply = LocalPlayer();
	
	if (!ply:GetNWBool("IsChimera")) then
	
		local rank = ConvertRank(LocalPlayer()):lower();
		
		local mood = "walk";
		
		local x, y, w, h = ply.SprintX, ply.SprintY, ply.SprintW, ply.SprintH;
		
		local pigw, pigh = (sh * .1), (sh * .11);
		
		if (ply:Alive()) then
			if (ply:GetNWBool("UCScared")) then
				surface.SetTexture(pigranks[rank]["run"]);
			else
				if (ply:GetNWInt("UCSprint") > 0) then
					if (ply:IsOnGround()) then
						if (ply.UCSprinting) then
							mood = "run";
							if (ply:GetNWInt("PigRank") == 4) then
								pigw = (sh * .1075);
							end
						else
							if (!ply:KeyDown(IN_FORWARD) && !ply:KeyDown(IN_BACK) && !ply:KeyDown(IN_MOVELEFT) && !ply:KeyDown(IN_MOVERIGHT)) then
								mood = "stand";
								pigw = (sh * .076);
								x, y = (x + 1), (y + 1);
							end
						end
					else
						if (ply:GetVelocity():Length() > 2) then
							mood = "inair";
							pigw = (sh * .092);
							if (ply:GetNWInt("PigRank") == 4) then
								pigw = (sh * .098);
							end
						end
					end
				else
					pigh = pigw;
					mood = "rest";
				end
				//chat.AddText(tostring(rank) .. " " .. tostring(mood));
				surface.SetTexture(pigranks[rank][mood]);
			end
		else
			pigh = pigw;
			surface.SetTexture(pigranks[rank]["dead"]);
		end
		
		surface.SetDrawColor(255, 255, 255, 255);
		surface.DrawTexturedRect(((x + (w * .25)) - (pigw * .5)), (y - (pigh * 1.1)), pigw, pigh);
		
	else
		
		//chimera hud
		
	end
	
end


local function DrawPigCrosshair()
	
	if (LocalPlayer():Alive() && !LocalPlayer():GetNWBool("IsChimera")) then
		
		if (!LocalPlayer().XHairAlpha) then
			LocalPlayer().XHairAlpha = GAMEMODE.XHairAlpha;
		end
		
		LocalPlayer().XHairAlpha = math.Approach(LocalPlayer().XHairAlpha, GAMEMODE.XHairAlpha, (FrameTime() * 200));
		
		local w, h = (sh * .05), (sh * .05);
		surface.SetTexture(mat_xhair);
		surface.SetDrawColor(255, 255, 255, LocalPlayer().XHairAlpha);
		surface.DrawTexturedRect(((sw * .5) - (w * .5)), ((sh * .5) - (h * .5)), w, h);
	end
	
end


local function DrawPigSweats()
	
	for k, v in ipairs(player.GetAll()) do
	
		//pixel visible stuff
		if (!v.PixVis) then
			v.PixVis = util.GetPixelVisibleHandle();
		end
		if (!v.SweatAlpha) then
			v.SweatAlpha = 0;
		end
		
		local vis = util.PixelVisible(v:GetShootPos(), 16, v.PixVis);
		if (v:GetNWBool("UCScared") && vis && vis != 0) then
			v.SweatAlpha = math.Approach(v.SweatAlpha, 250, (FrameTime() * 750));
		else
			v.SweatAlpha = math.Approach(v.SweatAlpha, 0, (FrameTime() * 1250));
		end
		
		if (v.SweatAlpha > 0) then
			cam.Start3D(EyePos(), EyeAngles())
				render.SetMaterial(sweatbead);
				local pos = (v:GetShootPos() + Vector(0, 0, 22));
				pos = (pos + (EyeAngles():Right():GetNormal() * 12));
				local clr = Color(255, 255, 255, v.SweatAlpha);
				render.DrawSprite(pos, 16, 12, clr);
			cam.End3D()
		end
		
	end
	
end


local function DrawHelpText()

	if (GAMEMODE:GetState() == GAMEMODE.STATE_PLAYING) then
		
		local ply = LocalPlayer();
		if (ply:GetNWBool("IsChimera")) then
			
			if (ply:GetNWBool("UCText")) then
				ply.HTextAlpha = math.Approach(ply.HTextAlpha, 255, (FrameTime() * 500));
			else
				ply.HTextAlpha = math.Approach(ply.HTextAlpha, 0, (FrameTime() * 250));
			end
			
		else
		
			if (ply:GetNWBool("PigText")) then
				ply.HTextAlpha = math.Approach(ply.HTextAlpha, 255, (FrameTime() * 500));
			else
				ply.HTextAlpha = math.Approach(ply.HTextAlpha, 0, (FrameTime() * 250));
			end
			
		end
		
		if (ply.HTextAlpha > 0) then
			local txt = "";
			if (ply:GetNWBool("IsChimera")) then
				txt = GAMEMODE.UCHelpText;
			else
				txt = GAMEMODE.PigHelpText;
			end
			local y = 0;
			for k, v in ipairs(string.Explode("\n", txt)) do
			
				DrawShadowedText(v, "UCTextPly", (sw * .5), ((sh * .1) + y), Color(255, 255, 255, ply.HTextAlpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, (ply.HTextAlpha * .5));
				y = (y + 24);
		
			end
		end
		
	end

end


local function GetKillNotice(um)
	local ply, ply2 = um:ReadEntity(), um:ReadEntity();
	local clr, clr2 = team.GetColor(ply:Team()), team.GetColor(ply2:Team());
	local tex = mat_pig_death;
	if (ply == ply2) then
		tex = mat_pig_suicide;
	end
	if (ply:GetNWBool("IsChimera")) then
		tex = mat_chimera_death;
	end
	table.insert(death_notices, {ply, ply2, CurTime(), clr, clr2, tex});
end
usermessage.Hook("SendKillNotice", GetKillNotice);


local function DrawKillIcons()
	
	local y = 0;
	
	for k, v in ipairs(death_notices) do
		
		local ply, ply2, tm, clr, clr2, tex = v[1], v[2], v[3], v[4], v[5], v[6];
		
		if ((tm + GAMEMODE.DeathNoticeTime) > CurTime()) then //draw this one
		
			local alpha = ((tm + GAMEMODE.DeathNoticeTime) - CurTime());
			alpha = math.Clamp((alpha * 255), 0, 255);
			
			clr.a = alpha;
			clr2.a = alpha;
			
			DrawShadowedText(ply:Name(), "UCTextPly", (sw * .795), ((sh * .05) + y), clr, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER, 2, alpha);
			
			local w, h = (sh * .06), (sh * .041);
			if (tex == mat_chimera_death) then
				w, h = (sh * .06), (sh * .06);
			end
			surface.SetTexture(tex);
			surface.SetDrawColor(255, 255, 255, alpha);
			surface.DrawTexturedRect(((sw * .825) - (w * .5)), (((sh * .05) + y) - (h * .5)), w, h);
			
			DrawShadowedText(ply2:Name(), "UCTextPly", (sw * .855), ((sh * .05) + y), clr2, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, 2, alpha);
			
			y = (y + 44);
			
		else
			table.remove(death_notices, k);
		end
		
	end
	
end


local function DrawPlayers() //spectate draw
	
	for k, v in ipairs(player.GetAll()) do
		if (v:Alive() && v != LocalPlayer()) then
			local pos = v:GetPos();
			local dis = LocalPlayer():GetPos():Distance(pos);
			if (dis <= GAMEMODE.PlayerNameDistance) then
				
				pos = (v:GetShootPos() + Vector(0, 0, 20));
				pos = pos:ToScreen();
				
				local visible = util.PixelVisible(v:GetShootPos(), 16, v.PixVis);
				local alpha = ((255 && visible) || 0);
				
				if (alpha != 0) then
					local size = TextSize("UCTextPlySmall", ConvertRank(v));
					DrawShadowedText(ConvertRank(v), "UCTextPlySmall", pos.x, (pos.y - (size[2] + (size[2] * .2))), Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, 250);
					DrawShadowedText(v:Name(), "UCTextPly", pos.x, pos.y, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, 250);
				end
				
			end
		end
	end
	
end


function GM:HUDPaint()
	
	local ply = LocalPlayer();
	
	ply.SprintFade = (ply.SprintFade || "out");
	ply.SprintAlpha = (ply.SprintAlpha || 1);
	
	ply.PigMood = (ply.PigMood || "walk");
	
	if (!ply.SprintX) then
		ply.SprintX = (sw * .05);
	end
	if (!ply.SprintY) then
		ply.SprintY = (sh * .95);
	end
	if (!ply.SprintW) then
		ply.SprintW = (sh * .3);
	end
	if (!ply.SprintH) then
		ply.SprintH = (sh * .02);
	end
	if (!ply.PlyAlpha) then
		ply.PlyAlpha = 0;
	end
	if (!ply.PlyInfo) then
		ply.PlyInfo = 0;
	end
	if (!ply.HTextAlpha) then
		ply.HTextAlpha = 0;
	end
		
	if (self:GetState() == self.STATE_WAITING) then //Waiting for players...
		ply.UCAlphaGoal = 255;
		ShowMessage("Waiting for players...");
	elseif (self:GetState() == self.STATE_STARTING) then
		ply.UCAlphaGoal = 255;
		ShowMessage("Game commencing!");
	else
		ply.UCAlphaGoal = 0;
	end
	
	if (ply.SprintFade == "out") then
		ply.SprintAlpha = math.Approach(ply.SprintAlpha, 0, (FrameTime() * 250));
	else
		ply.SprintAlpha = math.Approach(ply.SprintAlpha, 255, (FrameTime() * 400));
	end
	
	if (ply:GetNWInt("UCSprint") < 1 && ply:GetNWInt("UCSprint") > 0 && !ply:GetNWBool("UCScared")) then
		ply.SprintFade = "in";
	end
	if (!ply:Alive() || ply:GetNWInt("UCSprint") <= .025 || ply:GetNWInt("UCSprint") >= 1 || ply:GetNWBool("UCScared")) then
		ply.SprintFade = "out";
	end
	
	if (ply.SprintAlpha > 0) then
		DrawSprintMeter();
	end
	
	DrawPigMask();
	
	local ply2 = ply:GetNWEntity("UCSpec");
	if (ply:GetNWInt("SpecMode") == 1 && !ply.DrawUCScores) then
		if (ValidEntity(ply2) && ply2 != ply) then
			local size = TextSize("UCTextPlySmall", ConvertRank(ply2));
			DrawShadowedText(ConvertRank(ply2), "UCTextPlySmall", (sw * .5), ((sh * .46) - (size[2] + (size[2] * .2))), Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, 250);
			DrawShadowedText(ply2:Name(), "UCTextPly", (sw * .5), (sh * .46), Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, 250);
		end
	end
	
	local tr = ply:GetEyeTrace();
	if (ValidEntity(tr.Entity) && tr.Entity:IsPlayer() && tr.Entity:Alive() && !tr.Entity:GetNWBool("IsChimera") && !ply.DrawUCScores && !ply:GetNWBool("Taunting")) then
		ply.PlyAlpha = math.Approach(ply.PlyAlpha, 255, (FrameTime() * 1000));
		ply.PlyInfo = tr.Entity;
	else
		ply.PlyAlpha = math.Approach(ply.PlyAlpha, 0, (FrameTime() * 800));
	end
	
	if (ply:Alive()) then
		if (!ValidEntity(ply2)) then
			if (!ply:GetNWBool("IsChimera") && ply.PlyAlpha > 0) then
			
				DrawShadowedText(ConvertRank(ply.PlyInfo), "UCTextPlySmall", (sw * .5), (sh * .435), Color(255, 255, 255, ply.PlyAlpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, .5, ply.PlyAlpha);

				DrawShadowedText(ply.PlyInfo:Name(), "UCTextPly", (sw * .5), (sh * .46), Color(255, 255, 255, ply.PlyAlpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, ply.PlyAlpha);
				
			end
		end
	else
		if (ply:GetNWInt("SpecMode") == 2) then
			DrawPlayers();
		end
	end
	
	if (!ply.DrawUCScores && !ply:GetNWBool("Taunting")) then
		DrawPigCrosshair();
	end
	
	DrawPigSweats();
	
	DrawHelpText();
	
	DrawKillIcons();
	
	ply.ScoresAlpha = (ply.ScoresAlpha || 0);
	
	if (ply.DrawUCScores) then
		ply.ScoresAlpha = math.Approach(ply.ScoresAlpha, 255, (FrameTime() * 1200));
	else
		ply.ScoresAlpha = math.Approach(ply.ScoresAlpha, 0, (FrameTime() * 1000));
	end
	
	if (ply.ScoresAlpha > 0) then
		self:DrawScores();
	end
		
end

function GM:HUDShouldDraw(name)  

	local hud = {
	"CHudHealth",  
	"CHudBattery",  
	"CHudAmmo",  
	"CHudSecondaryAmmo",
	"CHudCrosshair"
	};
	
	for _, v in ipairs(hud) do
		if (name == v) then
			return false;
		end
	end
	return true;
	
end  
