#include "Engine.h"
#include <iostream>
#include <glad/glad.h> // or use GLEW if preferred

GLFWwindow* Engine::window = nullptr;

bool Engine::Init() {
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW\n";
        return false;
    }

    // Specify the OpenGL version (e.g., 3.3 Core Profile)
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Create the GLFW window
    window = glfwCreateWindow(1280, 800, "Engine Window", nullptr, nullptr);
    if (!window) {
        std::cerr << "Failed to create GLFW window\n";
        glfwTerminate();
        return false;
    }

    // Make the OpenGL context current
    glfwMakeContextCurrent(window);


    // Set the viewport size to match the framebuffer dimensions
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    glViewport(0, 0, width, height);

    return true;
}

void Engine::BeginFrame() {
    // Clear the screen with a chosen color
    glClearColor(0.0f, 0.2f, 0.4f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
}

void Engine::EndFrame() {
    // Swap the front and back buffers and process events
    glfwSwapBuffers(window);
    glfwPollEvents();
}

GLFWwindow* Engine::GetWindow() {
    return window;
}
