#include "Game.h"
#include "Parser.h"
#include "List.h"
#include "Control.h"
#include "Player.h"

using namespace std;

Game::Game(int levelnum)
:_currentLevel( NULL )
{
	/*glfuncs* f = glfuncs::instance();

	_scramSurface = f->load_image("scramble.png");
	_scram = f->SDL_GL_LoadTexture(_scramSurface, _scramCoords);*/

	// Load in the level list text file
	List* levelList = Parser::parse( "levels/levellist.txt" );
	while( !levelList->empty() )
	{
		_levelList.push( levelList->firstList()->firstString() );
		levelList = levelList->rest();
	}
	// End loading level list

	for(int i=0; i<levelnum; i++)
		_levelList.pop();

	// Load the first level
	loadLevel( _levelList.front() );
}

Game::~Game()
{
	delete _currentLevel;
}

bool Game::nextLevel()
{
	_levelList.pop();
	if( _levelList.empty() )
	{
		cerr << "Error loading level: No next level" << endl;
		return false;
	}
	/*Player * oldPlayer = NULL;
	if((Player *) _currentLevel->getPlayer())
		oldPlayer =(Player *) _currentLevel->getPlayer()->copy();
	else
		oldPlayer = NULL;*/

	delete _currentLevel;
	_currentLevel = new Level();
	_currentLevel->levelLoad( _levelList.front());
	return true;
}

void Game::loadLevel( std::string filename )
{
	if(!_currentLevel)
		delete _currentLevel;
	_currentLevel = new Level();
	_currentLevel->levelLoad( filename );
}

/*
* Game main loop
* returns true if the game ended normally
* returns false if the escape key was pressed
*/
bool Game::mainLoop()
{
	SDL_Event event;
	Screen* screen = Screen::instance();
	glfuncs* f = glfuncs::instance();
	bool leave = false;
	int ticks = SDL_GetTicks();	// used to limit the frame rate
	for(;;){
		while(!leave)
		{
			// Clear OpenGL screen
			f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			
			//This runs if we are trying to run frames in too rapid succession
			while((SDL_GetTicks() - ticks) < 1000/Screen::FRAMERATE)
			{
				//Just wait until we can do a frame again
				Sleep(0);
			}
			

			if(!_currentLevel->update())
				return true;

			_currentLevel->draw();

			//reset for the next time we want to update
			ticks = SDL_GetTicks();

			// Draw Team Scramble logo
			/*SDL_Rect scramRect;
			scramRect.w = 51;
			scramRect.h = 81;
			scramRect.x = 5;
			scramRect.y = screen->getScreenRect().h - 80;
			screen->drawScreen(_scram,_scramCoords,scramRect);*/

			Screen::drawScramble();

			// Clean up
			f->glFinish();
			SDL_GL_SwapBuffers();
			
			// Check that escape key isn't being pressed
			while(SDL_PollEvent(&event))
			{
				if(event.type == SDL_KEYDOWN){
					if( event.key.keysym.sym == Control::quitKey)
						leave = true;
				}
				//if user has Xed out
				else if( event.type == SDL_QUIT )
				{
					return false;
				}
			}
		}

		f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		//This runs if we are trying to run frames in too rapid succession
		while((SDL_GetTicks() - ticks) < 1000/Screen::FRAMERATE)
		{
			//Just wait until we can do a frame again
			Sleep(0);
		}
		ticks = SDL_GetTicks();

		Screen::drawPause();

		Screen::drawScramble();

		// Clean up
		f->glFinish();
		SDL_GL_SwapBuffers();

		//user has pressed esc.
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_KEYDOWN){
				if( event.key.keysym.sym == Control::quitKey)
					leave = false;
				if( event.key.keysym.sym == Control::selectKey)
					return false;
			}
				//if user has Xed out
			else if( event.type == SDL_QUIT )
			{
				return false;
			}
		}
	}
}