#include "director.h"
#include <SDL2/SDL.h>				 // for SDL_Init, SDL_Quit, SDL_INIT_EV...
#include <SDL2/SDL_audio.h>			 // for AUDIO_S16
#include <SDL2/SDL_blendmode.h>		 // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_error.h>			 // for SDL_GetError
#include <SDL2/SDL_gamecontroller.h> // for SDL_CONTROLLER_BUTTON_B, SDL_CO...
#include <SDL2/SDL_hints.h>			 // for SDL_SetHint, SDL_HINT_RENDER_DR...
#include <SDL2/SDL_scancode.h>		 // for SDL_SCANCODE_A, SDL_SCANCODE_ES...
#include <SDL2/SDL_stdinc.h>		 // for Uint32
#include <SDL2/SDL_timer.h>			 // for SDL_GetTicks
#include <errno.h>					 // for errno, EEXIST, EACCES, ENAMETOO...
#include <stdio.h>					 // for printf, perror
#include <string.h>					 // for strcmp
#include <sys/stat.h>				 // for mkdir, stat, S_IRWXU
#include <unistd.h>					 // for getuid
#include <cstdlib>					 // for exit, EXIT_FAILURE, srand
#include <iostream>					 // for basic_ostream, operator<<, cout
#include <memory>					 // for make_unique, unique_ptr
#include <string>					 // for operator+, allocator, char_traits
#include "asset.h"					 // for Asset, AssetType
#include "cheevos.h"				 // for Cheevos
#include "credits.h"				 // for Credits
#include "debug.h"					 // for Debug
#include "defines.h"				 // for WINDOW_CAPTION, borderColor
#include "ending.h"					 // for Ending
#include "ending2.h"				 // for Ending2
#include "game.h"					 // for Game
#include "game_over.h"				 // for GameOver
#include "input.h"					 // for Input, inputs_e
#include "jail_audio.h"				 // for JA_GetMusicState, JA_DeleteMusic
#include "loading_screen.h"			 // for LoadingScreen
#include "logo.h"					 // for Logo
#include "notifier.h"				 // for Notifier
#include "options.h"				 // for Options, options, Section, Cheat
#include "resource.h"				 // for Resource
#include "screen.h"					 // for Screen
#include "title.h"					 // for Title
#include "utils.h"					 // for Color

#ifndef _WIN32
#include <pwd.h>
#endif

// Constructor
Director::Director(int argc, const char *argv[])
{
	std::cout << "Game start" << std::endl;

	// Crea e inicializa las opciones del programa
	initOptions();

	// Comprueba los parametros del programa
	executable_path_ = checkProgramArguments(argc, argv);

	// Crea el objeto que controla los ficheros de recursos
	Asset::init(executable_path_);

	// Crea la carpeta del sistema donde guardar datos
	createSystemFolder("jailgames");
	createSystemFolder("jailgames/jaildoctors_dilemma");

	// Si falta algún fichero no inicia el programa
	if (!setFileList())
	{
		exit(EXIT_FAILURE);
	}

	// Carga las opciones desde un fichero
	loadOptionsFromFile(Asset::get()->get("config.txt"));

	// Inicializa SDL
	initSDL();

	// Inicializa JailAudio
	initJailAudio();

	// Crea los objetos
	Screen::init(window_, renderer_);
	Screen::get()->setBorderColor(border_color);
	Resource::init();
	Notifier::init("", "8bithud");
	Input::init(Asset::get()->get("gamecontrollerdb.txt"));
	initInput();
	Debug::init();
	Cheevos::init(Asset::get()->get("cheevos.bin"));
}

Director::~Director()
{
	// Guarda las opciones a un fichero
	saveOptionsToFile(Asset::get()->get("config.txt"));

	// Destruye los singletones
	Asset::destroy();
	Input::destroy();
	Screen::destroy();
	Notifier::destroy();
	Debug::destroy();
	Resource::destroy();
	Cheevos::destroy();

	SDL_DestroyRenderer(renderer_);
	SDL_DestroyWindow(window_);
	SDL_Quit();

	std::cout << "\nBye!" << std::endl;
}

// Comprueba los parametros del programa
std::string Director::checkProgramArguments(int argc, const char *argv[])
{
	// Iterar sobre los argumentos del programa
	for (int i = 1; i < argc; ++i)
	{
		std::string argument(argv[i]);

		if (argument == "--console")
		{
			options.console = true;
		}
		else if (argument == "--infiniteLives")
		{
			options.cheats.infinite_lives = Cheat::CheatState::ENABLED;
		}
		else if (argument == "--invincible")
		{
			options.cheats.invincible = Cheat::CheatState::ENABLED;
		}
		else if (argument == "--jailEnabled")
		{
			options.cheats.jail_is_open = Cheat::CheatState::ENABLED;
		}
		else if (argument == "--altSkin")
		{
			options.cheats.alternate_skin = Cheat::CheatState::ENABLED;
		}
	}

	return argv[0];
}

// Crea la carpeta del sistema donde guardar datos
void Director::createSystemFolder(const std::string &folder)
{
#ifdef _WIN32
	system_folder_ = std::string(getenv("APPDATA")) + "/" + folder;
#elif __APPLE__
	struct passwd *pw = getpwuid(getuid());
	const char *homedir = pw->pw_dir;
	system_folder_ = std::string(homedir) + "/Library/Application Support" + "/" + folder;
#elif __linux__
	struct passwd *pw = getpwuid(getuid());
	const char *homedir = pw->pw_dir;
	system_folder_ = std::string(homedir) + "/.config/" + folder;

	{
		// Intenta crear ".config", per si no existeix
		std::string config_base_folder = std::string(homedir) + "/.config";
		int ret = mkdir(config_base_folder.c_str(), S_IRWXU);
		if (ret == -1 && errno != EEXIST)
		{
			printf("ERROR CREATING CONFIG BASE FOLDER.");
			exit(EXIT_FAILURE);
		}
	}
#endif

	struct stat st = {0};
	if (stat(system_folder_.c_str(), &st) == -1)
	{
		errno = 0;
#ifdef _WIN32
		int ret = mkdir(system_folder_.c_str());
#else
		int ret = mkdir(system_folder_.c_str(), S_IRWXU);
#endif

		if (ret == -1)
		{
			switch (errno)
			{
			case EACCES:
				printf("the parent directory does not allow write");
				exit(EXIT_FAILURE);

			case EEXIST:
				printf("pathname already exists");
				exit(EXIT_FAILURE);

			case ENAMETOOLONG:
				printf("pathname is too long");
				exit(EXIT_FAILURE);

			default:
				perror("mkdir");
				exit(EXIT_FAILURE);
			}
		}
	}
}

// Inicia las variables necesarias para arrancar el programa
void Director::initInput()
{
	// Establece si ha de mostrar mensajes
	Input::get()->setVerbose(options.console);

	// Busca si hay un mando conectado
	Input::get()->discoverGameController();

	// Teclado - Movimiento
	if (options.keys == ControlScheme::CURSOR)
	{
		Input::get()->bindKey(InputAction::JUMP, SDL_SCANCODE_UP);
		Input::get()->bindKey(InputAction::LEFT, SDL_SCANCODE_LEFT);
		Input::get()->bindKey(InputAction::RIGHT, SDL_SCANCODE_RIGHT);
		Input::get()->bindKey(InputAction::UP, SDL_SCANCODE_UP);
		Input::get()->bindKey(InputAction::DOWN, SDL_SCANCODE_DOWN);
	}
	else if (options.keys == ControlScheme::OPQA)
	{
		Input::get()->bindKey(InputAction::JUMP, SDL_SCANCODE_Q);
		Input::get()->bindKey(InputAction::LEFT, SDL_SCANCODE_O);
		Input::get()->bindKey(InputAction::RIGHT, SDL_SCANCODE_P);
		Input::get()->bindKey(InputAction::UP, SDL_SCANCODE_Q);
		Input::get()->bindKey(InputAction::DOWN, SDL_SCANCODE_A);
	}
	else if (options.keys == ControlScheme::WASD)
	{
		Input::get()->bindKey(InputAction::JUMP, SDL_SCANCODE_W);
		Input::get()->bindKey(InputAction::LEFT, SDL_SCANCODE_A);
		Input::get()->bindKey(InputAction::RIGHT, SDL_SCANCODE_D);
		Input::get()->bindKey(InputAction::UP, SDL_SCANCODE_W);
		Input::get()->bindKey(InputAction::DOWN, SDL_SCANCODE_S);
	}

	// Teclado - Otros
	Input::get()->bindKey(InputAction::ACCEPT, SDL_SCANCODE_RETURN);
	Input::get()->bindKey(InputAction::CANCEL, SDL_SCANCODE_ESCAPE);
	Input::get()->bindKey(InputAction::PAUSE, SDL_SCANCODE_H);
	Input::get()->bindKey(InputAction::EXIT, SDL_SCANCODE_ESCAPE);
	Input::get()->bindKey(InputAction::WINDOW_DEC_ZOOM, SDL_SCANCODE_F1);
	Input::get()->bindKey(InputAction::WINDOW_INC_ZOOM, SDL_SCANCODE_F2);
	Input::get()->bindKey(InputAction::TOGGLE_VIDEOMODE, SDL_SCANCODE_F3);
	Input::get()->bindKey(InputAction::TOGGLE_SHADERS, SDL_SCANCODE_F4);
	Input::get()->bindKey(InputAction::TOGGLE_PALETTE, SDL_SCANCODE_F5);
	Input::get()->bindKey(InputAction::TOGGLE_MUSIC, SDL_SCANCODE_M);
	Input::get()->bindKey(InputAction::TOGGLE_BORDER, SDL_SCANCODE_B);

	// Mando - Movimiento
	Input::get()->bindGameControllerButton(InputAction::JUMP, SDL_CONTROLLER_BUTTON_B);
	Input::get()->bindGameControllerButton(InputAction::LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
	Input::get()->bindGameControllerButton(InputAction::RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);

	// Mando - Otros
	Input::get()->bindGameControllerButton(InputAction::ACCEPT, SDL_CONTROLLER_BUTTON_B);
	Input::get()->bindGameControllerButton(InputAction::CANCEL, SDL_CONTROLLER_BUTTON_A);
#ifdef GAME_CONSOLE
	Input::get()->bindGameControllerButton(InputAction::input_pause, SDL_CONTROLLER_BUTTON_BACK);
	Input::get()->bindGameControllerButton(InputAction::input_exit, SDL_CONTROLLER_BUTTON_START);
#else
	Input::get()->bindGameControllerButton(InputAction::PAUSE, SDL_CONTROLLER_BUTTON_START);
	Input::get()->bindGameControllerButton(InputAction::EXIT, SDL_CONTROLLER_BUTTON_BACK);
#endif
	Input::get()->bindGameControllerButton(InputAction::TOGGLE_PALETTE, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
	Input::get()->bindGameControllerButton(InputAction::TOGGLE_MUSIC, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
	Input::get()->bindGameControllerButton(InputAction::TOGGLE_BORDER, SDL_CONTROLLER_BUTTON_X);
}

// Inicializa JailAudio
void Director::initJailAudio()
{
	JA_Init(48000, AUDIO_S16, 2);
	if (options.audio.enabled)
	{
		JA_SetMusicVolume(options.audio.music.volume);
		JA_SetSoundVolume(options.audio.sound.volume);
	}
	else
	{
		JA_SetMusicVolume(0);
		JA_SetSoundVolume(0);
	}
}

// Arranca SDL y crea la ventana
bool Director::initSDL()
{
	// Indicador de éxito
	bool success = true;

	// Inicializa SDL
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
	{
		if (options.console)
		{
			std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
		}
		success = false;
	}
	else
	{
		// Inicia el generador de numeros aleatorios
		std::srand(static_cast<unsigned int>(SDL_GetTicks()));

		// Establece el filtro de la textura a nearest
		if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(static_cast<int>(options.video.filter)).c_str()))
		{
			if (options.console)
			{
				std::cout << "Warning: Nearest texture filtering not enabled!\n";
			}
		}

		// Activa el render OpenGL
		if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"))
		{
			std::cout << "Warning: opengl not enabled!\n";
		}

		// Crea la ventana
		const auto window_width = options.video.border.enabled ? options.game.width + options.video.border.width * 2 : options.game.width;
		const auto window_height = options.video.border.enabled ? options.game.height + options.video.border.height * 2 : options.game.height;

		window_ = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width * options.window.zoom, window_height * options.window.zoom, SDL_WINDOW_HIDDEN);
		if (window_ == nullptr)
		{
			if (options.console)
			{
				std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
			}
			success = false;
		}
		else
		{
			// Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones
			Uint32 flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
			if (options.video.vertical_sync)
			{
				flags = flags | SDL_RENDERER_PRESENTVSYNC;
			}
			renderer_ = SDL_CreateRenderer(window_, -1, flags);

			if (renderer_ == nullptr)
			{
				if (options.console)
				{
					std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
				}
				success = false;
			}
			else
			{
				// Inicializa el color de renderizado
				SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);

				// Modifica el tamaño del renderizador
				const int extra_width = options.video.border.enabled ? options.video.border.width * 2 : 0;
				const int extra_height = options.video.border.enabled ? options.video.border.height * 2 : 0;
				SDL_RenderSetLogicalSize(renderer_, options.game.width + extra_width, options.game.height + extra_height);

				// Establece el modo de mezcla
				SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
			}
		}
	}

	if (options.console)
	{
		std::cout << std::endl;
	}
	return success;
}

// Crea el indice de ficheros
bool Director::setFileList()
{
#ifdef MACOS_BUNDLE
	const std::string prefix = "/../Resources";
#else
	const std::string prefix = "";
#endif

	// Texto
	Asset::get()->add(prefix + "/data/font/smb2.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/font/smb2.txt", AssetType::FONT);
	Asset::get()->add(prefix + "/data/font/debug.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/font/debug.txt", AssetType::FONT);
	Asset::get()->add(prefix + "/data/font/gauntlet.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/font/gauntlet.txt", AssetType::FONT);
	Asset::get()->add(prefix + "/data/font/subatomic.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/font/subatomic.txt", AssetType::FONT);
	Asset::get()->add(prefix + "/data/font/8bithud.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/font/8bithud.txt", AssetType::FONT);

	// Paletas
	Asset::get()->add(prefix + "/data/palette/zx-spectrum.gif", AssetType::PALETTE);
	Asset::get()->add(prefix + "/data/palette/zx-spectrum-adjusted.gif", AssetType::PALETTE);
	Asset::get()->add(prefix + "/data/palette/ruzx-spectrum.gif", AssetType::PALETTE);
	Asset::get()->add(prefix + "/data/palette/ruzx-spectrum-revision-2.gif", AssetType::PALETTE);
	Asset::get()->add(prefix + "/data/palette/zxarne-5-2.gif", AssetType::PALETTE);

	// Shaders
	Asset::get()->add(prefix + "/data/shaders/crtpi_192.glsl", AssetType::DATA);
	Asset::get()->add(prefix + "/data/shaders/crtpi_240.glsl", AssetType::DATA);

	// Datos
	Asset::get()->add(prefix + "/data/input/gamecontrollerdb.txt", AssetType::DATA);

	// Ficheros de sistema
	Asset::get()->add(system_folder_ + "/config.txt", AssetType::DATA, false, true);
	Asset::get()->add(system_folder_ + "/stats_buffer.csv", AssetType::DATA, false, true);
	Asset::get()->add(system_folder_ + "/stats.csv", AssetType::DATA, false, true);
	Asset::get()->add(system_folder_ + "/cheevos.bin", AssetType::DATA, false, true);

	// Habitaciones
	Asset::get()->add(prefix + "/data/room/01.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/02.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/03.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/04.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/05.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/06.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/07.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/08.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/09.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/10.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/11.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/12.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/13.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/14.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/15.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/16.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/17.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/18.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/19.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/20.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/21.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/22.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/23.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/24.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/25.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/26.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/27.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/28.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/29.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/30.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/31.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/32.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/33.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/34.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/35.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/36.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/37.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/38.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/39.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/40.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/41.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/42.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/43.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/44.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/45.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/46.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/47.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/48.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/49.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/50.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/51.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/52.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/53.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/54.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/55.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/56.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/57.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/58.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/59.room", AssetType::ROOM);
	Asset::get()->add(prefix + "/data/room/60.room", AssetType::ROOM);

	// Tilemaps
	Asset::get()->add(prefix + "/data/room/01.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/02.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/03.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/04.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/05.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/06.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/07.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/08.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/09.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/10.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/11.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/12.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/13.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/14.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/15.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/16.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/17.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/18.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/19.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/20.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/21.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/22.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/23.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/24.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/25.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/26.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/27.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/28.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/29.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/30.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/31.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/32.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/33.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/34.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/35.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/36.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/37.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/38.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/39.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/40.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/41.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/42.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/43.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/44.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/45.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/46.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/47.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/48.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/49.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/50.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/51.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/52.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/53.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/54.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/55.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/56.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/57.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/58.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/59.tmx", AssetType::TILEMAP);
	Asset::get()->add(prefix + "/data/room/60.tmx", AssetType::TILEMAP);

	// Tilesets
	Asset::get()->add(prefix + "/data/tilesets/standard.gif", AssetType::BITMAP);

	// Enemigos
	Asset::get()->add(prefix + "/data/enemies/abad_bell.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/abad_bell.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/abad.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/abad.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/amstrad_cs.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/amstrad_cs.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/flying_arounder.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/flying_arounder.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/stopped_arounder.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/stopped_arounder.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/walking_arounder.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/walking_arounder.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/arounders_door.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/arounders_door.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/arounders_machine.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/arounders_machine.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/bat.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/bat.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/batman_bell.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/batman_bell.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/batman_fire.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/batman_fire.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/batman.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/batman.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/bell.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/bell.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/bin.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/bin.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/bird.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/bird.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/breakout.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/breakout.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/bry.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/bry.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/chip.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/chip.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/code.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/code.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/congo.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/congo.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/crosshair.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/crosshair.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/demon.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/demon.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/dimallas.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/dimallas.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/floppy.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/floppy.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/dong.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/dong.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/guitar.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/guitar.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/heavy.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/heavy.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/jailer_#1.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/jailer_#1.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/jailer_#2.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/jailer_#2.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/jailer_#3.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/jailer_#3.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/jailbattle_alien.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/jailbattle_alien.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/jailbattle_human.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/jailbattle_human.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/jeannine.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/jeannine.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/lamp.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/lamp.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/lord_abad.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/lord_abad.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/matatunos.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/matatunos.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/mummy.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/mummy.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/paco.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/paco.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/elsa.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/elsa.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/qvoid.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/qvoid.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/robot.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/robot.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/sam.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/sam.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/shock.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/shock.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/sigmasua.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/sigmasua.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/spark.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/spark.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/special/aerojailer.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/special/aerojailer.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/special/arounder.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/special/arounder.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/special/pepe_rosita_job.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/special/pepe_rosita_job.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/special/shooting_star.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/special/shooting_star.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/spider.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/spider.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/tree_thing.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/tree_thing.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/tuno.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/tuno.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/tv_panel.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/tv_panel.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/tv.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/tv.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/upv_student.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/upv_student.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/wave.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/wave.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/enemies/z80.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/enemies/z80.gif", AssetType::BITMAP);

	// Jugador
	Asset::get()->add(prefix + "/data/player/player.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/player/player.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/player/player2.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/player/player2.ani", AssetType::ANIMATION);
	Asset::get()->add(prefix + "/data/player/player_game_over.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/player/player_game_over.ani", AssetType::ANIMATION);

	// Items
	Asset::get()->add(prefix + "/data/items/items.gif", AssetType::BITMAP);

	// Musicas
	Asset::get()->add(prefix + "/data/music/title.ogg", AssetType::MUSIC);
	Asset::get()->add(prefix + "/data/music/game.ogg", AssetType::MUSIC);
	Asset::get()->add(prefix + "/data/music/loading_sound1.ogg", AssetType::MUSIC);
	Asset::get()->add(prefix + "/data/music/loading_sound2.ogg", AssetType::MUSIC);
	Asset::get()->add(prefix + "/data/music/loading_sound3.ogg", AssetType::MUSIC);
	Asset::get()->add(prefix + "/data/music/ending1.ogg", AssetType::MUSIC);
	Asset::get()->add(prefix + "/data/music/ending2.ogg", AssetType::MUSIC);
	Asset::get()->add(prefix + "/data/music/game_over.ogg", AssetType::MUSIC);

	// Efectos de sonido
	Asset::get()->add(prefix + "/data/sound/item.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/death.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump1.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump2.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump3.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump4.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump5.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump6.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump7.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump8.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump9.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump10.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump11.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump12.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump13.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump14.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump15.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump16.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump17.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump18.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump19.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump20.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump21.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump22.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump23.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/jump24.wav", AssetType::SOUND);
	Asset::get()->add(prefix + "/data/sound/notify.wav", AssetType::SOUND);

	// Logo
	Asset::get()->add(prefix + "/data/logo/jailgames.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/logo/since_1998.gif", AssetType::BITMAP);

	// Loading
	Asset::get()->add(prefix + "/data/loading/loading_screen_bn.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/loading/loading_screen_color.gif", AssetType::BITMAP);

	// Title
	Asset::get()->add(prefix + "/data/title/title_loading.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/title/title_logo.gif", AssetType::BITMAP);

	// Ending
	Asset::get()->add(prefix + "/data/ending/ending1.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/ending/ending2.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/ending/ending3.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/ending/ending4.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/ending/ending5.gif", AssetType::BITMAP);

	// Credits
	Asset::get()->add(prefix + "/data/credits/shine.gif", AssetType::BITMAP);
	Asset::get()->add(prefix + "/data/credits/shine.ani", AssetType::ANIMATION);

	return Asset::get()->check();
}

// Ejecuta la seccion de juego con el logo
void Director::runLogo()
{
	auto logo = std::make_unique<Logo>();
	logo->run();
}

// Ejecuta la seccion de juego de la pantalla de carga
void Director::runLoadingScreen()
{
	auto loadingScreen = std::make_unique<LoadingScreen>();
	loadingScreen->run();
}

// Ejecuta la seccion de juego con el titulo y los menus
void Director::runTitle()
{
	auto title = std::make_unique<Title>();
	title->run();
}

// Ejecuta la seccion de los creditos del juego
void Director::runCredits()
{
	auto credits = std::make_unique<Credits>();
	credits->run();
}

// Ejecuta la seccion de la demo, donde se ven pantallas del juego
void Director::runDemo()
{
	auto game = std::make_unique<Game>(GameMode::DEMO);
	game->run();
}

// Ejecuta la seccion del final del juego
void Director::runEnding()
{
	auto ending = std::make_unique<Ending>();
	ending->run();
}

// Ejecuta la seccion del final del juego
void Director::runEnding2()
{
	auto ending2 = std::make_unique<Ending2>();
	ending2->run();
}

// Ejecuta la seccion del final de la partida
void Director::runGameOver()
{
	auto gameOver = std::make_unique<GameOver>();
	gameOver->run();
}

// Ejecuta la seccion de juego donde se juega
void Director::runGame()
{
	JA_StopMusic();
	auto game = std::make_unique<Game>(GameMode::GAME);
	game->run();
}

int Director::run()
{
	// Bucle principal
	while (options.section.section != Section::QUIT)
	{
		switch (options.section.section)
		{
		case Section::LOGO:
			runLogo();
			break;

		case Section::LOADING_SCREEN:
			runLoadingScreen();
			break;

		case Section::TITLE:
			runTitle();
			break;

		case Section::CREDITS:
			runCredits();
			break;

		case Section::DEMO:
			runDemo();
			break;

		case Section::GAME:
			runGame();
			break;

		case Section::GAME_OVER:
			runGameOver();
			break;

		case Section::ENDING:
			runEnding();
			break;

		case Section::ENDING2:
			runEnding2();
			break;

		default:
			break;
		}
	}

	return 0;
}