#include "engine/sdl.hpp"

// SDLWindow Constructor
SDLWindow::SDLWindow(const char* title, int width, int height, SDL_WindowFlags flags): running(true) {
    if (!SDL_Init(SDL_INIT_VIDEO)) {
        // Handle initialization error
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL: %s", SDL_GetError());
        running = false;
        return;
    }

    window = SDL_CreateWindow(title, width, height, flags);
    if (!window) {
        // Handle window creation error
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create SDL window: %s", SDL_GetError());
        SDL_Quit();
        running = false;
    }

    return;
}

// SDLWindow Destructor
SDLWindow::~SDLWindow()
{
    if (window) {
        SDL_DestroyWindow(window);
    }
    SDL_Quit();
}

// Poll Events
void SDLWindow::pollEvents() {
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_EVENT_QUIT) {
            running = false;
        }
        if (eventCallback) {
            eventCallback(event);
        }
    }
}

// Set Event Callback
void SDLWindow::setEventCallback(void (*callback)(const SDL_Event&)) {
    eventCallback = callback;
}

// Check if Window is Running
bool SDLWindow::isRunning() const {
    return running;
}

// Show Window
void SDLWindow::showWindow() {
    SDL_ShowWindow(window);
}

// Hide Window
void SDLWindow::hideWindow() {
    SDL_HideWindow(window);
}

// Set Window Position
void SDLWindow::setPosition(int x, int y) {
    SDL_SetWindowPosition(window, x, y);
}

// Return raw SDL_Window pointer
SDL_Window* SDLWindow::getSDLWindow() const {
    return window;
}