#include "empire.h"

int show_winner()
{
	static bool first = true;
	static bool gotstats = false;
	static int time = 0;
	static int delay = 0;
	static int current = -1;
	static int left = 0;
	static int winningteam = -1;
	int i, t;
	char c[40];
	DyBytes data;
	
	if (first)
	{
		gotstats = false;
		time = 0;
		delay = 0;
		current = -1;
		left = 0;
		
		for (i = 0; i < 4; i++)
		{
			if (player[i].playing == 1)
			{
				winningteam = player[i].team;
				break;
			}
		}
		
		first = false;
	}
  
	if (global.netgame && !net.server && !gotstats)
	{
		if (get_host_message(data))
		{
			if (data.getByte(0) == NET_STATS)
			{
				data.removeFront(1);
				data.decompress();
				i = 0;
				while (i < 4)
				{
					data.extractVariable(0, player[i].stats.raised);
					data.removeFront(sizeof(int));
					data.extractVariable(0, player[i].stats.spent);
					data.removeFront(sizeof(int));
					data.extractVariable(0, player[i].stats.produced);
					data.removeFront(sizeof(int));
					data.extractVariable(0, player[i].stats.killed);
					data.removeFront(sizeof(int));
					data.extractVariable(0, player[i].stats.lost);
					data.removeFront(sizeof(int));
					i++;
				}
				gotstats = true;
			}
		}
	}
	
	if (time == 0)
	{
		stop_music();
		play_song(victory_music);
		
		if (global.netgame && net.server)
		{
			data.clear();
			i = 0;
			while (i < 4)
			{
				data.appendVariable(player[i].stats.raised);
				data.appendVariable(player[i].stats.spent);
				data.appendVariable(player[i].stats.produced);
				data.appendVariable(player[i].stats.killed);
				data.appendVariable(player[i].stats.lost);
				i++;
			}
			data.compress(9);
			data.insertByte(NET_STATS);
			net.engine->broadcastTCP(data);
		}
		
		left = 410;
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				left -= 50;
			}
			i++;
		}
		
		delay = 0;
		current = -1;
		time++;
	}
	else if (time < 60)
	{
		t = 60 - time;
		buffer_rectfill(0, 0, 399 * time / 60, 600, DGREY);
		buffer_vline(400 * time / 60, 0, 600, GREY);
		buffer_vline(401 * time / 60, 0, 600, LGREY);
		buffer_rectfill(400 + (401 * t / 60), 0, 800, 600, DGREY);
		buffer_vline(400 + (400 * t / 60), 0, 600, GREY);
		buffer_vline(399 + (400 * t / 60), 0, 600, LGREY);
		buffer_textout_centre_ex(big, "Game Over", 400, time - 50, WHITE);
		time++;
	}
	else if (time < 128)
	{  //fade to black
		t = 128 - time;
		buffer_clear_to_color(makecol(t, t, t));
		buffer_textout_centre_ex(big, "Game Over", 400, 10, WHITE);
		time++;
	}
	else if (time < 188)
	{  //move player pictures from the bottom of the screen to y = 100
		time++;
		t = 188 - time;
		buffer_textout_centre_ex(big, "Game Over", 400, 10, WHITE);
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				buffer_rectfill(left + (100 * i), 100 + (t * 6), left + (100 * i) + 83, 183 + (t * 6), player[i].tintcolor);
				buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), 102 + (t * 6));
			}
			i++;
		}
	}
	else if (time < 220)
	{  //pause for a bit
		buffer_textout_centre_ex(big, "Game Over", 400, 10, WHITE);
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				buffer_rectfill(left + (100 * i), 100, left + (100 * i) + 83, 183, player[i].tintcolor);
				buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), 102);
				if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, 185, WHITE);
			}
			i++;
		}
		time++;
	}
	else if (time == 220)
	{  //go through and replace defeated players with an X 
		buffer_textout_centre_ex(big, "Game Over", 400, 10, WHITE);
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				buffer_rectfill(left + (100 * i), 100, left + (100 * i) + 83, 183, player[i].tintcolor);
			}
			i++;
		}
		i = 0;
		while (i < 4)
		{
			if (i <= current)
			{
				if (player[i].everplaying == 1)
				{
					if (player[i].team != winningteam)
					{
						glb.draw(19, left + 2 + (100 * i), 102);
					}
					else
					{
						buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), 102);
						if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, 185, WHITE);
					}
				}
			}
			else
			{
				if (player[i].everplaying == 1)
				{
					buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), 102);
					if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, 185, WHITE);
				}
			}
			i++;
		}
		if ((delay == 0) && (current < 4))
		{
			delay = 25;
			current++;
			if ((player[current].everplaying == 1) && (player[current].team != winningteam))
			{
				i = rand()%3;
				switch(i)
				{
				case 0:
					play_sound(S_TANKFIRE);
					break;
				case 1:
					play_sound(S_MDTANKFIRE);
					break;
				case 2:
					play_sound(S_EXPLOSION);
					break;
				}
			}
		}
		else if (current < 4)
		{
			delay--;
		}
		if (current == 4)
		{
			time++;  //keeps this section active until current == 4
		}
	}
	else
	{
		if (time == 221)
		{
			play_sound(S_VICTORY);
		}
		else
		{
			time--;  //just to keep time from increasing any more
		}
		buffer_textout_centre_ex(big, "Game Over", 400, 10, WHITE);
		i = 0;
		t = 0;
		
		buffer_textout_ex(normal, "Money Raised  :", 100, 220, WHITE);
		buffer_textout_ex(normal, "Money Spent   :", 100, 240, WHITE);
		buffer_textout_ex(normal, "Units Created :", 100, 260, WHITE);
		buffer_textout_ex(normal, "Enemies Killed:", 100, 280, WHITE);
		buffer_textout_ex(normal, "Units Lost    :", 100, 300, WHITE);
		
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				sprintf(c, "$%d", player[i].stats.raised);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, 220, WHITE);
				sprintf(c, "$%d", player[i].stats.spent);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, 240, WHITE);
				sprintf(c, "%d", player[i].stats.produced);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, 260, WHITE);
				sprintf(c, "%d", player[i].stats.killed);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, 280, WHITE);
				sprintf(c, "%d", player[i].stats.lost);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, 300, WHITE);
				
				buffer_rectfill(left + (100 * i), 100, left + (100 * i) + 83, 183, player[i].tintcolor);//player_color(i));
				if (player[i].team != winningteam)
				{
					glb.draw(19, left + 2 + (100 * i), 102);
				}
				else
				{
					t++;
					buffer_rectrounded(left - 1 + (100 * i), 99, left + 84 + (100 * i), 184, WHITE);
					buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), 102);
					if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, 185, WHITE);
				}
			}
			i++;
		}
		if (t == 1)
		{
			buffer_textout_centre_ex(big, "The Winner!", 400, 350, WHITE);
		}
		else
		{
			buffer_textout_centre_ex(big, "The Winners!", 400, 350, WHITE);
		}
		buffer_textout_centre_ex(normal, "Press Enter", 400, 570, WHITE);
		time++;
	}

	if (key[KEY_ENTER])
	{
		first = true;
		
		play_song(title_music);
		position_mouse(400, 300);
		time = 0;
		player[0].reset();
		player[1].reset();
		player[2].reset();
		player[3].reset();
		winningteam = -1;
		
		if (global.netgame)
			return NETWORK;
		
		return MENU;
	}
	
	return GAMEOVER;
}
