#ifdef _DEBUG

#include "game/editor/editor_statusbar.hpp"

#include <string>  // Para to_string
#include <utility>

#include "core/rendering/screen.hpp"          // Para Screen
#include "core/rendering/surface.hpp"         // Para Surface
#include "core/rendering/text.hpp"            // Para Text
#include "core/resources/resource_cache.hpp"  // Para Resource::Cache
#include "game/options.hpp"                   // Para Options::game
#include "utils/defines.hpp"                  // Para Tile::SIZE
#include "utils/utils.hpp"                    // Para stringToColor, toLower

// Constructor
EditorStatusBar::EditorStatusBar(std::string room_number, std::string room_name)
    : surface_(std::make_shared<Surface>(Options::game.width, 6.0F * Tile::SIZE)),
      surface_dest_{.x = 0, .y = Options::game.height - (6.0F * Tile::SIZE), .w = Options::game.width, .h = 6.0F * Tile::SIZE},
      room_number_(std::move(room_number)),
      room_name_(std::move(room_name)) {
}

// Pinta la barra de estado en pantalla
void EditorStatusBar::render() {
    surface_->render(nullptr, &surface_dest_);
}

// Actualiza la barra de estado
void EditorStatusBar::update([[maybe_unused]] float delta_time) {
    fillTexture();
}

void EditorStatusBar::setMouseTile(int tile_x, int tile_y) {
    mouse_tile_x_ = tile_x;
    mouse_tile_y_ = tile_y;
}

void EditorStatusBar::setLine2(const std::string& text) { line2_ = text; }
void EditorStatusBar::setLine3(const std::string& text) { line3_ = text; }
void EditorStatusBar::setLine4(const std::string& text) { line4_ = text; }
void EditorStatusBar::setLine5(const std::string& text) { line5_ = text; }

// Dibuja los elementos en la surface
void EditorStatusBar::fillTexture() {
    auto previous_renderer = Screen::get()->getRendererSurface();
    Screen::get()->setRendererSurface(surface_);

    surface_->clear(stringToColor("black"));

    auto text = Resource::Cache::get()->getText("8bithud");
    const Uint8 LABEL_COLOR = stringToColor("bright_cyan");
    const Uint8 VALUE_COLOR = stringToColor("white");
    const Uint8 DETAIL_COLOR = stringToColor("bright_yellow");

    // Línea 1: Nombre de la habitación
    text->writeColored(LEFT_X, LINE1_Y, toLower(room_number_ + " " + room_name_), LABEL_COLOR);

    // Línea 2: Propiedades de room o info de enemigo
    if (!line2_.empty()) {
        text->writeColored(LEFT_X, LINE2_Y, toLower(line2_), DETAIL_COLOR);
    }

    // Línea 3: Conexiones+items o propiedades del enemigo
    if (!line3_.empty()) {
        text->writeColored(LEFT_X, LINE3_Y, toLower(line3_), VALUE_COLOR);
    }

    // Línea 4: Extra
    if (!line4_.empty()) {
        text->writeColored(LEFT_X, LINE4_Y, toLower(line4_), DETAIL_COLOR);
    }

    // Línea 5: Tile coords + drag info
    const std::string TILE_X_STR = (mouse_tile_x_ < 10 ? "0" : "") + std::to_string(mouse_tile_x_);
    const std::string TILE_Y_STR = (mouse_tile_y_ < 10 ? "0" : "") + std::to_string(mouse_tile_y_);
    std::string line5 = "tile:" + TILE_X_STR + "," + TILE_Y_STR;
    if (!line5_.empty()) {
        line5 += "  " + line5_;
    }
    text->writeColored(LEFT_X, LINE5_Y, toLower(line5), stringToColor("bright_green"));

    Screen::get()->setRendererSurface(previous_renderer);
}

#endif  // _DEBUG
