#include "GameManager.h"
#include "LogManager.h"
#include "WorldManager.h"
#include "ResourceManager.h"
#include "InputManager.h"
#include "EventStep.h"
#include <Windows.h>
#include <curses.h>

GameManager::~GameManager(){}

/// Get the singleton instance of the GameManager.
GameManager &GameManager::getInstance(){
	static GameManager GameM;
	return GameM;
}

/// Startup all the GameManager services.
/// append = true if add to log file (default false).
/// flush = true if flush after each write (default false).
/// seed is optional random seed (default is seed with system time).
int GameManager::startUp(){
	LogManager();
	WorldManager::getInstance().startUp();
	ResourceManager::getInstance().startUp();
	InputManager::getInstance().startUp();
	LogManager::getInstance().startUp(false,false);
	return 0;
}
int GameManager::startUp(bool append, bool flush){
	LogManager();
	WorldManager::getInstance().startUp();
	ResourceManager::getInstance().startUp();
	InputManager::getInstance().startUp();
	LogManager::getInstance().startUp(append,flush);
	return 0;
}
int GameManager::startUp(bool append, bool flush, time_t seed){
	srand(seed);
	LogManager();
	WorldManager::getInstance().startUp();
	ResourceManager::getInstance().startUp();
	InputManager::getInstance().startUp();
	LogManager::getInstance().startUp(append,flush);
	return 0;
}

/// Shut down the GameManager services, also terminating the program.
void GameManager::shutDown(){
	LogManager::getInstance().shutDown();
	
}

/// Run the game loop.
/// Frame_time is time between frames (default FRAME_TIME, indicated above).
//hard code pressing esc to get out of the game
void GameManager::run(){
	while(!this->game_over){
		if(GetAsyncKeyState(VK_ESCAPE)){
			this->game_over=true;
		}
		Sleep(DEFAULT_FRAME_TIME);
		clear();
		//GameManager::getInstance().onEvent(&EventStep());
		InputManager::getInstance().getInput();
		WorldManager::getInstance().update();
		refresh();
	}
}

void GameManager::run(int frame_time){
	while(!this->game_over){
		if(GetAsyncKeyState(VK_ESCAPE)){
			this->game_over=true;
		}
		Sleep(frame_time);
		clear();
		//GameManager::getInstance().onEvent(&EventStep());
		InputManager::getInstance().getInput();
		WorldManager::getInstance().update();
		refresh();
	}
}

/// Indicate the game is over, which will stop the game loop.
void GameManager::setGameOver(){
	this->game_over=true;
}