#include "ggjgame.h"
#include "gameframe.h"
#include "title_frame.h"
#include "intro_frame.h"
#include "story_frame.h"
#include <ctime>

GGJGame::GGJGame()
	: Game("Planetary Plan C")
{
	srand((unsigned)time(NULL));
}

void GGJGame::addResourcePack(String name)
{
	try {
		ResourceLocator::add(shared_ptr<ResourceLocator>(new ResourcePack(GetProgramPath()+name)));
	} catch (...) {}
}

void GGJGame::Init(StringArray parameters)
{
	// Setup resource locators
	ResourceLocator::add(shared_ptr<ResourceLocator>(new FileSystemResourceLocator(GetProgramPath())));
	addResourcePack("game.pack");
	
	// Setup resource managers
	TextureManager::setManager(shared_ptr<TextureManager>(new TextureManager("data/images/")));
	FontManager::setManager(shared_ptr<FontManager>(new FontManager("data/fonts/")));
	SampleManager::setManager(shared_ptr<SampleManager>(new SampleManager("data/samples/")));
	MusicManager::setManager(shared_ptr<MusicManager>(new MusicManager("data/music/")));
	AnimationManager::setManager(shared_ptr<AnimationManager>(new AnimationManager("data/anims/")));

	// Parse parameters
	bool fs = false;
	for (size_t i=0; i<parameters.size(); i++) {
		if (parameters[i] == "-fs") fs = true;
	}

	// Setup screen
	const int w = 1024;
	const int h = 640;
	Vector2i screen = Video::GetScreenSize();
	Vector2i winSize;
	if (fs) {
		winSize = screen;
		//virtualSize = Vector2i(screen.x, screen.x * h / w);
	} else {
		if (screen.x > w && screen.y > h) winSize = Vector2i(w, h);
		else winSize = Vector2i(w * 4 / 5, h * 4 / 5);
	}
	Video::SetVideo(fs, winSize, Vector2f(w, h));

	// Start game
	SetTopFrame(CreateFrame("intro"));
}

void GGJGame::DeInit()
{
	std::cout << "Game shutting down." << std::endl;

	TextureManager::setManager(shared_ptr<TextureManager>());
	FontManager::setManager(shared_ptr<FontManager>());

	std::cout << "Goodbye!" << std::endl;
}

spFrame GGJGame::CreateFrame(String name) {
	if (name == "intro") return spFrame(new IntroFrame());
	if (name == "game") return spFrame(new GameFrame());
	if (name == "title") return spFrame(new TitleFrame());
	if (name == "story") return spFrame(new StoryFrame());
	return spFrame();
}
