#include "main.h"

#ifdef __APPLE__
#include "pthread_barrier.h"
#endif

#include "axis.h"
#include "camera.h"
#include "grid.h"
#include "io.h"
#include "particle.h"

// ImGui includes
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"

#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
#include <time.h>

const char FRAGMENT_SHADER_SOURCE[] = "./source/shader/fragment.glsl";
const char VERTEX_SHADER_SOURCE[] = "./source/shader/vertex.glsl";

const char PROGRAM_NAME[] = "Particle Sim";
const unsigned int THREAD_COUNT = 8;

const unsigned int SCR_WIDTH = 2560;
const unsigned int SCR_HEIGHT = 1440;
const char *glsl_version;

const unsigned int PARTICLE_COUNT = 8;

GLFWwindow *window;
unsigned int VBO, VAO;
unsigned int shader;
char errLog[128];

Camera *g_camera = nullptr;
AxisRenderer g_axis;
GridRenderer g_grid;

pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_barrier_t barrier;

// INFO: Debug views toggles
bool viewThreads = 0;

static void cursor_callback(GLFWwindow *w, double xpos, double ypos) {
#ifndef NDEBUG
	// Let ImGui eat mouse when cursor is free
	if (!g_camera->cursor_captured)
		return;
#endif
	if (g_camera)
		camera_mouse(*g_camera, xpos, ypos);
}

static void scroll_callback(GLFWwindow *w, double xoffset, double yoffset) {
	if (g_camera && g_camera->cursor_captured)
		camera_scroll(*g_camera, yoffset);
}

static void mouse_button_callback(GLFWwindow *w, int button, int action, int mods) {
	if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
		if (g_camera && !g_camera->cursor_captured) {
			g_camera->cursor_captured = true;
			g_camera->first_mouse = true;
			glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
		}
	}
}

__attribute__((constructor)) static void init_glfw(void) {
	srand(time(NULL));

	if (!glfwInit()) {
		fprintf(stderr, "GLFW initialization failed\n");
		abort(); // program cannot continue
	}
	glsl_version = "#version 330";
#ifdef __APPLE__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#endif
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, PROGRAM_NAME, NULL, NULL);
	if (window == NULL) {
		glfwTerminate();
		fprintf(stderr, "Failed to create GLFW window\n");
		abort();
	}

	glfwMakeContextCurrent(window);
	glfwSwapInterval(1); // Enable vsync

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		std::cout << "Failed to initialize GLAD" << std::endl;
		glfwTerminate();
		abort();
	}
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glEnable(GL_PROGRAM_POINT_SIZE);
	glEnable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);

	glEnableVertexAttribArray(0);

	// Position: 3 floats, starts at 0
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)0);
	glEnableVertexAttribArray(0);

	// Color: 4 floats, starts after 6 floats (Position + Velocity)
	glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(6 * sizeof(float)));
	glEnableVertexAttribArray(1);

	// Life: 1 float, starts after 10 floats (Position + Velocity + Color)
	glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(10 * sizeof(float)));
	glEnableVertexAttribArray(2);

	// Dampening: 1 float, starts after 11 floars (Positon + Velocity + Color + Life)
	glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(11 * sizeof(float)));
	glEnableVertexAttribArray(3);

	// Size: 1 float, starts after 11 floars (Positon + Velocity + Color + Life + Dampening)
	glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(12 * sizeof(float)));
	glEnableVertexAttribArray(4);

	glBufferData(GL_ARRAY_BUFFER, sizeof(Particle) * PARTICLE_COUNT, NULL, GL_DYNAMIC_DRAW);

	char *vertex_shader_source = read_file(VERTEX_SHADER_SOURCE);
	char *fragment_shader_source = read_file(FRAGMENT_SHADER_SOURCE);

	if (!vertex_shader_source || !fragment_shader_source) {
		fprintf(stderr, "Failed to read shader files. CWD is probably wrong.\n");
		abort();
	}

	int success;

	unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
	glCompileShader(vertex_shader);

	glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(vertex_shader, 512, NULL, errLog);
		fprintf(stderr, "VERTEX ERROR: %s\n", errLog);
	}

	unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
	glCompileShader(fragment_shader);

	glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(fragment_shader, 512, NULL, errLog);
		fprintf(stderr, "FRAGMENT ERROR: %s\n", errLog);
	}

	shader = glCreateProgram();
	glAttachShader(shader, vertex_shader);
	glAttachShader(shader, fragment_shader);
	glLinkProgram(shader);
	glGetProgramiv(shader, GL_LINK_STATUS, &success);
	if (!success) {
		glGetProgramInfoLog(shader, 512, NULL, errLog);
		fprintf(stderr, "LINK: %s\n", errLog);
	}

	glDeleteShader(vertex_shader);
	glDeleteShader(fragment_shader);

	free(vertex_shader_source);
	free(fragment_shader_source);
}

__attribute__((destructor)) static void shutdown_glfw(void) {
	axis_destroy(g_axis);
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteProgram(shader);
	glfwDestroyWindow(window);
	glfwTerminate();
}

static inline void init_imgui() {
	IMGUI_CHECKVERSION();
	ImGui::CreateContext();
	ImGui::StyleColorsDark();
	ImGui_ImplGlfw_InitForOpenGL(window, true);
	ImGui_ImplOpenGL3_Init(glsl_version);
}

int main() {
#ifndef NDEBUG
	init_imgui();
#endif

	pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * THREAD_COUNT);
	Physics_params *thread_params = (Physics_params *)malloc(sizeof(Physics_params) * THREAD_COUNT);

	Camera camera = camera_init(glm::vec3(2.0f, 2.0f, 5.0f), SCR_WIDTH, SCR_HEIGHT);
	g_camera = &camera;

	// Axis renderer
	g_axis = axis_init();
	g_grid = grid_init();

	// Register callbacks AFTER ImGui init so ImGui gets them first via chain
	// glfwSetCursorPosCallback(window, cursor_callback);
	// glfwSetScrollCallback(window, scroll_callback);
	// glfwSetMouseButtonCallback(window, mouse_button_callback);

	Particle *particles = (Particle *)malloc(sizeof(Particle) * PARTICLE_COUNT);
	Color *default_particle_color = (Color *)malloc(sizeof(Color) * PARTICLE_COUNT);

	if (particles == NULL) {
		snprintf(errLog, 128, "There was a issue allocating memory for the particle list\n");
	} else {
		for (unsigned long long i = 0; i < PARTICLE_COUNT; i++) {
			// TEST: 3 Particles
			// INFO: Best run with 3 particles and 1 thread
			/*
			particles[0] = particle_init({1.000001f, 1.0f, 0.994f}, {1.0f, 0.0f, 0.0f, 255.0f});
			particles[1] = particle_init({1.0f, 1.2f, 1.0f}, {0.0f, 1.0f, 0.0f, 255.0f});
			particles[2] = particle_init({0.99995f, 1.4f, 1.004f}, {0.0f, 0.0f, 1.0f, 255.0f});
			*/

			// TEST: Cloud of particles in a 2x2x2 cube
			// INFO: Best run with 500 particles and 16 threads
			float x = (float)(rand() % 200) / 100.0f; // Random x between 0 and 2
			float y = (float)(rand() % 200) / 100.0f; // Random y between 0 and 2
			float z = (float)(rand() % 200) / 100.0f; // Random z between 0 and 2

			default_particle_color[i] = {x, y, z, 255.0f};
			particles[i] = particle_init({x, y, z}, {x, y, z, 255.0f});
            particles[i].radius = (float)(rand() % 50) / 1000.0f;
		}

		/*
		particles[0] = particle_init({1.000001f, 1.0f, 0.994f}, {1.0f, 0.0f, 0.0f, 255.0f});
		particles[1] = particle_init({1.0f, 1.2f, 1.0f}, {0.0f, 1.0f, 0.0f, 255.0f});
		particles[2] = particle_init({0.99995f, 1.4f, 1.004f}, {0.0f, 0.0f, 1.0f, 255.0f});
		*/
	}

	float current_time;
	float last_time = static_cast<float>(glfwGetTime());
	float delta_time;
	int alive_count = 0;

	pthread_mutex_init(&mutex, NULL);
	pthread_cond_init(&cond, NULL);
	pthread_barrier_init(&barrier, NULL, THREAD_COUNT + 1);

	for (int i = 0; i < THREAD_COUNT; i++) {
		thread_params[i].particle = particles;
		thread_params[i].interval_start = (PARTICLE_COUNT / THREAD_COUNT) * i;
		thread_params[i].interval_end = (PARTICLE_COUNT / THREAD_COUNT) * (i + 1);

		thread_params[i].time = 0;
		thread_params[i].exit = 0;
		thread_params[i].run = 0;
		thread_params[i].alive_count = 0;

		thread_params[i].thread_id = i;
		thread_params[i].mutex = &mutex;
		thread_params[i].cond = &cond;
		thread_params[i].barrier = &barrier;
		pthread_create(&threads[i], NULL, t_update, &thread_params[i]);
	}

	while (!glfwWindowShouldClose(window)) {
		current_time = static_cast<float>(glfwGetTime());
		delta_time = current_time - last_time;
		last_time = current_time;

		// Poll events
		glfwPollEvents();

		// ESC releases cursor; if cursor already free, close window
		if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
			if (camera.cursor_captured) {
				camera.cursor_captured = false;
				camera.first_mouse = true;
				glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
			} else {
				glfwSetWindowShouldClose(window, true);
			}
		}

		camera_keyboard(camera, window, delta_time);

		// Build MVP
		float aspect = (float)SCR_WIDTH / (float)SCR_HEIGHT;
		glm::mat4 view = camera_view(camera);
		glm::mat4 projection = camera_projection(camera, aspect);
		glm::mat4 mvp = projection * view; // model is identity

		pthread_mutex_lock(&mutex);
		for (int i = 0; i < THREAD_COUNT; i++) {
			thread_params[i].time = delta_time;
			thread_params[i].run = 1;
		}
		pthread_cond_broadcast(&cond);
		pthread_mutex_unlock(&mutex);

		pthread_barrier_wait(&barrier);

		alive_count = 0;
		for (int i = 0; i < THREAD_COUNT; i++)
			alive_count += thread_params[i].alive_count;

		// collision_update(particles, alive_count, 0.3f);

		// Render
		glClearColor(0.03f, 0.03f, 0.03f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		grid_draw(g_grid, mvp);
		axis_draw(g_axis, mvp);

		glUseProgram(shader);
		glUniformMatrix4fv(glGetUniformLocation(shader, "uMVP"), 1, GL_FALSE, glm::value_ptr(mvp));

		glBindBuffer(GL_ARRAY_BUFFER, VBO);
		glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Particle) * PARTICLE_COUNT, particles);

		glBindVertexArray(VAO);
		glDrawArrays(GL_POINTS, 0, alive_count);
		glBindVertexArray(0);

#ifndef NDEBUG
		ImGui_ImplOpenGL3_NewFrame();
		ImGui_ImplGlfw_NewFrame();
		ImGui::NewFrame();

		ImGui::Begin("Debug");
		ImGui::Text("FPS: %.1f", (double)ImGui::GetIO().Framerate);
		ImGui::Text("Camera: %.2f  %.2f  %.2f", (double)camera.position.x, (double)camera.position.y, (double)camera.position.z);
		ImGui::Text("Alive particles: %d", alive_count);
		if (particles)
			ImGui::Text("P[0]: %3.2f  %3.2f  %3.2f, Life: %0.2f", (double)particles[0].position.x, (double)particles[0].position.y, (double)particles[0].position.z,
			            (double)particles[0].life);
		ImGui::Separator();
		ImGui::Text("Left-click to capture mouse");
		ImGui::Text("ESC = release mouse / quit");
		ImGui::Text("%s", errLog);
		if (ImGui::Checkbox("Thread View", &viewThreads)) {
			if (!viewThreads) {
				snprintf(errLog, 128, "Switched views to default\n");
				for (unsigned long long i = 0; i < PARTICLE_COUNT; i++)
					particles[i].color = default_particle_color[i];
			} else {

				snprintf(errLog, 128, "Switched views to thread view\n");
			}
		}
		ImGui::End();

		// Render ImGui
		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
#endif

		// Swap buffers
		glfwSwapBuffers(window);
	}

	// Cleanup

	for (int i = 0; i < THREAD_COUNT; i++) {
		thread_params[i].exit = 1;
		pthread_cond_broadcast(&cond); // Wake up threads so they can exit
	}

	free(threads);
	free(thread_params);
	free(particles);
    free(default_particle_color);

	ImGui_ImplGlfw_Shutdown();
	ImGui_ImplOpenGL3_Shutdown();
	ImGui::DestroyContext();
	return 0;
}
