#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/ElementDocument.h>
#include <RmlUi/Debugger/Debugger.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_log.h>
#include <iostream>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <RmlUi/Core.h>
#include <RmlUi/Debugger.h>
#include <RmlUi_Backend.h>
#include "sdl_file_interface.hpp"

static Rml::Context *ctx = nullptr;
static Rml::ElementDocument *doc = nullptr;
static bool is_running = false;
static SDLFileInterface file_interface;

int main(int arc, char *argv[]) {
  std::cout << "Hello world!\n";

  if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
    SDL_Log("[ERROR] Failed to initalize SDL3");
    return 1;
  }

  if (!Backend::Initialize("main", 800, 600, true)) {
    SDL_Log("[ERROR] Failed to initalize RmlUi Backend");
    return 1;
  }

  Rml::SetSystemInterface(Backend::GetSystemInterface());
  Rml::SetRenderInterface(Backend::GetRenderInterface());
  Rml::SetFileInterface(&file_interface);
  Rml::Initialise();

  int window_width = 800;
  int window_height = 600;
    {
        int window_count = 0;
        SDL_Window **windows = SDL_GetWindows(&window_count);
        if (windows && window_count > 0) {
            SDL_GetWindowSize(windows[0], &window_width, &window_height);
        }
        SDL_free(windows);
    }


  ctx = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));

    {
        int window_count = 0;
        SDL_Window **windows = SDL_GetWindows(&window_count);
        if (windows && window_count > 0) {
            float display_scale = SDL_GetWindowDisplayScale(windows[0]);
            ctx->SetDensityIndependentPixelRatio(display_scale);
            SDL_Log("Display scale (dp ratio): %f", display_scale);
        }
        SDL_free(windows);
    }

  if (!ctx) {
    SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[ERROR] Failed to create context");
    return 1;
  }

  Rml::Debugger::Initialise(ctx);
  
  doc = ctx->LoadDocument("./assets/main.rml");
  if (!doc) {
    SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[ERROR] Failed to load main.rml");
  }
  doc->Show();

  is_running = true;
  while (is_running) {
    is_running = Backend::ProcessEvents(ctx);

            {
            int window_count = 0;
            SDL_Window **windows = SDL_GetWindows(&window_count);
            if (windows && window_count > 0) {
                int current_width = 0, current_height = 0;
                SDL_GetWindowSize(windows[0], &current_width, &current_height);
                
                float display_scale = SDL_GetWindowDisplayScale(windows[0]);
                
                if (Rml::Vector2i(current_width, current_height) != ctx->GetDimensions()) {
                    ctx->SetDimensions(Rml::Vector2i(current_width, current_height));
                }
                
                if (display_scale != ctx->GetDensityIndependentPixelRatio()) {
                    ctx->SetDensityIndependentPixelRatio(display_scale);
                }
            }
            SDL_free(windows);
        }

    ctx->Update();
    Backend::BeginFrame();
    ctx->Render();
    Backend::PresentFrame();
  }

  Rml::Shutdown();
  Backend::Shutdown();
  SDL_Quit();
}
