#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"

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, 8*sizeof(float), (void*)0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(3*sizeof(float)));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    // 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);
    unsigned char* data2 = stbi_load("awesomeface.png", &width2, &height2, &nrChannels2, 0);


    if (data1 && data2) {
        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);
        
        glActiveTexture(GL_TEXTURE1);
        glGenTextures(1, &texture2);
        glBindTexture(GL_TEXTURE_2D, texture2);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
        glGenerateMipmap(GL_TEXTURE_2D);

        myShader.setInt("texture1", 0);
        myShader.setInt("texture2", 1);

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

    // Camera Work
    glm::mat4 trans = glm::rotate(glm::mat4(1.0f), (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
    unsigned int transLoc = glGetUniformLocation(myShader.id, "transform");
    glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(trans));


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

        // Rendering Loop
        trans = glm::rotate(glm::mat4(1.0f), (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
        glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(trans));
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

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

    glfwTerminate();
    return 0;
}