#include <fstream>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>

// glad and GLFW
#include <GLFW/glfw3.h>
#include <glad/glad.h>

// glm math
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

// HPP files
#include "shader.h"

// imGui
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"

// CURL
#include "http_get.h"
#include <curl/curl.h>

#define VERTEX_FILE "./shaders/vertex.glsl"
#define FRAGMENT_FILE "./shaders/fragment.glsl"
#define COMPUTE_FILE "./shaders/compute.glsl"
#define ENV_FILE "./.env"

const float SCR_WIDTH = 1200.0f;
const float SCR_HEIGHT = 1200.0f;

unsigned char apiExist = 0;
unsigned char apiIsValid = 0;
unsigned char isVsync = 1;
char savedLocation[128] = "Lisbon";
float location_latitude = -1;  // default invalid location_latitude
float location_longitude = -1; // default invalid location_longitude

int currentAA = 4; // 0=off, 2=2x, 4=4x, 8=8x

int main(void) {
	// Initialize GLFW
	if (!glfwInit())
		return -1;

	// Set OpenGL version (4.1 Core) and other window hints
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // REQUIRED on macOS
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_SAMPLES, currentAA);

	GLFWwindow *window = glfwCreateWindow((int)SCR_WIDTH, (int)SCR_HEIGHT, "RASTER", nullptr, nullptr);
	if (!window) {
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		std::cerr << "Failed to initialize GLAD\n";
		return -1;
	}

	glViewport(0, 0, (int)SCR_WIDTH, (int)SCR_HEIGHT);

	// DEBUG
	std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;

	// Setup Dear ImGui context
	IMGUI_CHECKVERSION();
	ImGui::CreateContext();
	ImGuiIO &io = ImGui::GetIO();
	io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
	io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;  // Enable Gamepad Controls

	// Setup Platform/Renderer backends
	ImGui_ImplGlfw_InitForOpenGL(window, true); // Second param install_callback=true will install GLFW
	                                            // callbacks and chain to existing ones.
	ImGui_ImplOpenGL3_Init();

	// DEBUG
	std::cout << "ImGui Version: " << IMGUI_VERSION << std::endl;
	std::string api_key = "";

	// Get API keys from .env file
	std::ifstream envFile(ENV_FILE);
	if (envFile.is_open()) {
		std::string line;

		while (std::getline(envFile, line)) {
			// std::cout << line << std::endl; // Print each line (for
			// debugging) Here you can parse the line to extract key-value pairs
			// if needed

			if (line.find("WEATHER_API_KEY=") == 0) {
				api_key = line.substr(16); // Extract value after "WEATHER_API_KEY="
				apiExist = 1;
			}
		}
		envFile.close();
	} else {
		std::cerr << "Could not open the .env file." << std::endl;
	}

	// ERROR: macOS does not support compute shaders in OpenGL < 4.3.
	// GLuint computeProgram = createComputeProgram(COMPUTE_FILE);

	// Load shaders
	GLuint shaderProgram = createShaderProgram(VERTEX_FILE, FRAGMENT_FILE);

	// Quad to render vertex too
	float vertices[] = {// positions
	                    -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f};

	unsigned int indices[] = {0, 1, 2, 2, 1, 3};

	GLuint VAO, VBO, EBO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);

	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	// positions
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
	glEnableVertexAttribArray(0);

	glBindVertexArray(0);

	glEnable(GL_MULTISAMPLE);

	// FPS tracking
	double lastTime = glfwGetTime(); // when we last printed FPS
	double fps = 0.0;
	int frameCount = 0; // frames since last FPS print
	float mousePos[2] = {0.0f, 0.0f};

	// Check if API key is valid (dummy check here, implement actual validation
	// as needed)
	char *url = (char *)malloc(1024);
	if (apiExist) {
		snprintf(url, 1024, "http://api.openweathermap.org/geo/1.0/direct?q=%s&appid=%s", savedLocation, api_key.c_str());
		char *response = http_get(url);
		if (response) {
			apiIsValid = 1;
			// Extract latitude and longitude from response (simple parsing,
			// improve as needed)
			std::string respStr(response);
			size_t latPos = respStr.find("\"lat\":");
			size_t lonPos = respStr.find("\"lon\":");

			if (latPos != std::string::npos && lonPos != std::string::npos) {
				sscanf(respStr.c_str() + latPos, "\"lat\":%f", &location_latitude);
				sscanf(respStr.c_str() + lonPos, "\"lon\":%f", &location_longitude);
				std::cout << "Location: " << savedLocation << " Latitude: " << location_latitude << " Longitude: " << location_longitude << std::endl;
			} else {
				std::cerr << "Could not parse latitude and longitude from API "
				             "response."
				          << std::endl;
				apiIsValid = 0;
			}

			free(response);
		} else {
			apiIsValid = 0;
		}
	}

	char *currentWeather = (char *)malloc(1024); // buffer for current weather data
	snprintf(currentWeather, 1024, "No data");

	// Render loop
	while (!glfwWindowShouldClose(window)) {
		glfwPollEvents();

		// Time
		float timeValue = (float)glfwGetTime();
		mousePos[0] = (float)io.MousePos.x;
		mousePos[1] = (float)io.MousePos.y;

		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(shaderProgram);
		GLint resLoc = glGetUniformLocation(shaderProgram, "u_resolution");
		glUniform2f(resLoc, (float)SCR_WIDTH, (float)SCR_HEIGHT);

		GLint timeLoc = glGetUniformLocation(shaderProgram, "u_time");
		glUniform1f(timeLoc, timeValue);

		// draw quad
		glBindVertexArray(VAO);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

		// API request to get weather data
		if (((int)(timeValue * 100)) % 1000 == 1 && apiExist && apiIsValid) {
			snprintf(url, 1024, "http://api.openweathermap.org/data/2.5/direct?q=%s&appid=%s", savedLocation, api_key.c_str());
			char *response = http_get(url);
			if (response) {
				snprintf(currentWeather, 1024, "%s", response);
				free(response);
			}
		}
		// Start the Dear ImGui frame
		ImGui_ImplOpenGL3_NewFrame();
		ImGui_ImplGlfw_NewFrame();
		ImGui::NewFrame();

		ImGui::Begin("Debug Window");
		ImGui::Text("FPS: %.1f", fps);
		ImGui::Text("Time: %.2f seconds", timeValue);
		ImGui::Separator();
		ImGui::Text("Options:");
		// V-Sync options
		if (ImGui::Checkbox("V-Sync", (bool *)&isVsync)) {
			glfwSwapInterval(isVsync ? 1 : 0);
		}
		ImGui::Separator();
		ImGui::Text("API Key Status:");
		if (apiExist) {
			ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "\t\tAPI Key Loaded OK");
		} else {
			ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "\t\tAPI Key Missing or Invalid!");
		}
		if (apiIsValid) {
			ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "\t\tAPI Key Validated OK");
		} else {
			ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "\t\tAPI Key Not Validated!");
		}
		ImGui::Separator();
		ImGui::Text("Program Info:");
		ImGui::Text("\t\tOpenGL Version: %s", glGetString(GL_VERSION));
		ImGui::Text("\t\tShader Program: %s", shaderProgram ? "Loaded" : "Error");

		ImGui::End();

		ImGui::Begin("Weather Data");
		if (apiExist && apiIsValid) {
			ImGui::TextWrapped("%s", currentWeather);
		} else if (!apiExist) {
			ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "No API Key found in .env file!");
		} else if (!apiIsValid) {
			ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "API Key is not valid!");
		}
		ImGui::Separator();
		ImGui::Text("API URL: %s", url);
		ImGui::End();

		// --- FPS Counter ---
		double currentTime = glfwGetTime();
		double delta = currentTime - lastTime;

		if (delta >= 1.0) { // print every ~1 second
			fps = double(frameCount) / delta;
			/* std::cout << "FPS: " << fps
			      << " Time: " << timeValue
			      << " Mouse: (" << mousePos[0] << ", " << mousePos[1] << ")"
			      << std::endl;
			*/
			frameCount = 0;
			lastTime = currentTime;
		}

		frameCount++;

		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

		glfwSwapBuffers(window);
	}

	glDeleteProgram(shaderProgram);

	ImGui_ImplOpenGL3_Shutdown();
	ImGui_ImplGlfw_Shutdown();
	ImGui::DestroyContext();

	glfwDestroyWindow(window);
	glfwTerminate();
	free(url);
	free(currentWeather);
	return 0;
}
