#include "notifier.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h>    // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_render.h>    // for SDL_RenderFillRect, SDL_RenderClear
#include <algorithm>            // for remove_if
#include <string>               // for string, basic_string
#include <vector>               // for vector
#include "jail_audio.h"         // for JA_PlaySound
#include "options.h"            // for Options, options, OptionsNotification
#include "resource.h"           // for Resource
#include "screen.h"             // for Screen
#include "s_sprite.h"           // for SSprite
#include "text.h"               // for Text, TEXT_CENTER, TEXT_COLOR
#include "surface.h"            // for Surface

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

// [SINGLETON] Crearemos el objeto con esta función estática
void Notifier::init(const std::string &icon_file, const std::string &text)
{
    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
Notifier *Notifier::get()
{
    return Notifier::notifier_;
}

// Constructor
Notifier::Notifier(const std::string &icon_file, const std::string &text)
    : icon_surface_(!icon_file.empty() ? Resource::get()->getSurface(icon_file) : nullptr),
      text_(Resource::get()->getText(text)),
      bg_color_(options.notifications.color),
      wait_time_(150),
      stack_(false),
      has_icons_(!icon_file.empty()) {}

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

// Actualiza el estado de las notificaiones
void Notifier::update()
{
    for (int i = 0; i < (int)notifications_.size(); ++i)
    {
        // Si la notificación anterior está "saliendo", no hagas nada
        if (i > 0)
        {
            if (notifications_[i - 1].state == NotificationStatus::RISING)
            {
                break;
            }
        }

        notifications_[i].counter++;

        // Hace sonar la notificación en el primer frame
        if (notifications_[i].counter == 1)
        {
            if (options.notifications.sound)
            {
                if (notifications_[i].state == NotificationStatus::RISING)
                {
                    // Reproduce el sonido de la notificación
                    JA_PlaySound(Resource::get()->getSound("notify.wav"));
                }
            }
        }

        // Comprueba los estados
        if (notifications_[i].state == NotificationStatus::RISING)
        {
            // const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist);
            // const int alpha = 255 * step;
            // constexpr int ALPHA = 255;

            if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
            {
                notifications_[i].rect.y++;
            }
            else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
            {
                notifications_[i].rect.y--;
            }
            // notifications_[i].surface->setAlpha(ALPHA);

            if (notifications_[i].rect.y == notifications_[i].y)
            {
                notifications_[i].state = NotificationStatus::STAY;
                // notifications_[i].surface->setAlpha(255);
                notifications_[i].counter = 0;
            }
        }

        else if (notifications_[i].state == NotificationStatus::STAY)
        {
            if (notifications_[i].counter == wait_time_)
            {
                notifications_[i].state = NotificationStatus::VANISHING;
                notifications_[i].counter = 0;
            }
        }
        else if (notifications_[i].state == NotificationStatus::VANISHING)
        {

            // const float step = (notifications_[i].counter / (float)notifications_[i].travel_dist);
            // const int ALPHA = 255 * (1 - step);
            // constexpr int ALPHA = 255;

            if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
            {
                notifications_[i].rect.y--;
            }
            else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
            {
                notifications_[i].rect.y++;
            }
            // notifications_[i].surface->setAlpha(ALPHA);

            if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist)
            {
                notifications_[i].state = NotificationStatus::FINISHED;
            }
        }

        notifications_[i].sprite->setPosition(notifications_[i].rect);
    }

    clearFinishedNotifications();
}

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

void Notifier::show(std::vector<std::string> texts, NotificationText text_is, 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
    texts.erase(std::remove_if(texts.begin(), texts.end(), [](const std::string &s)
                               { return s.empty(); }),
                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
    const auto padding_in_h = text_->getCharacterSize();
    const auto padding_in_v = text_->getCharacterSize() / 2;
    const int icon_space = icon >= 0 ? ICON_SIZE_ + padding_in_h : 0;
    text_is = icon_space > 0 ? NotificationText::LEFT : text_is;
    const int width = options.game.width - (PADDING_OUT_ * 2);
    const int height = (text_->getCharacterSize() * texts.size()) + (padding_in_v * 2);
    const auto shape = NotificationShape::SQUARED;

    // Posición horizontal
    auto desp_h = 0;
    if (options.notifications.getHorizontalPosition() == NotificationPosition::LEFT)
    {
        desp_h = PADDING_OUT_;
    }
    else if (options.notifications.getHorizontalPosition() == NotificationPosition::CENTER)
    {
        desp_h = ((options.game.width / 2) - (width / 2));
    }
    else if (options.notifications.getHorizontalPosition() == NotificationPosition::RIGHT)
    {
        desp_h = options.game.width - width - PADDING_OUT_;
    }

    // Posición vertical
    const int desp_v = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? PADDING_OUT_ : options.game.height - height - PADDING_OUT_;
    // Offset
    const auto travel_dist = height + PADDING_OUT_;
    auto offset = 0;
    if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
    {
        offset = !notifications_.empty() ? notifications_.back().y + notifications_.back().travel_dist : desp_v;
    }
    else if (options.notifications.getVerticalPosition() == NotificationPosition::BOTTOM)
    {
        offset = !notifications_.empty() ? notifications_.back().y - notifications_.back().travel_dist : desp_v;
    }

    // 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;
    int y_pos = offset + ((options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -travel_dist : travel_dist);
    n.rect = {desp_h, y_pos, width, height};

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

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

    // Dibuja el fondo de la notificación
    SDL_Rect rect;
    if (shape == NotificationShape::ROUNDED)
    {
        rect = {4, 0, width - (4 * 2), height};
        n.surface->fillRect(&rect, bg_color_);

        rect = {4 / 2, 1, width - 4, height - 2};
        n.surface->fillRect(&rect, bg_color_);

        rect = {1, 4 / 2, width - 2, height - 4};
        n.surface->fillRect(&rect, bg_color_);

        rect = {0, 4, width, height - (4 * 2)};
        n.surface->fillRect(&rect, bg_color_);
    }

    else if (shape == NotificationShape::SQUARED)
    {
        n.surface->clear(bg_color_);
    }

    // Dibuja el icono de la notificación
    if (has_icons_ && icon >= 0 && texts.size() >= 2)
    {
        auto sp = std::make_unique<SSprite>(icon_surface_, (SDL_Rect){0, 0, ICON_SIZE_, ICON_SIZE_});
        sp->setPosition({padding_in_h, padding_in_v, ICON_SIZE_, ICON_SIZE_});
        sp->setClip({ICON_SIZE_ * (icon % 10), ICON_SIZE_ * (icon / 10), ICON_SIZE_, ICON_SIZE_});
        sp->render();
    }

    // Escribe el texto de la notificación
    const Uint8 color = stringToColor("white");
    int iterator = 0;
    for (const auto &text : texts)
    {
        if (text_is == NotificationText::LEFT)
        {
            text_->writeColored(padding_in_h + icon_space, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, color);
        }
        else if (text_is == NotificationText::CENTER)
        {
            text_->writeDX(TEXT_CENTER | TEXT_COLOR, width / 2, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, 1, color);
        }
        ++iterator;
    }

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

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

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

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

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

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

    clearFinishedNotifications();
}

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