//Copyright (C) 2007 SiegeLord
//See ReadMe.txt and License.txt for copying information

#include "neonz.h"
#include "camera.h"
#include "mouse.h"
#include "ship.h"
#include "world.h"
#include "quadtree.h"
#include "selection.h"
#include "orders.h"
#include "objectex.h"
#include "explosion.h"
#include "shipregistry.h"
#include "buildscreen.h"
#include "solararray.h"
#include "mainmenu.h"
#include "message.h"
#include "ai.h"
#include "sounds.h"

extern SCamera g_strCamera;

int g_nPlayer;
int g_nPlayerCash[2];

bool g_bQuit = false;

SObjectEx *g_obRootObject;
SShip* g_obMotherShip[2];

float g_fOut;

FONT* g_fntFont;

bool g_bDrawCollision = false;

void rester(void* data)
{
	rest(5);
}
END_OF_FUNCTION(rester)
void close_button_handler()
{
	g_bQuit = true;
}
END_OF_FUNCTION(close_button_handler)

int DoMainMenu()
{
	while(!g_bQuit)
    {
    	UpdateInput();
		UpdateSounds();

    	int ret = HandleMainMenu();
    	if(ret == 1)
    	{
    		return 1;
    	}
    	if(ret == 2)
    	{
    		g_bQuit = true;
    		return 2;
    	}
    	DrawMainMenu();

		DrawMouse();

    	Present();
    	WaitUntil(0.01, rester, 0, true);
    }
    return 0;
}

int DoGame()
{
	InitBuildScreen();

    InitCamera(1500, -AL_PI / 2, GetMinWorldRadius(), GetMaxWorldRadius());

	g_obRootObject = new SObjectEx();
    InitObject(g_obRootObject, NULL);

    g_qnRootNode = new SQuadNode();
    float nn = GetMaxWorldRadius() * 1.25;
    g_qnRootNode->extents.Set(-nn, -nn, nn, nn);

	int game_mode = 0;

	g_nPlayerCash[0] = 10000;
	g_nPlayerCash[1] = 10000;
	g_nPlayer = 0;

    CreateSolarArray(g_obRootObject, vector2d(1500, 1.0f * AL_PI / 3.0f));
    CreateSolarArray(g_obRootObject, vector2d(1500, 2.0f * AL_PI / 3.0f));
    CreateSolarArray(g_obRootObject, vector2d(1500, 4.0f * AL_PI / 3.0f));
    CreateSolarArray(g_obRootObject, vector2d(1500, 5.0f * AL_PI / 3.0f));

    CreateSolarArray(g_obRootObject, vector2d(1500, AL_PI / 12.0f))->player = g_nPlayer;
    CreateSolarArray(g_obRootObject, vector2d(1500, AL_PI + AL_PI / 12.0f))->player = 1 - g_nPlayer;

    g_obMotherShip[0] = CreateMotherShip(g_obRootObject);
    g_obMotherShip[0]->player = g_nPlayer;
    g_obMotherShip[0]->SetPolarPosition(1500, 0);
	AddObjectToQuadNode(g_qnRootNode, g_obMotherShip[0]);
	RegisterShip(g_obMotherShip[0]);

	g_obMotherShip[1] = CreateMotherShip(g_obRootObject);
    g_obMotherShip[1]->player = 1 - g_nPlayer;
    g_obMotherShip[1]->SetPolarPosition(1500, AL_PI);
	AddObjectToQuadNode(g_qnRootNode, g_obMotherShip[1]);
	RegisterShip(g_obMotherShip[1]);

	InitExplosionBuffers();

	SendMessage("Welcome to ODZ!");

    InitAI();

    float timer = -1;
    bool lost = false;
    bool won = false;

	SSoundFile* sound = FindSound("music.ogg");
	if(sound)
		PlaySound(sound, true);

	InitWorld();

    while(!g_bQuit)
    {
    	UpdateSounds();
    	//logic
    	UpdateInput();

    	if(IsKeyDown(KEY_Q) && IsCtrlDown())
    		break;

		if(IsKeyDown(KEY_B))
		{
			game_mode = 1;
		}

    	if(game_mode == 0)
    	{
			if(IsKeyJustPressed(KEY_T))
			{
				g_bDrawCollision = !g_bDrawCollision;
			}

			HandleCamera();

//			if(IsKeyJustPressed(KEY_SPACE))
//			{
//				vector2d pos = GetWorldMousePos();
//				SShip* ship = CreateShip(g_obRootObject);
//				ship->player = int(Rand(0, 2));
//				ship->SetPolarPosition(pos.GetLength(), atan2f(pos.y, pos.x));
//				AddObjectToQuadNode(g_qnRootNode, ship);
//				RegisterShip(ship);
//			}

			HandleOrders(&g_lstSelectedShips);
			HandleSelection(g_qnRootNode);
    	}
    	if(game_mode == 1)
    	{
    		int ret = HandleBuildScreen();
    		if(ret)
    		{
    			game_mode = 0;
    		}
    	}

    	HandleMessage();
    	HandleShipBuilding(g_nPlayer);
    	HandleShipBuilding(1 - g_nPlayer);

		DoObjectLogic(g_obRootObject);
		SubdivideQuadNode(g_qnRootNode);
		PruneNode(g_qnRootNode);

		HandleAI();

		//drawing

		if(game_mode == 0)
		{
			DrawWorld();
			DrawObject(g_obRootObject);
			DrawSelection();
			//SetCameraMatrix(g_trIdentity);
			if(g_bDrawCollision)
			{
				DrawModeAlpha();
				DrawQuadNode(g_qnRootNode);
				DrawModeSolid();
			}
		}

    	//gui stuff follows
		SetCameraMatrix(g_trIdentity);

		if(game_mode == 0)
		{
			char buf[256];
			sprintf(buf, "$%d", g_nPlayerCash[g_nPlayer]);
			DrawTextAlign(buf, SCREEN_W / 2, 10, -1, 0, 0xffccffcc, g_fntFont);

			//sprintf(buf, "$%f", g_fOut);
			//DrawTextAlign(buf, SCREEN_W / 2, 25, -1, 0, 0xffccffcc, g_fntFont);

			DrawMessage();
		}

		if(game_mode == 1)
		{
			DrawBuildScreen();
		}

		//lose/win conditions
		if((g_obMotherShip[g_nPlayer] == 0) && timer < 0.0f)
		{
			lost = true;
			timer = 5.0f;
			SendMessage("You have lost...");
		}
		if((g_obMotherShip[1 - g_nPlayer] == 0) && timer < 0.0f)
		{
			won = true;
			timer = 5.0f;
			SendMessage("You have won!");
		}

		if(timer > 0.0f)
			timer -= g_fDelta;

		if((won || lost) && timer < 0.0f)
		{
			break;
		}

		DrawMouse();
		SetCameraMatrix(g_strCamera.trans);

    	Present();
		WaitUntil(0.01, rester, 0, true);
    }

    StopAllSounds();

    ResetSolarArrays();
    SetCameraMatrix(g_trIdentity);
    DestroyQuadNode(g_qnRootNode);
    DeInitBuildScreen();
    DestroyObject(g_obRootObject);
    return 0;
}

int main()
{
	try
	{
    if(InitNeonZ() != 1)
		Throw("Could not initialize engine... :(");

	LOCK_FUNCTION(close_button_handler);
	set_close_button_callback(close_button_handler);

    InitFileIO("data", "");

	g_fntFont = FindFont("font.tga");

    Tick();
    WaitUntil(0.01, rester, 0, true);//set up our timers, set to 100 fps

    InitSounds();

    InitMainMenu();

    while(!g_bQuit)
    {
    	int ret = DoMainMenu();
    	if(ret == 1)
	    	DoGame();
    }

    DeInitMainMenu();
    DeInitNeonZ();
	}
	catch(SException* ex)
	{
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message(ex->text.c_str(), allegro_error);

		delete ex;
	}
    return 0;
}
END_OF_MAIN()
