#include "engine/sdl.hpp"
#include "engine/opengl.hpp"
#include "engine/obj_loader.hpp"

int main(int argc, char* argv[]) {
    SDLWindow window("My SDL Application", 800, 600);
    if (!window.isRunning()) { return -1; }
    OpenGLContext glContext(window);
    if (!glContext.isInitialized()) { return -1; }
    glEnable(GL_DEPTH_TEST); 

    Camera camera(
        glm::vec3(3.0f, 4.0f, 6.0f), // Position
        glm::vec3(0.0f, 1.0f, 0.0f), // Target
        glm::vec3(0.0f, 1.0f, 0.0f)  // Up vector
    );

    camera.setProjectionMatrix(
        45.0f,               // Field of View
        800.0f / 600.0f,    // Aspect Ratio
        0.1f,                // Near Plane
        100.0f               // Far Plane
    );

    Shader shader_program("shaders/triangle.vert", "shaders/triangle.frag");

    OBJLoader objLoader("assets/models/Sci-fi_Container_Game.obj");
    if (!objLoader.isLoaded()) {
        SDL_Log("Failed to load OBJ model.");
        return -1;
    }

    VertexArrayObject VAO;
    VAO.bufferVertexData(objLoader.getVertexData(), objLoader.getVertexCount() * sizeof(float) * 8);
    VAO.bufferElementData(objLoader.getIndexData(), objLoader.getIndexCount() * sizeof(unsigned int));
    VAO.setAttributePointer(0, 3, GL_FLOAT, false, 8 * sizeof(float), 0); // position
    VAO.setAttributePointer(1, 3, GL_FLOAT, false, 8 * sizeof(float), 3 * sizeof(float)); // normal
    VAO.setAttributePointer(2, 2, GL_FLOAT, false, 8 * sizeof(float), 6 * sizeof(float)); // texCoord

    Texture texture("assets/textures/Sci-fi_Container_Base_Color.png");
    texture.bind(GL_TEXTURE0);

    window.setPosition(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
    window.showWindow();

    while (window.isRunning()) {
        window.pollEvents();

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        shader_program.use();
        shader_program.setMat4("view", camera.getViewMatrix());
        shader_program.setMat4("projection", camera.getProjectionMatrix());
        shader_program.setMat4("model", glm::mat4(1.0f));

        VAO.draw();

        glContext.swapBuffers();
    }

    return 0;
}