#include "game/ui/notifier.hpp"

#include <SDL3/SDL.h>

#include <algorithm>  // Para remove_if
#include <iterator>   // Para prev
#include <ranges>     // Para reverse_view
#include <string>     // Para string, basic_string
#include <vector>     // Para vector

#include "core/audio/audio.hpp"               // Para Audio
#include "core/rendering/screen.hpp"          // Para Screen
#include "core/rendering/sprite/sprite.hpp"   // Para SSprite
#include "core/rendering/surface.hpp"         // Para Surface
#include "core/rendering/text.hpp"            // Para Text, Text::CENTER_FLAG, Text::COLOR_FLAG
#include "core/resources/resource_cache.hpp"  // Para Resource
#include "game/options.hpp"                   // Para Options, options, NotificationPosition
#include "game/ui/console.hpp"                // Para Console
#include "utils/delta_timer.hpp"              // Para DeltaTimer
#include "utils/utils.hpp"                    // Para PaletteColor

// [SINGLETON]
Notifier* Notifier::notifier = nullptr;

// Definición de estilos predefinidos
const Notifier::Style Notifier::Style::DEFAULT = {
    .bg_color = static_cast<Uint8>(PaletteColor::BLUE),
    .border_color = static_cast<Uint8>(PaletteColor::CYAN),
    .text_color = static_cast<Uint8>(PaletteColor::CYAN),
    .shape = Notifier::Shape::SQUARED,
    .text_align = Notifier::TextAlign::CENTER,
    .duration = 2.0F,
    .sound_file = "notify.wav",
    .play_sound = false};

const Notifier::Style Notifier::Style::CHEEVO = {
    .bg_color = static_cast<Uint8>(PaletteColor::MAGENTA),
    .border_color = static_cast<Uint8>(PaletteColor::BRIGHT_MAGENTA),
    .text_color = static_cast<Uint8>(PaletteColor::WHITE),
    .shape = Notifier::Shape::SQUARED,
    .text_align = Notifier::TextAlign::CENTER,
    .duration = 4.0F,
    .sound_file = "notify.wav",
    .play_sound = true};

// [SINGLETON] Crearemos el objeto con esta función estática
void Notifier::init(const std::string& icon_file, const std::string& text) {  // NOLINT(readability-convert-member-functions-to-static)
    Notifier::notifier = new Notifier(icon_file, text);
}

// [SINGLETON] Destruiremos el objeto con esta función estática
void Notifier::destroy() {
    delete Notifier::notifier;
}

// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
auto Notifier::get() -> Notifier* {
    return Notifier::notifier;
}

// Constructor
Notifier::Notifier(const std::string& icon_file, const std::string& text)
    : icon_surface_(!icon_file.empty() ? Resource::Cache::get()->getSurface(icon_file) : nullptr),
      text_(Resource::Cache::get()->getText(text)),
      delta_timer_(std::make_unique<DeltaTimer>()),
      has_icons_(!icon_file.empty()) {}

// Dibuja las notificaciones por pantalla
void Notifier::render() {
    for (auto& notification : std::ranges::reverse_view(notifications_)) {
        notification.sprite->render();
    }
}

// Actualiza el estado de las notificaiones
void Notifier::update(float delta_time) {
    // Base Y leída cada frame: cada notificación se dibuja en rect.y (relativo a BASE) + BASE
    const auto BASE = static_cast<float>(getStackBaseY());

    for (auto& notification : notifications_) {
        // Si la notificación anterior está "entrando", no hagas nada (stall del resto)
        if (!notifications_.empty() && &notification != &notifications_.front()) {
            const auto& previous_notification = *(std::prev(&notification));
            if (previous_notification.state == Status::RISING) {
                break;
            }
        }

        switch (notification.state) {
            case Status::RISING: {
                const auto TARGET = static_cast<float>(notification.y);
                notification.rect.y += SLIDE_SPEED * delta_time;
                if (notification.rect.y >= TARGET) {
                    notification.rect.y = TARGET;
                    notification.state = Status::STAY;
                    notification.elapsed_time = 0.0F;
                }
                break;
            }
            case Status::STAY: {
                notification.rect.y = static_cast<float>(notification.y);
                notification.elapsed_time += delta_time;
                if (notification.elapsed_time >= notification.display_duration) {
                    notification.state = Status::VANISHING;
                }
                break;
            }

            case Status::VANISHING: {
                const auto TARGET_Y = static_cast<float>(notification.y - notification.travel_dist);
                notification.rect.y -= SLIDE_SPEED * delta_time;
                if (notification.rect.y <= TARGET_Y) {
                    notification.rect.y = TARGET_Y;
                    notification.state = Status::FINISHED;
                }
                break;
            }

            case Status::FINISHED:
            default:
                break;
        }
    }

    // Refrescar posiciones de sprite cada frame (convierte rect.y relativo a absoluto)
    for (auto& notification : notifications_) {
        SDL_FRect sprite_rect = notification.rect;
        sprite_rect.y += BASE;
        notification.sprite->setPosition(sprite_rect);
    }

    clearFinishedNotifications();
}

// Elimina las notificaciones finalizadas
void Notifier::clearFinishedNotifications() {  // NOLINT(readability-convert-member-functions-to-static)
    auto result = std::ranges::remove_if(notifications_, [](const Notification& notification) -> bool {
        return notification.state == Status::FINISHED;
    });
    notifications_.erase(result.begin(), result.end());
}

void Notifier::show(std::vector<std::string> texts, const Style& style, int icon, bool can_be_removed, const std::string& code) {
    // Si no hay texto, acaba
    if (texts.empty()) {
        return;
    }

    // Si las notificaciones no se apilan, elimina las anteriores
    if (!stack_) {
        clearNotifications();
    }

    // Elimina las cadenas vacías
    auto result = std::ranges::remove_if(texts, [](const std::string& s) -> bool { return s.empty(); });
    texts.erase(result.begin(), result.end());

    // Encuentra la cadena más larga
    std::string longest;
    for (const auto& text : texts) {
        if (text.length() > longest.length()) {
            longest = text;
        }
    }

    // Inicializa variables
    const int TEXT_SIZE = 6;
    const auto PADDING_IN_H = TEXT_SIZE;
    const auto PADDING_IN_V = TEXT_SIZE / 2;
    const int ICON_SPACE = icon >= 0 ? ICON_SIZE + PADDING_IN_H : 0;
    const TextAlign TEXT_IS = ICON_SPACE > 0 ? TextAlign::LEFT : style.text_align;
    const float WIDTH = Options::game.width - (PADDING_OUT * 2);
    const float HEIGHT = (TEXT_SIZE * static_cast<float>(texts.size())) + (PADDING_IN_V * 2);
    const auto SHAPE = style.shape;

    // Posición horizontal
    float desp_h = ((Options::game.width / 2) - (WIDTH / 2));

    // Offset vertical (relativo a la base de la pila, que se consulta cada frame)
    const auto TRAVEL_DIST = HEIGHT + PADDING_OUT;
    const int TRAVEL_MOD = 1;
    const int OFFSET = !notifications_.empty()
        ? notifications_.back().y + (TRAVEL_MOD * notifications_.back().travel_dist)
        : 0;

    // Crea la notificacion
    Notification n;

    // Inicializa variables
    n.code = code;
    n.can_be_removed = can_be_removed;
    n.y = OFFSET;
    n.travel_dist = TRAVEL_DIST;
    n.texts = texts;
    n.shape = SHAPE;
    n.display_duration = style.duration;
    // Posición inicial relativa a la base: arranca "travel_dist" por encima del target (=OFFSET)
    const float Y_POS_REL = static_cast<float>(OFFSET) - TRAVEL_DIST;
    n.rect = {.x = desp_h, .y = Y_POS_REL, .w = WIDTH, .h = HEIGHT};

    // Crea la textura
    n.surface = std::make_shared<Surface>(WIDTH, HEIGHT);

    // Prepara para dibujar en la textura
    auto previuos_renderer = Screen::get()->getRendererSurface();
    Screen::get()->setRendererSurface(n.surface);

    // Dibuja el fondo de la notificación
    SDL_FRect rect;
    if (SHAPE == Shape::ROUNDED) {
        rect = {.x = 4, .y = 0, .w = WIDTH - (4 * 2), .h = HEIGHT};
        n.surface->fillRect(&rect, style.bg_color);

        rect = {.x = 4 / 2, .y = 1, .w = WIDTH - 4, .h = HEIGHT - 2};
        n.surface->fillRect(&rect, style.bg_color);

        rect = {.x = 1, .y = 4 / 2, .w = WIDTH - 2, .h = HEIGHT - 4};
        n.surface->fillRect(&rect, style.bg_color);

        rect = {.x = 0, .y = 4, .w = WIDTH, .h = HEIGHT - (4 * 2)};
        n.surface->fillRect(&rect, style.bg_color);
    }

    else if (SHAPE == Shape::SQUARED) {
        n.surface->clear(style.bg_color);
        SDL_FRect squared_rect = {.x = 0, .y = 0, .w = static_cast<float>(n.surface->getWidth()), .h = static_cast<float>(n.surface->getHeight())};
        n.surface->drawRectBorder(&squared_rect, style.border_color);
    }

    // Dibuja el icono de la notificación
    if (has_icons_ && icon >= 0 && texts.size() >= 2) {
        auto sp = std::make_unique<Sprite>(icon_surface_, SDL_FRect{.x = 0, .y = 0, .w = ICON_SIZE, .h = ICON_SIZE});
        sp->setPosition({.x = PADDING_IN_H, .y = PADDING_IN_V, .w = ICON_SIZE, .h = ICON_SIZE});
        sp->setClip(SDL_FRect{.x = ICON_SIZE * (icon % 10), .y = ICON_SIZE * (icon / 10), .w = ICON_SIZE, .h = ICON_SIZE});
        sp->render();
    }

    // Escribe el texto de la notificación
    const auto COLOR = style.text_color;
    int iterator = 0;
    for (const auto& text : texts) {
        switch (TEXT_IS) {
            case TextAlign::LEFT:
                text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + (iterator * (TEXT_SIZE + 1)), text, COLOR);
                break;
            case TextAlign::CENTER:
                text_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG, WIDTH / 2, PADDING_IN_V + (iterator * (TEXT_SIZE + 1)), text, 1, COLOR);
                break;
            default:
                break;
        }

        ++iterator;
    }

    // Deja de dibujar en la textura
    Screen::get()->setRendererSurface(previuos_renderer);

    // Crea el sprite de la notificación (rect absoluto a partir del relativo + BASE)
    SDL_FRect initial_sprite_rect = n.rect;
    initial_sprite_rect.y += static_cast<float>(getStackBaseY());
    n.sprite = std::make_shared<Sprite>(n.surface, initial_sprite_rect);

    // Añade la notificación a la lista
    notifications_.emplace_back(n);

    // Reproduce el sonido de la notificación
    if (style.play_sound && !style.sound_file.empty()) {
        Audio::get()->playSound(style.sound_file, Audio::Group::INTERFACE);
    }
}

// Indica si hay notificaciones activas
auto Notifier::isActive() -> bool { return !notifications_.empty(); }

// Finaliza y elimnina todas las notificaciones activas
void Notifier::clearNotifications() {
    for (auto& notification : notifications_) {
        if (notification.can_be_removed) {
            notification.state = Status::FINISHED;
        }
    }

    clearFinishedNotifications();
}

// Y absoluta de la base de la pila (justo debajo de Console, o 0 si no hay Console)
auto Notifier::getStackBaseY() -> int {
    return Console::get() != nullptr ? Console::get()->getVisibleHeight() : 0;
}

// Altura animada ocupada por la pila (usa rect.y animado, no el target — para transiciones suaves)
auto Notifier::getVisibleHeight() const -> int {
    int bottom = 0;
    for (const auto& n : notifications_) {
        if (n.state == Status::FINISHED) { continue; }
        const int N_BOTTOM = static_cast<int>(n.rect.y + n.rect.h);
        bottom = std::max(N_BOTTOM, bottom);
    }
    return bottom;
}

// Obtiene los códigos de las notificaciones
auto Notifier::getCodes() -> std::vector<std::string> {
    std::vector<std::string> codes;
    codes.reserve(notifications_.size());
    std::ranges::transform(notifications_, std::back_inserter(codes), [](const auto& notification) { return notification.code; });
    return codes;
}