#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdio.h>
#include <vector>
#include <math.h>

#include "renderer.hpp"

#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

#define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg"

void framebuffer_size_callback(GLFWwindow* win, int w, int h) {
	glViewport(0, 0, w, h);
}

void processInput(GLFWwindow *win) {
	int action = glfwGetKey(win, GLFW_KEY_ESCAPE);
	if (action == GLFW_PRESS) {
		glfwSetWindowShouldClose(win, true);
	}
}

void renderCallback() {
	// Make background
	glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

int main() {
	glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow* win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hohmann", NULL, NULL);
	if (win == NULL) {
		printf("Failed to create a window.\n");
		glfwTerminate();
		return 1;
	}

	glfwMakeContextCurrent(win);

	if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
		printf("Failed to init GLAD.\n");
		return 1;
	}

	std::vector<float> verts({
		-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
		 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
		 0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,

		 0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
		-0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
		-0.5f, -0.5f, -0.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0f, 1.0f,
		 0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,

		 0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
		-0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
		-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,


		-0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
		 0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
		 0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,

		 0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
		-0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
		-0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
	});
	// Vert struc: x y z  r g b  tx ty

	std::vector<unsigned int> indices({  
		0, 1, 3, 
		1, 2, 3,
		5, 6, 7, 
		7, 8, 9, 
		9, 10, 11, 
		11, 12, 13,
		0, 1, 3, 
		3, 4, 5, 
		5, 6, 7, 
		7, 8, 9, 
		9, 10, 11, 
		11, 12, 13,
		0, 1, 3, 
		3, 4, 5, 
		5, 6, 7, 
		7, 8, 9, 
		9, 10, 11, 
		11, 12, 13,
		0, 1, 3, 
		3, 4, 5, 
		5, 6, 7, 
		7, 8, 9, 
		9, 10, 11, 
		11, 12, 13,
		0, 1, 3, 
		3, 4, 5, 
		5, 6, 7, 
		7, 8, 9, 
		9, 10, 11, 
		11, 12, 13,
	});  

	glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
	glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
	glEnable(GL_DEPTH_TEST);  

	float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
	glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

	// Create scene
	Renderer::Scene scene(win);

	Renderer::TexturedObject ro(verts, indices);
	ro.setPosition(glm::vec3(0.2f, -1.0f, -8.0f));
	ro.name = "Hej";

	Renderer::TexturedObject ro2(verts, indices);
	ro2.setPosition(glm::vec3(0.5f, 0.0, -8.0f));

	ro.setTexture(RUSTY_METAL_TEXTURE);
	ro2.setTexture("assets/textures/meep.jpg");

	scene.spawnObject(ro);
	// scene.spawnObject(ro2);

	// Window width & height
	while (!glfwWindowShouldClose(win)) {
		// Handle input
		processInput(win);

		// rendering
		renderCallback();

		float time = glfwGetTime();
		float gVal = sin(time);

		// Move the camera left and right
		// scene.camera.setPosition(glm::vec3(0.0f, 0.0f, 8*gVal-4.0f));
		scene.camera.translate(glm::vec3(0.0f, 0.0f, 0.01f));
		scene.camera.setRotation(glm::vec3(0.0f, 5.0f, 0.0f));
		//scene.camera.rotate(glm::vec3(0.0f, -0.4f, 0.0f));

		// Move the objects & stuff 
		float rotang = time;
		// ro.setRotation(glm::vec3(time*10, 0.0f, time));
		ro.translate(glm::vec3(0.0f, 0.0f, -0.015f));
		ro.rotate(glm::vec3(20.0f, 0.0f, 0.0f));

		// Render new frame
		scene.render();

		// glfw
		glfwSwapBuffers(win);
		glfwPollEvents();
	}

	glfwTerminate();
	return 0;
}