#include "notify.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h>    // for SDL_PIXELFORMAT_RGBA8888
#include <string>               // for basic_string, char_traits, string
#include "jail_audio.h"         // for JA_DeleteSound, JA_LoadSound, JA_Pla...
#include "options.h"            // for options
#include "param.h"
#include "sprite.h"  // for Sprite
#include "text.h"    // for Text
#include "texture.h" // for Texture

// Constructor
Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile)
    : renderer(renderer)
{
    // Inicializa variables
    bgColor = options.notification.color;
    waitTime = 150;
    stack = false;
    hasIcons = iconFile == "" ? false : true;

    // Crea objetos
    if (hasIcons)
    {
        iconTexture = std::make_unique<Texture>(renderer, iconFile);
    }
    text = std::make_unique<Text>(bitmapFile, textFile, renderer);
    sound = JA_LoadSound(soundFile.c_str());
}

// Destructor
Notify::~Notify()
{
    // Libera la memoria de los objetos
    JA_DeleteSound(sound);

    notifications.clear();
}

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

// Actualiza el estado de las notificaiones
void Notify::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].status == NotificationStatus::RISING)
            {
                break;
            }
        }

        notifications[i].counter++;

        // Hace sonar la notificación en el primer frame
        if (notifications[i].counter == 1)
        {
            if (options.notification.sound)
            {
                if (notifications[i].status == NotificationStatus::RISING)
                { // Reproduce el sonido de la notificación
                    JA_PlaySound(sound);
                }
            }
        }

        // Comprueba los estados
        if (notifications[i].status == NotificationStatus::RISING)
        {
            const float step = ((float)notifications[i].counter / notifications[i].travelDist);
            const int alpha = 255 * step;

            if (options.notification.pos_v == NotifyPosition::TOP)
            {
                notifications[i].rect.y++;
            }
            else
            {
                notifications[i].rect.y--;
            }
            notifications[i].texture->setAlpha(alpha);

            if (notifications[i].rect.y == notifications[i].y)
            {
                notifications[i].status = NotificationStatus::STAY;
                notifications[i].texture->setAlpha(255);
                notifications[i].counter = 0;
            }
        }

        else if (notifications[i].status == NotificationStatus::STAY)
        {
            if (notifications[i].counter == waitTime)
            {
                notifications[i].status = NotificationStatus::VANISHING;
                notifications[i].counter = 0;
            }
        }
        else if (notifications[i].status == NotificationStatus::VANISHING)
        {

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

            if (options.notification.pos_v == NotifyPosition::TOP)
            {
                notifications[i].rect.y--;
            }
            else
            {
                notifications[i].rect.y++;
            }
            notifications[i].texture->setAlpha(alpha);

            if (notifications[i].rect.y == notifications[i].y - notifications[i].travelDist)
            {
                notifications[i].status = NotificationStatus::FINISHED;
            }
        }

        notifications[i].sprite->setPos(notifications[i].rect);
    }

    clearFinishedNotifications();
}

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

// Muestra una notificación de texto por pantalla;
void Notify::showText(std::string text1, std::string text2, int icon)
{
    // Cuenta el número de textos a mostrar
    const int numTexts = (text1 != "") + (text2 != "");

    // Si no hay texto, acaba
    if (numTexts == 0)
    {
        return;
    }
    // Si solo hay un texto, lo coloca en la primera variable
    else if (numTexts == 1)
    {
        text1 = text1 + text2;
        text2 = "";
    }

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

    // Inicializa variables
    constexpr auto iconSize = 16;
    constexpr auto paddingOut = 1;
    const auto paddingIn = text->getCharacterSize() / 2;
    const auto iconSpace = icon >= 0 ? iconSize + paddingIn : 0;
    const std::string txt = text1.length() > text2.length() ? text1 : text2;
    const auto width = text->lenght(txt) + (paddingIn * 2) + iconSpace;
    const auto height = (text->getCharacterSize() * numTexts) + (paddingIn * 2);
    const auto shape = NotificationShape::SQUARED;

    // Posición horizontal
    auto despH = 0;
    if (options.notification.pos_h == NotifyPosition::LEFT)
    {
        despH = paddingOut;
    }
    else if (options.notification.pos_h == NotifyPosition::MIDDLE)
    {
        despH = ((param.game.width / 2) - (width / 2));
    }
    else
    {
        despH = param.game.width - width - paddingOut;
    }

    // Posición vertical
    const int despV = (options.notification.pos_v == NotifyPosition::TOP) ? paddingOut : (param.game.height - height - paddingOut);

    // Offset
    const auto travelDist = height + paddingOut;
    auto offset = 0;
    if (options.notification.pos_v == NotifyPosition::TOP)
    {
        offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV;
    }
    else
    {
        offset = (int)notifications.size() > 0 ? notifications.back().y - travelDist : despV;
    }

    // Crea la notificacion
    Notification n;

    // Inicializa variables
    n.y = offset;
    n.travelDist = travelDist;
    n.counter = 0;
    n.status = NotificationStatus::RISING;
    n.text1 = text1;
    n.text2 = text2;
    n.shape = shape;
    auto yPos = offset + (options.notification.pos_v == NotifyPosition::TOP ? -travelDist : travelDist);
    n.rect = {despH, yPos, width, 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, bgColor.r, bgColor.g, bgColor.b, 255);
    SDL_Rect rect;
    if (shape == NotificationShape::ROUNDED)
    {
        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);
    }

    else if (shape == NotificationShape::SQUARED)
    {
        SDL_RenderClear(renderer);
    }

    // Dibuja el icono de la notificación
    if (hasIcons && icon >= 0 && numTexts == 2)
    {
        auto sp = std::make_unique<Sprite>((SDL_Rect){0, 0, iconSize, iconSize}, iconTexture);
        sp->setPos({paddingIn, paddingIn, iconSize, iconSize});
        sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
        sp->render();
    }

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

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

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

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

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

// Indica si hay notificaciones activas
bool Notify::active()
{
    if ((int)notifications.size() > 0)
    {
        return true;
    }

    return false;
}

// Finaliza y elimnina todas las notificaciones activas
void Notify::clearNotifications()
{
    for (int i = 0; i < (int)notifications.size(); ++i)
    {
        notifications[i].status = NotificationStatus::FINISHED;
    }

    clearFinishedNotifications();
}