/**
 * @file window.cxx
 * @author Piotr Krygier (everyonencancode@gmail.com)
 * @brief
 * @version 0.1
 * @date 2022-02-15
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "window.h"

#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_video.h>
#include <stdint.h>
#include <stdio.h>

#include "utilities/commons.h"
#include "utilities/errors_common.h"
#include "vulkan/vulkan_core.h"
#include "vulkan_base.h"

rse_err_t window_init(SDL_Window** window_handle, bool* is_framebuffer_resized)
{
    SDL_Init(SDL_INIT_VIDEO);  // TODO: Move to initial phase of RSE initialization

    (void)is_framebuffer_resized;; // TODO: Actually use this parameter?

    *window_handle = SDL_CreateWindow("RedScarfEngine", 1024, 768, SDL_WINDOW_VULKAN);

    if (NULL == *window_handle) {
        SDL_LogCritical(SDL_LOG_CATEGORY_GPU, "Window context creation failed");
        return RSE_ERROR_INTERNAL_ERROR;
    }

    return RSE_ERROR_NO_ERROR;
}

rse_err_t window_loop(struct graphics_context_t* context)
{
    uint8_t done = 0;
    SDL_Event event;

    while (0 == done) {
        while (SDL_PollEvent(&event)) {
            if (SDL_EVENT_QUIT == event.type) {
                done = 1;
            }
        }

        draw_frame(context);
    }

    vkDeviceWaitIdle(context->vulkan_handles.device);
    return RSE_ERROR_NO_ERROR;
}

void window_terminate(SDL_Window* window_handle)
{
    SDL_DestroyWindow(window_handle);
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
