#include "empire.h"

int show_winner()
{
	const int y = global.screen_height / 6;
	
	static bool first = true;
	static bool gotstats = false;
	static int time = 0;
	static int delay = 0;
	static int current = -1;
	static int winningteam = -1;
	static int left = 0;
	int i, t;
	char c[40];
	DyBytes data;
	
	worldmap.draw();
	do_sidebar(game->pturn, NULL, true);
	
	if (first)
	{
		gotstats = false;
		time = 0;
		delay = 0;
		current = -1;
		left = global.screen_width / 2 + 10;
		
		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_THEME);
		
		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);
		}
		
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				left -= 50;
			}
			i++;
		}
		
		delay = 0;
		current = -1;
		time++;
	}
	else if (time < 60)
	{
		buffer_textout_centre_ex(big, "Game Over", global.screen_width / 2, global.screen_height / 2, WHITE, float(time) / 60.0);
		time++;
	}
	else if (time < 128)
	{  //fade to black
		t = 128 - time;
		
		buffer_rectfill(0, 0, global.screen_width, global.screen_height, BLACK, (1.0 - (float(t) / 68.0)) * 0.75);
		buffer_textout_centre_ex(big, "Game Over", global.screen_width / 2, global.screen_height / 2, WHITE);
		time++;
	}
	else if (time < 188)
	{  //move player pictures up the screen while fading in
		time++;
		t = 188 - time;
		
		buffer_rectfill(0, 0, global.screen_width, global.screen_height, BLACK, 0.75);
		buffer_textout_centre_ex(big, "Game Over", global.screen_width / 2, global.screen_height / 2, WHITE);
		
		const float alpha = float(time - 128) / 60.0;
		
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				buffer_rectfill(left + (100 * i), y + (t * 6), left + (100 * i) + 83, y + 83 + (t * 6), player[i].tintcolor, alpha);
				buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), y + 5 + (t * 6), alpha);
			}
			i++;
		}
	}
	else if (time < 220)
	{  //pause for a bit
		const int yincrement = (global.screen_height / 2 - 50) / 32;  //32 interations in this section
		float alpha = 0;
		
		if (time - 188 < 10)
			alpha = 1.0 - (float(time - 188) / 10.0);
		else if (time - 188 > 22)
			alpha = float(time - 210) / 10.0;
		
		buffer_rectfill(0, 0, global.screen_width, global.screen_height, BLACK, 0.75);
		buffer_textout_centre_ex(big, "Game Over", global.screen_width / 2, global.screen_height / 2 - yincrement * (time - 188), WHITE, alpha);
		
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				buffer_rectfill(left + (100 * i), y, left + (100 * i) + 83, y + 83, player[i].tintcolor);
				buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), y + 2);
				if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, y + 85, WHITE);
			}
			i++;
		}
		time++;
	}
	else if (time == 220)
	{  //go through and replace defeated players with an X 
		buffer_rectfill(0, 0, global.screen_width, global.screen_height, BLACK, 0.75);
		buffer_textout_centre_ex(big, "Game Over", global.screen_width / 2, 50, WHITE);
		
		i = 0;
		while (i < 4)
		{
			if (player[i].everplaying == 1)
			{
				buffer_rectfill(left + (100 * i), y, left + (100 * i) + 83, y + 83, 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), y + 2);
					}
					else
					{
						buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), y + 2);
						if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, y + 85, WHITE);
					}
				}
			}
			else
			{
				if (player[i].everplaying == 1)
				{
					buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), y + 2);
					if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, y + 85, 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_rectfill(0, 0, global.screen_width, global.screen_height, BLACK, 0.75);
		buffer_textout_centre_ex(big, "Game Over", global.screen_width / 2, 50, WHITE);
		i = 0;
		t = 0;
		
		buffer_textout_ex(normal, "Money Raised  :", 100, y + 120, WHITE);
		buffer_textout_ex(normal, "Money Spent   :", 100, y + 140, WHITE);
		buffer_textout_ex(normal, "Units Created :", 100, y + 160, WHITE);
		buffer_textout_ex(normal, "Enemies Killed:", 100, y + 180, WHITE);
		buffer_textout_ex(normal, "Units Lost    :", 100, y + 200, 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, y + 120, WHITE);
				sprintf(c, "$%d", player[i].stats.spent);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, y + 140, WHITE);
				sprintf(c, "%d", player[i].stats.produced);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, y + 160, WHITE);
				sprintf(c, "%d", player[i].stats.killed);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, y + 180, WHITE);
				sprintf(c, "%d", player[i].stats.lost);
				buffer_textout_centre_ex(normal, c, left + (100 * i) + 40, y + 200, WHITE);
				
				buffer_rectfill(left + (100 * i), y, left + (100 * i) + 83, y + 83, player[i].tintcolor);
				if (player[i].team != winningteam)
				{
					glb.draw(19, left + 2 + (100 * i), y + 2);
				}
				else
				{
					t++;
					buffer_rectrounded(left - 1 + (100 * i), y - 1, left + 84 + (100 * i), y + 84, WHITE);
					buffer_draw_sprite(currentcharacter[i]->picture, left + 2 + (100 * i), y + 2);
					if (global.netgame) buffer_textout_centre_ex(med, player[i].name, left + (100 * i) + 40, y + 85, WHITE);
				}
			}
			i++;
		}
		if (t == 1)
		{
			buffer_textout_centre_ex(big, "The Winner!", global.screen_width / 2, y + 250, WHITE);
		}
		else
		{
			buffer_textout_centre_ex(big, "The Winners!", global.screen_width / 2, y + 250, WHITE);
		}
		buffer_textout_centre_ex(med, "Press Enter", global.screen_width / 2, global.screen_height * 7 / 8, WHITE);
		time++;
	}

	if (key[KEY_ENTER])
	{
		first = true;
		
		play_song(TITLE_THEME);
		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;
}
