#include <SDL.h>
#include <SDL_image.h>

#include <iostream>
#include <tracy/Tracy.hpp>

// TODO: Fix circular dependency issues, mostly with input.h needing gameactor.h and command.h
#include "gameplay/game.h"

const float vertices[] = {
	0.0f, 0.5f, 0.0f,
	-0.5f, -0.5f, 0.0f,
	0.5f, -0.5f, 0.0f
};

int main(int argc, char* args[])
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
		return -1;
	if (IMG_Init(IMG_INIT_PNG) < 0)
		return -1;

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

	Game* game = new Game();
	if (!game->init())
	{
		std::cout << "Failed to init game!" << std::endl;
		return -1;
	}

	if (!game->loadDebugScene())
	{
		std::cout << "Failed to load debug scene!" << std::endl;
		return -1;
	}

	SDL_Event e;
	Uint64 lastCounter = SDL_GetPerformanceCounter();
	double freq = static_cast<double>(SDL_GetPerformanceFrequency());
	while (game->getGameState() & GAME_RUNNING)
	{
		Uint64 curCounter = SDL_GetPerformanceCounter();
		double deltaTime = ((curCounter - lastCounter) / freq);
		deltaTime = (deltaTime < 1.0) ? deltaTime : 1.0;
		SDL_PollEvent(&e);
		if (e.type == SDL_QUIT)
			game->quit();

		game->handleInput(e);

		game->update(deltaTime);

		game->render();
		lastCounter = curCounter;

	}

	IMG_Quit();
	SDL_Quit();

	return 0;
}