// debug_overlay.cpp - Implementación del overlay de debug.

#include "core/system/debug_overlay.hpp"

#include <SDL3/SDL.h>

#include <cctype>
#include <cmath>
#include <string>

#include "core/defaults.hpp"
#include "core/rendering/gpu/gpu_frame_renderer.hpp"
#include "core/types.hpp"

namespace System {

    namespace {
        namespace Cfg = Defaults::Hud::DebugOverlay;

        auto toUpperAscii(std::string s) -> std::string {
            for (char& c : s) {
                c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
            }
            return s;
        }
    }  // namespace

    DebugOverlay::DebugOverlay(Rendering::Renderer* renderer,
        const Config::RenderingConfig& rendering_cfg)
        : text_(renderer),
          renderer_(renderer),
          rendering_cfg_(&rendering_cfg) {}

    void DebugOverlay::update(float delta_time) {
        fps_accumulator_ += delta_time;
        fps_frame_count_++;

        if (fps_accumulator_ >= Cfg::FPS_UPDATE_INTERVAL) {
            fps_display_ = static_cast<int>(std::lround(static_cast<float>(fps_frame_count_) / fps_accumulator_));
            fps_frame_count_ = 0;
            fps_accumulator_ = 0.0F;
        }
    }

    void DebugOverlay::draw() const {
        if (!visible_) {
            return;
        }

        const std::string FPS_TEXT = "FPS: " + std::to_string(fps_display_);
        const std::string RES_TEXT = "RES: " + std::to_string(rendering_cfg_->render_width) + "X" + std::to_string(rendering_cfg_->render_height);
        const char* driver_raw = SDL_GetGPUDeviceDriver(renderer_->device().get());
        const std::string DRIVER_TEXT = "DRIVER: " + toUpperAscii(driver_raw != nullptr ? driver_raw : "?");
        const std::string VSYNC_TEXT = std::string("VSYNC: ") + (rendering_cfg_->vsync == 1 ? "ON" : "OFF");
        const std::string AA_TEXT = std::string("AA: ") + (rendering_cfg_->antialias == 1 ? "ON" : "OFF");

        float y = Cfg::Y_FPS;
        text_.render(FPS_TEXT,
            Vec2{.x = Cfg::X, .y = y},
            Cfg::FPS_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
        y += Cfg::FPS_LINE_HEIGHT;
        text_.render(RES_TEXT,
            Vec2{.x = Cfg::X, .y = y},
            Cfg::TEXT_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
        y += Cfg::LINE_HEIGHT;
        text_.render(DRIVER_TEXT,
            Vec2{.x = Cfg::X, .y = y},
            Cfg::TEXT_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
        y += Cfg::LINE_HEIGHT;
        text_.render(VSYNC_TEXT,
            Vec2{.x = Cfg::X, .y = y},
            Cfg::TEXT_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
        y += Cfg::LINE_HEIGHT;
        text_.render(AA_TEXT,
            Vec2{.x = Cfg::X, .y = y},
            Cfg::TEXT_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
    }

}  // namespace System
