#include "game.h"
#include "input.h"
#include "renderer.h"
#include "text.h"
#include "scene.h"
#include "menu.h"
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_scancode.h>
#include <SDL2/SDL_surface.h>
#include <time.h>
#include <unistd.h>
#include "cinematic.h"
#include "scaller.h"

clock_t timecode;
long iter;


static void space(void) {
	printf("space!\n");
	if(renderer_getScene() == &menuScene){
		menu_remove();
		cinematic_staticRender();
		renderer_setScene(&cinematicScene);
	} else if(renderer_getScene() == &cinematicScene) {
		cinematic_remove();
		game_staticRender();
		renderer_setScene(&gameScene);
	}
	else {
		game_handleSpaceKeyInput();
	}
}

Scene_t demo;

static void game_setup(void) {
	menu_staticRender();
	RenderObject_t text = EMPTY_RENDER_OBJECT;
	SDL_Color white = {255, 255, 255, 255};
	TTF_Init();
	if(text_renderText(&text, 1000, 1, white, "./assets/fonts/arial.ttf", 25, "Hello World") < 0){
		printf("error");
		exit(1);
	}
	input_listenKeyDown(SDL_SCANCODE_SPACE, space);
}

static void game_loop(void) {
	float delta = ((float)(clock() - timecode)) / CLOCKS_PER_SEC;

	//update time
	if(renderer_getScene() == &menuScene){
		menu_render(delta);
	}
	else if(renderer_getScene() == &cinematicScene){
		cinematic_render(delta);
	} else {
		game_render(delta);
	}
	timecode = clock();
}


int main(int argc, char ** argv) {
	TTF_Init();
	if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) != 0){
		printf("Music sound could not be open!\n"
			"SDL_Error: %s\n", Mix_GetError());
		running = false;
	}

	iter = 0;
	timecode = clock();

	Mix_Init(MIX_INIT_MP3);
	renderer_main(argc, argv, game_setup, game_loop);

	Mix_CloseAudio();
	text_freeFontCache();
}
