#include "gameplay/game.h"
#include "gameplay/input.h"
#include "gameplay/scene.h"
/*due for possible removal!*/
#include "gameplay/gameactor.h"
#include "gameplay/weapons/weapon.h"
/*-------------------------*/

#include "utility/command.h"
#include "utility/resourcemanager.h"
#include "utility/ftfont.h"
#include "utility/logger.h"

#include "graphics/glwindow.h"

#include <iostream>
#include <glm/gtc/matrix_transform.hpp>
#include <memory>
#include <utility/events.h>

bool Game::init()
{
	window = std::make_shared<GLWindow>("Yupple Mayham", 800, 600);

	if (!window->Init())
		ERROR_LOG("Failed to init GLWindow: {}", SDL_GetError());

	if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
		ERROR_LOG("Failed to load GLLoader");

#if _DEBUG
	LOG_LEVEL(DEBUG);
#else
	LOG_LEVEL(INFO);
#endif

	SDL_GL_SetSwapInterval(1);
	glViewport(0, 0, window->width(), window->height());
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// For now we'll init default bindings, but as we move forward controls will be set by the player and saved in controls.xml
	inputHandler = std::make_shared<InputHandler>();
	inputHandler->bindKeyCommand(SDLK_w, 0, new MoveUpCommand());
	inputHandler->bindKeyCommand(SDLK_a, 0, new MoveLeftCommand());
	inputHandler->bindKeyCommand(SDLK_s, 0, new MoveDownCommand());
	inputHandler->bindKeyCommand(SDLK_d, 0, new MoveRightCommand());

	inputHandler->bindMouseCommand(MOUSE_BUTTON_LEFT, 100, new ShootCommand());
	inputHandler->bindMouseMotion(new FollowMouseCommand());
	inputHandler->bindMouseScroll(new CycleCommand());

	game_state |= GAME_RUNNING;
	globalEventManager = std::make_shared<EventManager>();
	resourceManager = std::make_shared<ResourceManager>();
	renderer = std::make_shared<Renderer>(resourceManager);
	renderer->hookEventManager(globalEventManager);
	textHandler = std::make_shared<Text>();
	if (!textHandler->loadFonts("fonts"))
		return false;
	textHandler->setProjectionMatrix(glm::ortho(0.f, static_cast<float>(window->width()), 0.f, static_cast<float>(window->height())));
	return true;
}

const unsigned Game::getWindowWidth()	const { return window->width(); }
const unsigned Game::getWindowHeight()	const { return window->height(); }

bool Game::loadDebugScene()
{
	currentScene = std::make_shared<Scene>(SCENE_SHOOTER, resourceManager, globalEventManager);
	currentScene->init();
	if (currentScene->getPlayer() == nullptr)
		return false;
	inputHandler->setActor(currentScene->getPlayer().get());
	return true;
}

void Game::handleInput(SDL_Event& e)
{
	inputHandler->captureInput(e);
	inputHandler->handleInput();
}

void Game::update(double deltaTime)
{
	if (currentScene)
		currentScene->update(deltaTime);
}

void Game::render()
{
	glClearColor(0.05f, 0.25f, 0.05f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	if (currentScene)
	{
		currentScene->render(renderer);
		/*Debug ammo indicator*/
		textHandler->DrawText("comic.ttf", std::to_string(currentScene->getPlayer()->getHeldWeapon()->getMagazine()), glm::vec2(10, 10), 0.5f);
		textHandler->DrawText("comic.ttf", "/", glm::vec2(50, 10), 0.5f);
		textHandler->DrawText("comic.ttf", std::to_string(currentScene->getPlayer()->getHeldWeapon()->getAmmo()), glm::vec2(90, 10), 0.5f);
	}

	window->swap();
}
