#include "resource.h"
#include <SDL3/SDL_log.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError
#include <SDL3/SDL.h>
#include <algorithm> // Para find_if
#include <array>
#include <stdexcept>    // Para runtime_error
#include "asset.h"      // Para Asset, AssetType
#include "external/jail_audio.h" // Para JA_DeleteMusic, JA_DeleteSound, JA_LoadMusic
#include "lang.h"       // Para getText
#include "screen.h"     // Para Screen
#include "text.h"       // Para Text, loadTextFile
struct JA_Music_t;      // lines 11-11
struct JA_Sound_t;      // lines 12-12

// Singleton
Resource *Resource::instance_ = nullptr;

// Inicializa la instancia única del singleton
void Resource::init() { Resource::instance_ = new Resource(); }

// Libera la instancia
void Resource::destroy() { delete Resource::instance_; }

// Obtiene la instancia
Resource *Resource::get() { return Resource::instance_; }

// Constructor
Resource::Resource() : loading_text_(Screen::get()->getText()) { load(); }

// Vacia todos los vectores de recursos
void Resource::clear()
{
    clearSounds();
    clearMusics();
    textures_.clear();
    text_files_.clear();
    texts_.clear();
    animations_.clear();
    demos_.clear();
}

// Carga todos los recursos del juego y muestra el progreso de carga
void Resource::load()
{
    // Prepara la gestión del progreso de carga
    calculateTotalResources();
    initProgressBar();

    // Muerstra la ventana y desactiva el sincronismo vertical
    auto screen = Screen::get();
    auto vsync = screen->getVSync();
    screen->setVSync(false);

    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** LOADING RESOURCES");
    loadSounds();     // Carga sonidos
    loadMusics();     // Carga músicas
    loadTextures();   // Carga texturas
    loadTextFiles();  // Carga ficheros de texto
    loadAnimations(); // Carga animaciones
    loadDemoData();   // Carga datos de demo
    addPalettes();    // Añade paletas a las texturas
    createText();     // Crea objetos de texto
    createTextures(); // Crea texturas a partir de texto
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** RESOURCES LOADED");

    // Restablece el sincronismo vertical a su valor original
    screen->setVSync(vsync);
}

// Recarga todos los recursos (limpia y vuelve a cargar)
void Resource::reload()
{
    clear();
    load();
}

// Recarga solo las texturas y paletas
void Resource::reloadTextures()
{
    loadTextures();
    addPalettes();
    createTextures();
}

// Obtiene el sonido a partir de un nombre. Lanza excepción si no existe.
JA_Sound_t *Resource::getSound(const std::string &name)
{
    auto it = std::find_if(sounds_.begin(), sounds_.end(), [&name](const auto &s)
                           { return s.name == name; });

    if (it != sounds_.end())
    {
        return it->sound;
    }

    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Sonido no encontrado %s", name.c_str());
    throw std::runtime_error("Sonido no encontrado: " + name);
}

// Obtiene la música a partir de un nombre. Lanza excepción si no existe.
JA_Music_t *Resource::getMusic(const std::string &name)
{
    auto it = std::find_if(musics_.begin(), musics_.end(), [&name](const auto &m)
                           { return m.name == name; });

    if (it != musics_.end())
    {
        return it->music;
    }

    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Música no encontrada %s", name.c_str());
    throw std::runtime_error("Música no encontrada: " + name);
}

// Obtiene la textura a partir de un nombre. Lanza excepción si no existe.
std::shared_ptr<Texture> Resource::getTexture(const std::string &name)
{
    auto it = std::find_if(textures_.begin(), textures_.end(), [&name](const auto &t)
                           { return t.name == name; });

    if (it != textures_.end())
    {
        return it->texture;
    }

    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Imagen no encontrada %s", name.c_str());
    throw std::runtime_error("Imagen no encontrada: " + name);
}

// Obtiene el fichero de texto a partir de un nombre. Lanza excepción si no existe.
std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
{
    auto it = std::find_if(text_files_.begin(), text_files_.end(), [&name](const auto &t)
                           { return t.name == name; });

    if (it != text_files_.end())
    {
        return it->text_file;
    }

    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: TextFile no encontrado %s", name.c_str());
    throw std::runtime_error("TextFile no encontrado: " + name);
}

// Obtiene el objeto de texto a partir de un nombre. Lanza excepción si no existe.
std::shared_ptr<Text> Resource::getText(const std::string &name)
{
    auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto &t)
                           { return t.name == name; });

    if (it != texts_.end())
    {
        return it->text;
    }

    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Text no encontrado %s", name.c_str());
    throw std::runtime_error("Text no encontrado: " + name);
}

// Obtiene la animación a partir de un nombre. Lanza excepción si no existe.
AnimationsFileBuffer &Resource::getAnimation(const std::string &name)
{
    auto it = std::find_if(animations_.begin(), animations_.end(), [&name](const auto &a)
                           { return a.name == name; });

    if (it != animations_.end())
    {
        return it->animation;
    }

    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Animación no encontrada %s", name.c_str());
    throw std::runtime_error("Animación no encontrada: " + name);
}

// Obtiene el fichero con los datos para el modo demostración a partir de un índice
DemoData &Resource::getDemoData(int index)
{
    return demos_.at(index);
}

// Carga los sonidos del juego
void Resource::loadSounds()
{
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> SOUND FILES");
    auto list = Asset::get()->getListByType(AssetType::SOUND);
    sounds_.clear();

    for (const auto &l : list)
    {
        auto name = getFileName(l);
        sounds_.emplace_back(Resource::ResourceSound(name, JA_LoadSound(l.c_str())));
        printWithDots("Sound : ", name, "[ LOADED ]");
        updateLoadingProgress(name);
    }
}

// Carga las músicas del juego
void Resource::loadMusics()
{
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> MUSIC FILES");
    auto list = Asset::get()->getListByType(AssetType::MUSIC);
    musics_.clear();

    for (const auto &l : list)
    {
        auto name = getFileName(l);
        musics_.emplace_back(Resource::ResourceMusic(name, JA_LoadMusic(l.c_str())));
        printWithDots("Music : ", name, "[ LOADED ]");
        updateLoadingProgress(name);
    }
}

// Carga las texturas del juego
void Resource::loadTextures()
{
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXTURES");
    auto list = Asset::get()->getListByType(AssetType::BITMAP);
    textures_.clear();

    for (const auto &l : list)
    {
        auto name = getFileName(l);
        textures_.emplace_back(Resource::ResourceTexture(name, std::make_shared<Texture>(Screen::get()->getRenderer(), l)));
        updateLoadingProgress(name);
    }
}

// Carga los ficheros de texto del juego
void Resource::loadTextFiles()
{
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES");
    auto list = Asset::get()->getListByType(AssetType::FONT);
    text_files_.clear();

    for (const auto &l : list)
    {
        auto name = getFileName(l);
        text_files_.emplace_back(Resource::ResourceTextFile(name, loadTextFile(l)));
        updateLoadingProgress(name);
    }
}

// Carga las animaciones del juego
void Resource::loadAnimations()
{
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> ANIMATIONS");
    auto list = Asset::get()->getListByType(AssetType::ANIMATION);
    animations_.clear();

    for (const auto &l : list)
    {
        auto name = getFileName(l);
        animations_.emplace_back(Resource::ResourceAnimation(name, loadAnimationsFromFile(l)));
        updateLoadingProgress(name);
    }
}

// Carga los datos para el modo demostración
void Resource::loadDemoData()
{
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES");

    constexpr std::array<const char *, 2> demo_files = {"demo1.bin", "demo2.bin"};

    for (const auto &file : demo_files)
    {
        demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get(file)));
        updateLoadingProgress(file);
    }
}

// Añade paletas de colores a las texturas principales
void Resource::addPalettes()
{
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> PALETTES");

    // Paletas para el jugador 1
    getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_1_coffee_palette.gif"));
    getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_2_coffee_palette.gif"));
    getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_invencible_palette.gif"));

    // Paletas para el jugador 2
    getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_1_coffee_palette.gif"));
    getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_2_coffee_palette.gif"));
    getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_invencible_palette.gif"));

    // Paleta para la fuente SMB2
    getTexture("smb2.gif")->addPaletteFromFile(Asset::get()->get("smb2_palette1.pal"));
}

// Crea texturas a partir de textos para mostrar puntuaciones y mensajes
void Resource::createTextures()
{
    struct NameAndText
    {
        std::string name;
        std::string text;

        NameAndText(const std::string &name_init, const std::string &text_init)
            : name(name_init), text(text_init) {}
    };

    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING TEXTURES");

    // Texturas de tamaño normal
    std::vector<NameAndText> strings = {
        {"game_text_1000_points", "1.000"},
        {"game_text_2500_points", "2.500"},
        {"game_text_5000_points", "5.000"},
        {"game_text_powerup", Lang::getText("[GAME_TEXT] 4")},
        {"game_text_one_hit", Lang::getText("[GAME_TEXT] 5")},
        {"game_text_stop", Lang::getText("[GAME_TEXT] 6")},
        {"game_text_1000000_points", Lang::getText("[GAME_TEXT] 8")}};

    auto text = getText("04b_25");
    for (const auto &s : strings)
    {
        textures_.emplace_back(Resource::ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2)));
        printWithDots("Texture : ", s.name, "[  DONE  ]");
    }

    // Texturas de tamaño doble
    std::vector<NameAndText> strings2X = {
        {"game_text_100000_points", "100.000"},
        {"game_text_get_ready", Lang::getText("[GAME_TEXT] 7")},
        {"game_text_last_stage", Lang::getText("[GAME_TEXT] 3")},
        {"game_text_congratulations", Lang::getText("[GAME_TEXT] 1")},
        {"game_text_game_over", "Game Over"}};

    auto text2 = getText("04b_25_2x");
    for (const auto &s : strings2X)
    {
        textures_.emplace_back(Resource::ResourceTexture(s.name, text2->writeToTexture(s.text, 1, -4)));
        printWithDots("Texture : ", s.name, "[  DONE  ]");
    }
}

// Crea los objetos de texto a partir de los archivos de textura y texto
void Resource::createText()
{
    struct ResourceInfo
    {
        std::string key;
        std::string textureFile;
        std::string textFile;

        ResourceInfo(const std::string &k, const std::string &tFile, const std::string &txtFile)
            : key(k), textureFile(tFile), textFile(txtFile) {}
    };

    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> CREATING TEXT OBJECTS");

    std::vector<ResourceInfo> resources = {
        {"04b_25", "04b_25.png", "04b_25.txt"},
        {"04b_25_2x", "04b_25_2x.png", "04b_25_2x.txt"},
        {"04b_25_metal", "04b_25_metal.png", "04b_25.txt"},
        {"04b_25_grey", "04b_25_grey.png", "04b_25.txt"},
        {"04b_25_flat", "04b_25_flat.png", "04b_25.txt"},
        {"04b_25_reversed", "04b_25_reversed.png", "04b_25.txt"},
        {"04b_25_flat_2x", "04b_25_flat_2x.png", "04b_25_2x.txt"},
        {"04b_25_reversed_2x", "04b_25_reversed_2x.png", "04b_25_2x.txt"},
        {"8bithud", "8bithud.png", "8bithud.txt"},
        {"aseprite", "aseprite.png", "aseprite.txt"},
        {"smb2", "smb2.gif", "smb2.txt"}};

    for (const auto &resource : resources)
    {
        texts_.emplace_back(Resource::ResourceText(resource.key, std::make_shared<Text>(
                                                                     getTexture(resource.textureFile),
                                                                     getTextFile(resource.textFile))));
        printWithDots("Text : ", resource.key, "[  DONE  ]");
    }
}

// Vacía el vector de sonidos y libera la memoria asociada
void Resource::clearSounds()
{
    for (auto &sound : sounds_)
    {
        if (sound.sound)
        {
            JA_DeleteSound(sound.sound);
            sound.sound = nullptr;
        }
    }
    sounds_.clear();
}

// Vacía el vector de músicas y libera la memoria asociada
void Resource::clearMusics()
{
    for (auto &music : musics_)
    {
        if (music.music)
        {
            JA_DeleteMusic(music.music);
            music.music = nullptr;
        }
    }
    musics_.clear();
}

// Calcula el número total de recursos a cargar y reinicia el contador de carga
void Resource::calculateTotalResources()
{
    const std::array<AssetType, 6> ASSET_TYPES = {
        AssetType::SOUND,
        AssetType::MUSIC,
        AssetType::BITMAP,
        AssetType::FONT,
        AssetType::ANIMATION,
        AssetType::DEMODATA};

    size_t total = 0;
    for (const auto &asset_type : ASSET_TYPES)
    {
        auto list = Asset::get()->getListByType(asset_type);
        total += list.size();
    }

    loading_count_ = ResourceCount(total);
}

// Muestra el progreso de carga en pantalla (barra y texto)
void Resource::renderProgress()
{
    // Obtiene la pantalla y el renderer
    auto screen = Screen::get();
    auto renderer = screen->getRenderer();

    // Actualiza la lógica principal de la pantalla (input, etc.)
    screen->coreUpdate();

    // Inicia el frame y limpia la pantalla
    screen->start();
    screen->clean();

    // Establece el color de dibujo a blanco
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    // Dibuja la barra de progreso (marco y barra llena)
    SDL_RenderRect(renderer, &loading_wired_rect_);
    SDL_RenderFillRect(renderer, &loading_full_rect_);

    // Escribe el texto de carga encima de la barra
    loading_text_->write(
        loading_wired_rect_.x,
        loading_wired_rect_.y - 9,
        Lang::getText("[RESOURCE] LOADING") + " : " + loading_resource_name_
    );

    // Renderiza el frame en pantalla
    screen->coreRender();
}

// Comprueba los eventos durante la carga (permite salir con ESC o cerrar ventana)
void Resource::checkEvents()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
        case SDL_EVENT_QUIT:
            exit(0);
            break;
        case SDL_EVENT_KEY_DOWN:
            if (event.key.key == SDLK_ESCAPE)
            {
                exit(0);
            }
            break;
        }
    }
}

// Actualiza el progreso de carga, muestra la barra y procesa eventos
void Resource::updateLoadingProgress(std::string name)
{
    loading_resource_name_ = name;
    loading_count_.increase();
    updateProgressBar();
    renderProgress();
    checkEvents();
}

// Inicializa los rectangulos que definen la barra de progreso
void Resource::initProgressBar()
{
    constexpr float X_PADDING = 20.0f;
    constexpr float Y_PADDING = 20.0f;
    constexpr float BAR_HEIGHT = 10.0f;
    const float BAR_Y_POSITION = param.game.height - BAR_HEIGHT - Y_PADDING;

    const float WIRED_BAR_WIDTH = param.game.width - (X_PADDING * 2);
    loading_wired_rect_ = {X_PADDING, BAR_Y_POSITION, WIRED_BAR_WIDTH, BAR_HEIGHT};

    const float FULL_BAR_WIDTH = WIRED_BAR_WIDTH * loading_count_.getPercentage();
    loading_full_rect_ = {X_PADDING, BAR_Y_POSITION, FULL_BAR_WIDTH, BAR_HEIGHT};
}

// Actualiza la barra de estado
void Resource::updateProgressBar()
{
    loading_full_rect_.w = loading_wired_rect_.w * loading_count_.getPercentage();
}