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

#include "core/system/debug_overlay.hpp"

#include <string>

#include "core/defaults.hpp"
#include "core/types.hpp"

namespace System {

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

    DebugOverlay::DebugOverlay(Rendering::Renderer* renderer,
        const Config::RenderingConfig& rendering_cfg)
        : text_(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>(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 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");

        text_.render(FPS_TEXT,
            Vec2{.x = Cfg::X, .y = Cfg::Y_FPS},
            Cfg::TEXT_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
        text_.render(VSYNC_TEXT,
            Vec2{.x = Cfg::X, .y = Cfg::Y_FPS + Cfg::LINE_HEIGHT},
            Cfg::TEXT_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
        text_.render(AA_TEXT,
            Vec2{.x = Cfg::X, .y = Cfg::Y_FPS + (2.0F * Cfg::LINE_HEIGHT)},
            Cfg::TEXT_SCALE,
            Cfg::TEXT_SPACING,
            Cfg::BRIGHTNESS,
            Cfg::COLOR);
    }

}  // namespace System
