#include "resource.h"
#include <iostream>          // for basic_ostream, operator<<, cout, endl
#include "animatedsprite.h"  // for animatedSprite_t, loadAnimationFromFile
#include "asset.h"           // for Asset
#include "enemy.h"           // for enemy_t
#include "item.h"            // for item_t
#include "room.h"            // for room_t, loadRoomFile, loadRoomTileFile
#include "text.h"            // for textFile_t, LoadTextFile
#include "texture.h"         // for Texture
#include "utils.h"           // for options_t

// Constructor
Resource::Resource(SDL_Renderer *renderer, Asset *asset, options_t *options)
{
    this->renderer = renderer;
    this->asset = asset;
    this->options = options;
}

// Carga las texturas de una lista
void Resource::loadTextures(std::vector<std::string> list)
{
    for (auto l : list)
    {
        if (options->console)
        {
            std::cout << "\nLOAD TEXTURE: " << l << std::endl;
            std::cout << "png: " << asset->get(l) << std::endl;
        }

        res_texture_t t;
        t.name = l;
        t.texture = new Texture(renderer, asset->get(t.name), options->console);
        textures.push_back(t);
    }
}

// Vuelve a cargar las texturas
void Resource::reLoadTextures()
{
    for (auto texture : textures)
    {
        texture.texture->reLoad();
    }
}

// Carga las animaciones desde una lista
void Resource::loadAnimations(std::vector<std::string> list)
{
    for (auto l : list)
    {
        // Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura
        const std::string pngFile = l.substr(0, l.find_last_of(".")) + ".png";

        if (options->console)
        {
            std::cout << "\nLOAD ANIMATION: " << l << std::endl;
            std::cout << "png: " << asset->get(pngFile) << std::endl;
            std::cout << "ani: " << asset->get(l) << std::endl;
        }

        res_animation_t as;
        as.name = l;
        as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(as.name), options->console));
        animations.push_back(as);
    }
}

// Vuelve a cargar las animaciones
void Resource::reLoadAnimations()
{
    // reLoadTextures();

    for (auto &a : animations)
    {
        // Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura
        const std::string pngFile = a.name.substr(0, a.name.find_last_of(".")) + ".png";
        delete a.animation;
        a.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(a.name), options->console));
    }
}

// Carga los offsets desde una lista
void Resource::loadOffsets(std::vector<std::string> list)
{
    for (auto l : list)
    {
        res_textOffset_t to;
        to.name = l;
        to.textFile = new textFile_t(LoadTextFile(asset->get(l), options->console));
        offsets.push_back(to);
    }
}

// Vuelve a cargar los offsets
void Resource::reLoadOffsets()
{
    for (auto &o : offsets)
    {
        delete o.textFile;
        o.textFile = new textFile_t(LoadTextFile(asset->get(o.name), options->console));
    }
}

// Carga los mapas de tiles desde una lista
void Resource::loadTileMaps(std::vector<std::string> list)
{
    for (auto l : list)
    {
        res_tileMap_t tm;
        tm.name = l;
        tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(l), options->console));
        tileMaps.push_back(tm);
    }
}

// Vuelve a cargar los mapas de tiles
void Resource::reLoadTileMaps()
{
    for (auto &tm : tileMaps)
    {
        delete tm.tileMap;
        tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(tm.name), options->console));
    }
}

// Carga las habitaciones desde una lista
void Resource::loadRooms(std::vector<std::string> list)
{
    for (auto l : list)
    {
        res_room_t r;
        r.name = l;
        r.room = new room_t(loadRoomFile(asset->get(l), options->console));
        r.room->tileMap = getTileMap(r.room->tileMapFile);
        for (auto &e : r.room->enemies)
        {
            e.animation = getAnimation(e.animationString);
        }
        for (auto &i : r.room->items)
        {
            i.texture = getTexture(i.tileSetFile);
        }
        r.room->textureA = getTexture("standard.png");
        r.room->textureB = getTexture("standard_zxarne.png");
        rooms.push_back(r);
    }
}

// Vuelve a cargar las habitaciones
void Resource::reLoadRooms()
{
    reLoadTileMaps();

    for (auto &r : rooms)
    {
        delete r.room;
        r.room = new room_t(loadRoomFile(asset->get(r.name)));
        r.room->tileMap = getTileMap(r.room->tileMapFile);
        for (auto &e : r.room->enemies)
        {
            e.animation = getAnimation(e.animationString);
        }
        for (auto &i : r.room->items)
        {
            i.texture = getTexture(i.tileSetFile);
        }
        r.room->textureA = getTexture("standard.png");
        r.room->textureB = getTexture("standard_zxarne.png");
    }
}

// Vuelve a cargar todos los recursos
void Resource::reLoad()
{
    reLoadAnimations();
    reLoadOffsets();
    reLoadRooms();
}

// Libera las texturas
void Resource::freeTextures()
{
    for (auto texture : textures)
    {
        delete texture.texture;
    }
    textures.clear();
}

// Libera las animaciones
void Resource::freeAnimations()
{
    for (auto a : animations)
    {
        delete a.animation;
    }
    animations.clear();
}

// Libera los offsets
void Resource::freeOffsets()
{
    for (auto o : offsets)
    {
        delete o.textFile;
    }
    offsets.clear();
}

// Libera los mapas de tiles
void Resource::freeTileMaps()
{
    for (auto t : tileMaps)
    {
        delete t.tileMap;
    }
    tileMaps.clear();
}

// Libera las habitaciones
void Resource::freeRooms()
{
    for (auto r : rooms)
    {
        delete r.room;
    }
    rooms.clear();
}

// Libera todos los recursos
void Resource::free()
{
    freeTextures();
    freeAnimations();
    freeOffsets();
    freeTileMaps();
    freeRooms();
}

// Obtiene una textura
Texture *Resource::getTexture(std::string name)
{
    for (auto texture : textures)
    {
        // if (texture.name.find(name) != std::string::npos)
        if (texture.name == name)
        {
            // std::cout << "\nTEXTURE REQUESTED: " << name << std::endl;
            // std::cout << "served: " << texture.name << std::endl;

            return texture.texture;
        }
    }

    if (options->console)
    {
        std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
    }
    return nullptr;
}

// Obtiene una animación
animatedSprite_t *Resource::getAnimation(std::string name)
{
    for (auto animation : animations)
    {
        // if (animation.name.find(name) != std::string::npos)
        if (animation.name == name)
        {
            // std::cout << "\nANIMATION REQUESTED: " << name << std::endl;
            // std::cout << "served: " << animation.name << std::endl;
            return animation.animation;
        }
    }

    if (options->console)
    {
        std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
    }
    return nullptr;
}

// Obtiene un offset
textFile_t *Resource::getOffset(std::string name)
{
    for (auto offset : offsets)
    {
        // if (offset.name.find(name) != std::string::npos)
        if (offset.name == name)
        {
            return offset.textFile;
        }
    }

    if (options->console)
    {
        std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
    }
    return nullptr;
}

// Obtiene un mapa de tiles
std::vector<int> *Resource::getTileMap(std::string name)
{
    for (auto tileMap : tileMaps)
    {
        // if (tileMap.name.find(name) != std::string::npos)
        if (tileMap.name == name)
        {
            return tileMap.tileMap;
        }
    }

    if (options->console)
    {
        std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
    }
    return nullptr;
}

// Obtiene una habitacion
room_t *Resource::getRoom(std::string name)
{
    for (auto room : rooms)
    {
        // if (room.name.find(name) != std::string::npos)
        if (room.name == name)
        {
            return room.room;
        }
    }

    if (options->console)
    {
        std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
    }
    return nullptr;
}

// Obtiene todas las habitaciones
std::vector<res_room_t> *Resource::getAllRooms()
{
    return &rooms;
}