#include "Texture.h"
#include "AssetManager.h"
#include "Logging.h"

Texture::Texture(const std::string& path)
{
    if (!path.empty())
    {
        AssetManager::LoadAssetAsync(path, AssetType::Image);
        const auto* asset = AssetManager::GetAssetByPath(path);
        if (asset && asset->loaded)
        {
            if (const auto* imageAsset = dynamic_cast<const ImageAssetInfo*>(asset))
            {
                id = imageAsset->textureID;
                size = imageAsset->size;
            }
            else
            {
                Logger::LogError("[Texture] Asset at path '%s' is not an image!", path.c_str());
            }
        }
        else
        {
            Logger::LogError("[Texture] Failed to get asset for path: %s", path.c_str());
        }
    }
}

Texture::Texture(uint64_t uaid)
{
    const auto* asset = AssetManager::GetAssetByID(uaid);
    if (asset && asset->loaded)
    {
        if (const auto* imageAsset = dynamic_cast<const ImageAssetInfo*>(asset))
        {
            id = imageAsset->textureID;
            size = imageAsset->size;
        }
        else
        {
            Logger::LogError("[Texture] Asset with UAID '%llu' is not an image!", uaid);
        }
    }
    else
    {
        Logger::LogError("[Texture] Failed to get asset for UAID: %llu", uaid);
    }
}

Texture::~Texture() {}

unsigned int Texture::GetID() const
{
    return id;
}
