//
// Created by sebastian on 29.07.26.
//

#include "Application.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include "utils/openglWrapper/GLStateCache.h"

namespace engine {
    Application::Application(const EngineConfig& config)
        : m_config(config),
          m_window(std::make_unique<Window>(config)),
          m_inputContextStack(),
          m_keyboard(*m_window),
          m_mouse(*m_window),
          m_sceneManager(m_inputContextStack) {
    }

    Application::~Application() = default;

    void Application::run() {
        auto lastTime = static_cast<float>(glfwGetTime());

        while (!m_window->shouldClose()) {
            const auto currentTime = static_cast<float>(glfwGetTime());
            const float deltaTime = currentTime - lastTime;
            lastTime = currentTime;

            m_window->pollEvents();


            m_imguiContext.beginFrame();

            onUpdate(deltaTime);

            GLStateCache::clearFrameBuffer(m_config.clearColor);

            onRender();

            m_imguiContext.endFrame();
            m_window->swapBuffers();

            m_keyboard.update();
            m_mouse.update();
        }
    }
}
