#include "game/ui/notifier.hpp"

#include <SDL3/SDL.h>  // Para SDL_RenderFillRect, SDL_FRect, SDL_RenderClear

#include <algorithm>  // Para remove_if, min
#include <string>     // Para basic_string, string
#include <utility>
#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 Sprite
#include "core/rendering/text.hpp"           // Para Text
#include "core/rendering/texture.hpp"        // Para Texture
#include "utils/param.hpp"                   // Para Param, param, ParamNotification, ParamGame

// Singleton
Notifier* Notifier::instance = nullptr;

// Inicializa la instancia única del singleton
void Notifier::init(const std::string& icon_file, std::shared_ptr<Text> text) { Notifier::instance = new Notifier(icon_file, std::move(text)); }

// Libera la instancia
void Notifier::destroy() { delete Notifier::instance; }

// Obtiene la instancia
auto Notifier::get() -> Notifier* { return Notifier::instance; }

// Constructor
Notifier::Notifier(const std::string& icon_file, std::shared_ptr<Text> text)
    : renderer_(Screen::get()->getRenderer()),
      icon_texture_(!icon_file.empty() ? std::make_unique<Texture>(renderer_, icon_file) : nullptr),
      text_(std::move(text)),
      bg_color_(param.notification.color),
      stack_(false),
      has_icons_(!icon_file.empty()) {}

// Dibuja las notificaciones por pantalla
void Notifier::render() {
    for (int i = static_cast<int>(notifications_.size()) - 1; i >= 0; --i) {
        notifications_[i].sprite->render();
    }
}

// Actualiza el estado de las notificaciones
void Notifier::update(float delta_time) {
    for (int i = 0; std::cmp_less(i, notifications_.size()); ++i) {
        if (!shouldProcessNotification(i)) {
            break;
        }
        processNotification(i, delta_time);
    }
    clearFinishedNotifications();
}

auto Notifier::shouldProcessNotification(int index) const -> bool {
    // Si la notificación anterior está "saliendo", no hagas nada
    return index <= 0 || notifications_[index - 1].state != State::RISING;
}

void Notifier::processNotification(int index, float delta_time) {
    auto& notification = notifications_[index];
    notification.timer += delta_time;

    playNotificationSoundIfNeeded(notification);
    updateNotificationState(index, delta_time);
    notification.sprite->setPosition(notification.rect);
}

void Notifier::playNotificationSoundIfNeeded(const Notification& notification) {
    // Hace sonar la notificación al inicio
    if (notification.timer <= 0.016F &&
        param.notification.sound &&
        notification.state == State::RISING) {
        Audio::get()->playSound("notify.wav", Audio::Group::INTERFACE);
    }
}

void Notifier::updateNotificationState(int index, float delta_time) {
    auto& notification = notifications_[index];

    switch (notification.state) {
        case State::RISING:
            handleRisingState(index, delta_time);
            break;
        case State::STAY:
            handleStayState(index);
            break;
        case State::VANISHING:
            handleVanishingState(index, delta_time);
            break;
        default:
            break;
    }
}

void Notifier::handleRisingState(int index, float delta_time) {
    auto& notification = notifications_[index];

    const float PIXELS_TO_MOVE = ANIMATION_SPEED_PX_PER_S * delta_time;
    const float PROGRESS = notification.timer * ANIMATION_SPEED_PX_PER_S / notification.travel_dist;
    const int ALPHA = static_cast<int>(255 * std::min(PROGRESS, 1.0F));

    moveNotificationVertically(notification, param.notification.pos_v == Position::TOP ? PIXELS_TO_MOVE : -PIXELS_TO_MOVE);
    notification.texture->setAlpha(ALPHA);

    if ((param.notification.pos_v == Position::TOP && notification.rect.y >= notification.y) ||
        (param.notification.pos_v == Position::BOTTOM && notification.rect.y <= notification.y)) {
        transitionToStayState(index);
    }
}

void Notifier::handleStayState(int index) {
    auto& notification = notifications_[index];

    if (notification.timer >= STAY_DURATION_S) {
        notification.state = State::VANISHING;
        notification.timer = 0.0F;
    }
}

void Notifier::handleVanishingState(int index, float delta_time) {
    auto& notification = notifications_[index];

    const float PIXELS_TO_MOVE = ANIMATION_SPEED_PX_PER_S * delta_time;
    const float PROGRESS = notification.timer * ANIMATION_SPEED_PX_PER_S / notification.travel_dist;
    const int ALPHA = static_cast<int>(255 * (1 - std::min(PROGRESS, 1.0F)));

    moveNotificationVertically(notification, param.notification.pos_v == Position::TOP ? -PIXELS_TO_MOVE : PIXELS_TO_MOVE);
    notification.texture->setAlpha(ALPHA);

    if (PROGRESS >= 1.0F) {
        notification.state = State::FINISHED;
    }
}

void Notifier::moveNotificationVertically(Notification& notification, float pixels_to_move) {
    notification.rect.y += pixels_to_move;
}

void Notifier::transitionToStayState(int index) {
    auto& notification = notifications_[index];
    notification.state = State::STAY;
    notification.texture->setAlpha(255);
    notification.rect.y = static_cast<float>(notification.y);  // Asegurar posición exacta
    notification.timer = 0.0F;
}

// Elimina las notificaciones finalizadas
void Notifier::clearFinishedNotifications() {
    for (int i = static_cast<int>(notifications_.size()) - 1; i >= 0; --i) {
        if (notifications_[i].state == State::FINISHED) {
            notifications_.erase(notifications_.begin() + i);
        }
    }
}

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

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

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

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

    // Inicializa variables
    constexpr int ICON_SIZE = 16;
    constexpr int PADDING_OUT = 1;
    const float PADDING_IN_H = text_->getCharacterSize();
    const float PADDING_IN_V = text_->getCharacterSize() / 2;
    const int ICON_SPACE = icon >= 0 ? ICON_SIZE + PADDING_IN_H : 0;
    const float WIDTH = text_->length(longest) + (PADDING_IN_H * 2) + ICON_SPACE;
    const float HEIGHT = (text_->getCharacterSize() * texts.size()) + (PADDING_IN_V * 2);
    const auto SHAPE = Shape::SQUARED;

    // Posición horizontal
    float desp_h = 0;
    switch (param.notification.pos_h) {
        case Position::LEFT:
            desp_h = PADDING_OUT;
            break;

        case Position::MIDDLE:
            desp_h = ((param.game.width / 2) - (WIDTH / 2));
            break;

        case Position::RIGHT:
            desp_h = param.game.width - WIDTH - PADDING_OUT;
            break;

        default:
            desp_h = 0;
            break;
    }

    // Posición vertical
    const int DESP_V = (param.notification.pos_v == Position::TOP) ? PADDING_OUT : (param.game.height - HEIGHT - PADDING_OUT);

    // Offset
    const auto TRAVEL_DIST = HEIGHT + PADDING_OUT;
    auto offset = notifications_.empty()
        ? DESP_V
        : notifications_.back().y + (param.notification.pos_v == Position::TOP ? TRAVEL_DIST : -TRAVEL_DIST);

    // Crea la notificacion
    Notification n;

    // Inicializa variables
    n.code = code;
    n.y = offset;
    n.travel_dist = TRAVEL_DIST;
    n.texts = texts;
    n.shape = SHAPE;
    const float POS_Y = offset + (param.notification.pos_v == Position::TOP ? -TRAVEL_DIST : TRAVEL_DIST);
    n.rect = {.x = desp_h, .y = POS_Y, .w = WIDTH, .h = HEIGHT};

    // Crea la textura
    n.texture = std::make_shared<Texture>(renderer_);
    n.texture->createBlank(WIDTH, HEIGHT, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET);
    n.texture->setBlendMode(SDL_BLENDMODE_BLEND);

    // Prepara para dibujar en la textura
    n.texture->setAsRenderTarget(renderer_);

    // Dibuja el fondo de la notificación
    SDL_SetRenderDrawColor(renderer_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
    SDL_FRect rect;
    if (SHAPE == Shape::ROUNDED) {
        rect = {.x = 4, .y = 0, .w = WIDTH - (4 * 2), .h = HEIGHT};
        SDL_RenderFillRect(renderer_, &rect);

        rect = {.x = 4 / 2, .y = 1, .w = WIDTH - 4, .h = HEIGHT - 2};
        SDL_RenderFillRect(renderer_, &rect);

        rect = {.x = 1, .y = 4 / 2, .w = WIDTH - 2, .h = HEIGHT - 4};
        SDL_RenderFillRect(renderer_, &rect);

        rect = {.x = 0, .y = 4, .w = WIDTH, .h = HEIGHT - (4 * 2)};
        SDL_RenderFillRect(renderer_, &rect);
    }

    else if (SHAPE == Shape::SQUARED) {
        SDL_RenderClear(renderer_);
    }

    // Dibuja el icono de la notificación
    if (has_icons_ && icon >= 0 && texts.size() >= 2) {
        auto sp = std::make_unique<Sprite>(icon_texture_, (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->setSpriteClip(SDL_FRect{
            .x = static_cast<float>(ICON_SIZE * (icon % 10)),
            .y = static_cast<float>(ICON_SIZE * (icon / 10)),
            .w = ICON_SIZE,
            .h = ICON_SIZE});
        sp->render();
    }

    // Escribe el texto de la notificación
    const Color COLOR{255, 255, 255};
    int iterator = 0;
    for (const auto& text : texts) {
        text_->writeColored(PADDING_IN_H + ICON_SPACE, PADDING_IN_V + (iterator * (text_->getCharacterSize() + 1)), text, COLOR);
        ++iterator;
    }

    // Deja de dibujar en la textura
    SDL_SetRenderTarget(renderer_, nullptr);

    // Crea el sprite de la notificación
    n.sprite = std::make_shared<Sprite>(n.texture, n.rect);

    // Deja la notificación invisible
    n.texture->setAlpha(0);

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

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

    clearFinishedNotifications();
}

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