#include "notifier.h"
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
#include <string>               // Para basic_string, string, char_traits
#include "jail_audio.h"         // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
#include "sprite.h"             // Para Sprite
#include "text.h"               // Para Text
#include "texture.h"            // Para Texture
#include "screen.h"
#include "options.h"

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

// [SINGLETON] Crearemos el objeto con esta función estática
void Notifier::init(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile)
{
    Notifier::notifier_ = new Notifier(iconFile, bitmapFile, textFile, soundFile);
}

// [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(std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile)
{
    // Inicializa variables
    renderer_ = Screen::get()->getRenderer();
    bg_color_ = options.notifications.color;
    wait_time_ = 300;

    // Crea objetos
    icon_texture_ = new Texture(renderer_, iconFile);
    text_texture_ = new Texture(renderer_, bitmapFile);
    text_ = new Text(textFile, text_texture_, renderer_);
    sound_ = JA_LoadSound(soundFile.c_str());
}

// Destructor
Notifier::~Notifier()
{
    // Libera la memoria de los objetos
    delete text_texture_;
    delete icon_texture_;
    delete text_;
    JA_DeleteSound(sound_);

    for (auto notification : notifications_)
    {
        delete notification.sprite;
        delete notification.texture;
    }
}

// Dibuja las notificaciones por pantalla
void Notifier::render()
{
    if (active())
    {
        for (auto it = notifications_.rbegin(); it != notifications_.rend(); ++it)
        {
            it->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 == ns_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 == ns_rising)
                { // Reproduce el sonido de la notificación
                    JA_PlaySound(sound_);
                }
            }
        }

        // Comprueba los estados
        if (notifications_[i].state == ns_rising)
        {
            const float step = ((float)notifications_[i].counter / notifications_[i].travelDist);
            const int alpha = 255 * step;

            if (options.notifications.getVerticalPosition() == "UPPER")
            {
                notifications_[i].rect.y++;
            }
            else if (options.notifications.getVerticalPosition() == "BOTTOM")
            {
                notifications_[i].rect.y--;
            }
            notifications_[i].texture->setAlpha(alpha);

            if (notifications_[i].rect.y == notifications_[i].y)
            {
                notifications_[i].state = ns_stay;
                notifications_[i].texture->setAlpha(255);
                notifications_[i].counter = 0;
            }
        }

        else if (notifications_[i].state == ns_stay)
        {
            if (notifications_[i].counter == wait_time_)
            {
                notifications_[i].state = ns_vanishing;
                notifications_[i].counter = 0;
            }
        }
        else if (notifications_[i].state == ns_vanishing)
        {

            const float step = (notifications_[i].counter / (float)notifications_[i].travelDist);
            const int alpha = 255 * (1 - step);

            if (options.notifications.getVerticalPosition() == "UPPER")
            {
                notifications_[i].rect.y--;
            }
            else if (options.notifications.getVerticalPosition() == "BOTTOM")
            {
                notifications_[i].rect.y++;
            }
            notifications_[i].texture->setAlpha(alpha);

            if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travelDist)
            {
                notifications_[i].state = ns_finished;
            }
        }

        notifications_[i].sprite->setRect(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 == ns_finished)
        {
            delete notifications_[i].sprite;
            delete notifications_[i].texture;
            notifications_.erase(notifications_.begin() + i);
        }
    }
}

// Muestra una notificación de texto por pantalla;
void Notifier::show(std::string text1, std::string text2, int icon)
{
    // Inicializa variables
    const int iconSize = 16;
    const int padding = text_->getCharacterSize();
    const int iconSpace = icon >= 0 ? iconSize + padding : 0;
    const std::string txt = text1.length() > text2.length() ? text1 : text2;
    const int width = text_->lenght(txt) + (padding * 2) + iconSpace;
    const int height = (text_->getCharacterSize() * 2) + (padding * 2);

    // Posición horizontal
    int despH = 0;
    if (options.notifications.getHorizontalPosition() == "LEFT")
    {
        despH = padding;
    }
    else if (options.notifications.getHorizontalPosition() == "CENTER")
    {
        despH = (options.game.width - width) / 2;
    }
    else if (options.notifications.getHorizontalPosition() == "RIGHT")
    {
        despH = options.game.width - width - padding;
    }

    // Posición vertical
    int despV = 0;
    if (options.notifications.getVerticalPosition() == "UPPER")
    {
        despV = padding;
    }
    else
    {
        despV = options.game.height - height - padding;
    }

    const int travelDist = height + padding;

    // Offset
    int offset = 0;
    if (options.notifications.getVerticalPosition() == "UPPER")
    {
        offset = static_cast<int>(notifications_.size()) > 0 ? notifications_.back().y + travelDist : despV;
    }
    else
    {
        offset = static_cast<int>(notifications_.size()) > 0 ? notifications_.back().y - travelDist : despV;
    }

    // Crea la notificacion
    notification_t n;

    // Inicializa variables
    n.y = offset;
    n.travelDist = travelDist;
    n.counter = 0;
    n.state = ns_rising;
    n.text1 = text1;
    n.text2 = text2;
    if (options.notifications.getVerticalPosition() == "UPPER")
    {
        n.rect = {despH, offset - travelDist, width, height};
    }
    else
    {
        n.rect = {despH, offset + travelDist, width, height};
    }

    // Crea la textura
    n.texture = new Texture(renderer_);
    n.texture->createBlank(renderer_, width, height, 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_Rect rect;
    rect = {4, 0, width - (4 * 2), height};
    SDL_RenderFillRect(renderer_, &rect);

    rect = {4 / 2, 1, width - 4, height - 2};
    SDL_RenderFillRect(renderer_, &rect);

    rect = {1, 4 / 2, width - 2, height - 4};
    SDL_RenderFillRect(renderer_, &rect);

    rect = {0, 4, width, height - (4 * 2)};
    SDL_RenderFillRect(renderer_, &rect);

    // Dibuja el icono de la notificación
    if (icon >= 0)
    {
        Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, icon_texture_, renderer_);
        sp->setPos({padding, padding, iconSize, iconSize});
        sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
        sp->render();
        delete sp;
    }

    // Escribe el texto de la notificación
    Color color = {255, 255, 255};
    if (text2 != "")
    { // Dos lineas de texto
        text_->writeColored(padding + iconSpace, padding, text1, color);
        text_->writeColored(padding + iconSpace, padding + text_->getCharacterSize() + 1, text2, color);
    }
    else
    { // Una linea de texto
        text_->writeColored(padding + iconSpace, (height / 2) - (text_->getCharacterSize() / 2), text1, color);
    }

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

    // Crea el sprite de la notificación
    n.sprite = new Sprite(n.rect, n.texture, renderer_);

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

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