#define STB_IMAGE_IMPLEMENTATION

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <funcs.h>
#include <shader.h>
#include <math.h>
#include <iostream>
#include "data.cpp"

glm::vec3 cubePos[] = {
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(3.0f, 0.0f, 0.0f),
    glm::vec3(1.5f, 0.0f, 2.599f),
    glm::vec3(1.5f, 1.983f, 1.3f)
};

int main() {
    // Initalize GLFW
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Open Window
    GLFWwindow* window = glfwCreateWindow(800, 600, "Howdy Naif!", NULL, NULL);
    if (window == NULL) {
        std::cout << "Could not open the iwndow!\n";
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    // Start GLAD loader
    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)){
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
    }

    // Set viewport
    glViewport(0, 0, 800, 600);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // Create Vertex Buffer and bind it to array buffer, then specifiy how to use that data
    unsigned int VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    // Element Buffers
    // unsigned int EBO;
    // glGenBuffers(1, &EBO);
    // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // Use and set shader
    Shader myShader("../shad/vert.vs", "../shad/frag.fs");
    myShader.use();

    // Texture Loading
    unsigned int texture1, texture2;
    int width1, height1, nrChannels1, width2, height2, nrChannels2;
    stbi_set_flip_vertically_on_load(true);
    unsigned char* data1 = stbi_load("wall.jpg", &width1, &height1, &nrChannels1, 0);

    if (data1) {
        glActiveTexture(GL_TEXTURE0);
        glGenTextures(1, &texture1);
        glBindTexture(GL_TEXTURE_2D, texture1);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, data1);
        glGenerateMipmap(GL_TEXTURE_2D);

        myShader.setInt("texture1", 0);

    } else {
        std::cout << "COULD NOT LOAD TEXTUREs!\n";
    }
    stbi_image_free(data1);

    // View Matrices
    glm::mat4 model = glm::mat4(1.0f);
    model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
    glm::mat4 view = glm::mat4(1.0f);
    // view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
    view = glm::lookAt(glm::vec3(0.0f, 3.0f, 3.0f),
                        glm::vec3(0.0f, 0.0f, 0.0f),
                        glm::vec3(0.0f, 1.0f, 0.0f));
    glm::mat4 projection;
    projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);


    // Camera Work
    glm::vec3 camPos = glm::vec3(0.0f, 0.0f, 3.0f);
    glm::vec3 camTarget = glm::vec3(0.0f, 0.0f, 0.0f);
    glm::vec3 camDirection = glm::normalize(camPos - camTarget);
    glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
    glm::vec3 camRight = glm::vec3(glm::cross(worldUp, camDirection));
    glm::vec3 camUp = glm::vec3(glm::cross(camDirection, camRight));

    int modelLoc, viewLoc, projLoc;
    modelLoc = glGetUniformLocation(myShader.id, "model");
    viewLoc = glGetUniformLocation(myShader.id, "view");
    projLoc = glGetUniformLocation(myShader.id, "projection");

    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
    glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

    glEnable(GL_DEPTH_TEST);

    const float radius = 10.0f;

    // Window loop
    while(!glfwWindowShouldClose(window)) {
        // Input checking
        processInput(window);

        // Camera Rotate
        float camX = sin(glfwGetTime()) * radius;
        float camZ = cos(glfwGetTime()) * radius;
        view = glm::lookAt( glm::vec3(camX, 3.0f, camZ),
                            glm::vec3(0.0f, 0.0f, 0.0f),
                            glm::vec3(0.0f, 1.0f, 0.0f));
        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

        // Rendering Loop
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBindVertexArray(VAO);
        for (unsigned int i = 0; i < 4; ++i) {
            glm::mat4 model = glm::mat4(1.0f);
            model = glm::translate(model, cubePos[i]);
            myShader.setMat4("model", model);
            glDrawArrays(GL_TRIANGLES, 0, 36);
        }

        // Events and buffer swap
        glfwPollEvents();
        glfwSwapBuffers(window);
    }

    glfwTerminate();
    return 0;
}