
local ranks = {"ensign", "captain", "major", "colonel", "chimera"};

local tex = {};
for k, v in pairs(ranks) do
	tex[v] = surface.GetTextureID("chimerahunt/scoreboard/" .. v .. "_run");
	tex[v .. "_dead"] = surface.GetTextureID("chimerahunt/" .. v .. "/hud_pig_dead");
end
tex["chimera_dead"] = surface.GetTextureID("chimerahunt/scoreboard/chimera_dead");

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

function GM:ScoreboardShow()
	LocalPlayer().DrawUCScores = true;
	return true; //hide default scoreboard
end

function GM:ScoreboardHide()
	LocalPlayer().DrawUCScores = false;
end

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 DrawChimera(ply, x, y, w, h)
	
	local nx, ny = (x - (w * .5)), (y - (h * .5));
	
	local uc = tex["chimera"];
	if (!ply:Alive()) then
		uc = tex["chimera_dead"];
		w = (w * 1.15);
		h = (h * .752);
		ny = (ny + (ny * .05));
		y = (y + 4);
	end
	
	surface.SetTexture(uc);
	surface.SetDrawColor(Color(255, 255, 255, LocalPlayer().ScoresAlpha));
	surface.DrawTexturedRect(nx, ny, w, h);
	
	y = (y - (ScrH() * .06));
	local bounce = math.sin((CurTime() * 5));
	y = (y + (bounce * (ScrH() * .006)));
	
	surface.SetFont("UCTextPlySmall");
	local _, fh = surface.GetTextSize(ConvertRank(ply));
	
	local clr = Color(255, 255, 255, LocalPlayer().ScoresAlpha);
	
	if (ply == LocalPlayer()) then
		local num = math.sin((CurTime() * 5));
		num = math.Clamp((255 - ((num * 255) * .3)), 175, 255);
		clr = Color(num, num, num, clr.a);
	end
	
	DrawShadowedText(ConvertRank(ply), "UCTextPlySmall", x, (y - (fh + 2)), clr, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, (LocalPlayer().ScoresAlpha * .5));
	DrawShadowedText(ply:Name(), "UCTextPly", x, y, clr, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, (LocalPlayer().ScoresAlpha * .5));
	
end

local function DrawPigMask(ply, x, y, w, h)
	
	local nx, ny = (x - (w * .5)), (y - (h * .5));
	
	local str = ConvertRank(ply):lower();
	
	local clr = Color(255, 255, 255, LocalPlayer().ScoresAlpha);
	local txtclr = clr;
	
	if (ply == LocalPlayer()) then
		local num = math.sin((CurTime() * 5));
		num = math.Clamp((255 - ((num * 255) * .3)), 175, 255);
		txtclr = Color(num, num, num, clr.a);
	end
	
	local pig = tex[str];
	if (!ply:Alive()) then
		pig = tex[str .. "_dead"];
		clr = Color(150, 150, 150, clr.a);
		txtclr = clr;
	end
	
	surface.SetTexture(pig);
	surface.SetDrawColor(clr);
	surface.DrawTexturedRect(nx, ny, w, h);
	
	y = (y - (ScrH() * .045));
	local bounce = math.sin((CurTime() * 5));
	y = (y + (bounce * (ScrH() * .006)));
	
	surface.SetFont("UCTextPlySmall");
	local _, fh = surface.GetTextSize(ConvertRank(ply));
	
	DrawShadowedText(ConvertRank(ply), "UCTextPlySmall", x, (y - (fh + 2)), txtclr, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, (LocalPlayer().ScoresAlpha * .5));
	DrawShadowedText(ply:Name(), "ScoreboardText", x, y, txtclr, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, (LocalPlayer().ScoresAlpha * .5));
	
end

local even = {};
even["0"] = "0";
even["2"] = "2";
even["4"] = "4";
even["6"] = "6";
even["8"] = "8";

local function evenodd(num)
	local num = tostring(math.floor(num)):Right(1);
	if (even[num]) then
		return "even";
	else
		return "odd";
	end
	return nil;
end

local function nonchimera()
	local tbl = {};
	for k, v in ipairs(player.GetAll()) do
		if (!v:GetNWBool("IsChimera")) then
			table.insert(tbl, v);
		end
	end
	return tbl;
end

local sw, sh = ScrW(), ScrH();
function GM:DrawScores()
	
	local ratio = 0;
	
	local pw, ph = (sh * .062), (sh * .072);
	
	local ply = LocalPlayer();
	local uc = self:GetChimera();
	
	if (ValidEntity(uc)) then
		ratio = .075;
		local w, h = (sh * .12), (sh * .11);
		DrawChimera(uc, (sw * (.285 - ratio)), (sh * .5), w, h);
	end
	
	local x, y = 0, 0;
	local xnum = self.PigsOnRow;
	local kay = 1;
	for k, v in pairs(nonchimera()) do
		
		if (kay == (xnum + 1)) then
			xnum = self.PigsOnRow;
			x = (x + (pw + (pw * 1.52)));
			y = 0;
			kay = 1;
		end
		
		local num = evenodd(kay);
		
		local ny = ((sh * .5) - y);
		
		if (num == "even") then
			y = (y + (ph + (ph * .88)));
			ny = ((sh * .5) + y);
		end
		
		DrawPigMask(v, ((sw * (.4 + ratio)) + x), ny, pw, ph);
		
		kay = (kay + 1);
			
	end
	
end
