#include "vote.qh"

#include <client/draw.qh>

// Vote (#9)

void HUD_Vote_Export(int fh)
{
	// allow saving cvars that aesthetically change the panel into hud skin files
	HUD_Write_Cvar("hud_panel_vote_alreadyvoted_alpha");
}

void HUD_Vote(bool should_draw)
{
	if (!should_draw)
		return;
	if (autocvar_cl_allow_uid2name == -1 && (MUTATOR_CALLHOOK(ShowRankings) || (serverflags & SERVERFLAG_PLAYERSTATS)))
	{
		// this dialog gets overriden by the uid2name menu dialog, if it exists
		// TODO remove this client side uid2name dialog in the next release
		if (!autocvar__menu_alpha)
			uid2name_dialog = 0;

		if (!uid2name_dialog)
			localcmd("menu_cmd directmenu Uid2Name\n");

		vote_active = true;
		if (autocvar__hud_configure)
		{
			vote_yescount = 0;
			vote_nocount = 0;
			LOG_INFO(_("^1You must answer before entering HUD configure mode"));
			cvar_set("_hud_configure", "0");
		}
		strcpy(vote_called_vote, _("^2Name ^7instead of \"^1Anonymous player^7\" in stats"));
		uid2name_dialog = 1;
	}

	if (!autocvar__hud_configure)
	{
		if (!autocvar_hud_panel_vote)
			return;
		/*
		if (cvar("hud_panel_vote_test"))
		{
			if (vote_called_vote)
				strunzone(vote_called_vote);
			vote_called_vote = strzone("^1test the vote panel");
			vote_active = true; vote_yescount = 3; vote_nocount = 2; vote_needed = 4;
		}
		else
			vote_active = false;
		*/
	}
	else
	{
		vote_yescount = 3;
		vote_nocount = 2;
		vote_needed = 4;
	}

	if (vote_active != vote_prev)
	{
		vote_change = time;
		vote_prev = vote_active;
	}

	if (vote_active || autocvar__hud_configure)
		vote_alpha = bound(0, (time - vote_change) * 2, 1);
	else
		vote_alpha = bound(0, 1 - (time - vote_change) * 2, 1);

	float a = vote_alpha * (vote_highlighted ? autocvar_hud_panel_vote_alreadyvoted_alpha : 1);
	if (a <= 0)
		return;

	// menu can't hide this panel, unless:
	// 1) the uid2name menu dialog is open (replaces this panel)
	// 2) the settings menu of another panel is open (hud_config mode)
	float hud_fade_alpha_save = hud_fade_alpha;
	if (uid2name_dialog && autocvar__menu_alpha)
		hud_fade_alpha = 0;
	else if (!(hud_configure_menu_open == 2 && highlightedPanel != panel))
		hud_fade_alpha = a;
	HUD_Panel_LoadCvars();
	hud_fade_alpha = hud_fade_alpha_save;

	if (uid2name_dialog)
	{
		panel_pos  = vec2(0.3 * vid_conwidth, 0.1 * vid_conheight);
		panel_size = vec2(0.4 * vid_conwidth, 0.3 * vid_conheight);
	}

	vector pos = panel_pos;
	vector mySize = panel_size;

	if (autocvar_hud_panel_vote_dynamichud)
		HUD_Scale_Enable();
	else
		HUD_Scale_Disable();
	HUD_Panel_DrawBg();

	if (panel_bg_padding)
	{
		pos += '1 1 0' * panel_bg_padding;
		mySize -= '2 2 0' * panel_bg_padding;
	}

	// force 3:1 aspect, but if the panel is thin force 3:2 and draw yes on top of no
	vector newSize = '0 0 0';
	bool yes_on_top;
	if (mySize.x / mySize.y > 3)
	{
		newSize.x = 3 * mySize.y;
		newSize.y = mySize.y;

		pos.x += (mySize.x - newSize.x) * 0.5;
		yes_on_top = false;
	}
	else if (mySize.x / mySize.y > 3/2)
	{
		newSize.y = (1/3) * mySize.x;
		newSize.x = mySize.x;

		pos.y += (mySize.y - newSize.y) * 0.5;
		yes_on_top = false;
	}
	else
	{
		newSize.y = (2/3) * mySize.x;
		newSize.x = mySize.x;

		pos.y += (mySize.y - newSize.y) * 0.5;
		yes_on_top = true;
	}
	mySize = newSize;

	string s = uid2name_dialog
		? _("Allow servers to store and display your name?")
		: _("A vote has been called for:");
	drawstring_aspect(pos, s, vec2(mySize.x, (2/8) * mySize.y), '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
	// has no padding afterwards
	if (autocvar__hud_configure)
		s = _("^1Configure the HUD");
	else
		s = textShortenToWidth(ColorTranslateRGB(vote_called_vote), mySize.x, '1 1 0' * mySize.y * (1/8), stringwidth_colors);
	drawcolorcodedstring_aspect(pos + eY * (2/8) * mySize.y, s, vec2(mySize.x, (1.75/8) * mySize.y), panel_fg_alpha, DRAWFLAG_NORMAL);
	// has 0.25/8 padding afterwards

	// print the yes/no counts
	float height = (yes_on_top ? 1.3/8 : 1.5/8);
	s = sprintf("^2%s ^7(%d)", getcommandkey_forcename(_("Yes"), "vyes"), vote_yescount);
	drawcolorcodedstring_aspect(pos + eY * (4/8) * mySize.y, s, vec2(0.5 * mySize.x, height * mySize.y), panel_fg_alpha, DRAWFLAG_NORMAL);
	s = sprintf("^1%s ^7(%d)", getcommandkey_forcename(_("No"), "vno"), vote_nocount);
	drawcolorcodedstring_aspect(pos + vec2(0.5 * mySize.x, (4/8) * mySize.y), s, vec2(0.5 * mySize.x, height * mySize.y), panel_fg_alpha, DRAWFLAG_NORMAL);
	// overlap with the bars by up to 0.1/8 if yes_on_top or otherwise 0.3/8
	// this overlap looks fine only since the bar images have padding

	pos.y += 5.2/8 * mySize.y;
	vector tmp_size, pos_no, pos_yes = pos;
	if (yes_on_top)
	{
		tmp_size = vec2(mySize.x * 2, ((8 - 5.2) * 0.5 + 0.1) / 8 * mySize.y);
		// bar images have padding, so let them overlap a bit (by 0.1/8)
		pos_no = pos + eY * ((8 - 5.2) * 0.5 - 0.1) / 8 * mySize.y - eX * mySize.x;
		// yes is drawn taking the full width,
		// ... then no is drawn directly underneath also taking the full width,
		// ... so drawpic position has to be shifted left by the full width
	}
	else
	{
		tmp_size = vec2(mySize.x, (8 - 5.2) / 8 * mySize.y);
		pos_no = pos;
		mySize.x *= 0.5;
	}

	// draw the progress bar backgrounds
	if (yes_on_top)
	{
		drawsetcliparea(pos_yes.x, pos_yes.y, mySize.x, mySize.y);
		drawpic_skin(pos_yes, "voteprogress_back", tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL); // yes
		drawsetcliparea(pos_no.x + mySize.x, pos_no.y, mySize.x, mySize.y);
		drawpic_skin(pos_no, "voteprogress_back", tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL); // no
	}
	else
		drawpic_skin(pos, "voteprogress_back", tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL); // both, in a line

	// draw the highlights
	if (vote_highlighted == 1) // yes
	{
		drawsetcliparea(pos_yes.x, pos_yes.y, mySize.x, mySize.y);
		drawpic_skin(pos_yes, "voteprogress_voted", tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
	}
	else if (vote_highlighted == -1) // no
	{
		drawsetcliparea(pos_no.x + mySize.x, pos_no.y, mySize.x, mySize.y);
		drawpic_skin(pos_no, "voteprogress_voted", tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
	}

	// draw the progress bars
	if (vote_yescount && vote_needed) // yes
	{
		drawsetcliparea(pos_yes.x, pos_yes.y, mySize.x * (vote_yescount / vote_needed), mySize.y);
		drawpic_skin(pos_yes, "voteprogress_prog", tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
	}
	if (vote_nocount && vote_needed) // no
	{
		drawsetcliparea(pos_no.x + mySize.x * 2 - mySize.x * (vote_nocount / vote_needed), pos_no.y, mySize.x, mySize.y);
		drawpic_skin(pos_no, "voteprogress_prog", tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
	}

	drawresetcliparea();
}
