
#include "core/rendering/texture.h"

#include <SDL3/SDL.h>

#include <cstdlib>   // for exit
#include <iostream>  // for basic_ostream, operator<<, cout, endl
#define STB_IMAGE_IMPLEMENTATION
#include "core/resources/resource_helper.h"  // for loadFile (pack + filesystem fallback)
#include "external/stb_image.h"              // for stbi_failure_reason, stbi_image_free

SDL_ScaleMode Texture::current_scale_mode = SDL_SCALEMODE_NEAREST;

void Texture::setGlobalScaleMode(SDL_ScaleMode mode) {
    current_scale_mode = mode;
}

// Constructor
Texture::Texture(SDL_Renderer *renderer, const std::string &path, bool verbose)
    : texture_(nullptr),
      renderer_(renderer),
      width_(0),
      height_(0),
      path_(path) {
    // Carga el fichero en la textura
    if (!path.empty()) {
        loadFromFile(path, renderer, verbose);
    }
}

// Constructor desde bytes
Texture::Texture(SDL_Renderer *renderer, const std::vector<uint8_t> &bytes, bool verbose)
    : texture_(nullptr),
      renderer_(renderer),
      width_(0),
      height_(0) {
    if (!bytes.empty()) {
        loadFromMemory(bytes.data(), bytes.size(), renderer, verbose);
    }
}

// Destructor
Texture::~Texture() {
    // Libera memoria
    unload();
}

// Helper: convierte píxeles RGBA decodificados por stbi en SDL_Texture
static auto createTextureFromPixels(SDL_Renderer *renderer, unsigned char *data, int w, int h, int *out_w, int *out_h) -> SDL_Texture * {
    const int PITCH = 4 * w;
    SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, static_cast<void *>(data), PITCH);
    if (loaded_surface == nullptr) {
        return nullptr;
    }
    SDL_Texture *new_texture = SDL_CreateTextureFromSurface(renderer, loaded_surface);
    if (new_texture != nullptr) {
        *out_w = loaded_surface->w;
        *out_h = loaded_surface->h;
        SDL_SetTextureScaleMode(new_texture, Texture::current_scale_mode);
    }
    SDL_DestroySurface(loaded_surface);
    return new_texture;
}

// Carga una imagen desde un fichero (vía ResourceHelper: pack si està inicialitzat, filesystem si no)
auto Texture::loadFromFile(const std::string &path, SDL_Renderer *renderer, bool verbose) -> bool {
    const std::string FILE_NAME = path.substr(path.find_last_of("\\/") + 1);

    auto bytes = ResourceHelper::loadFile(path);
    if (bytes.empty()) {
        SDL_Log("Loading image failed: can't open %s", path.c_str());
        exit(1);
    }

    int req_format = STBI_rgb_alpha;
    int w;
    int h;
    int orig_format;
    unsigned char *data = stbi_load_from_memory(bytes.data(), static_cast<int>(bytes.size()), &w, &h, &orig_format, req_format);
    if (data == nullptr) {
        SDL_Log("Loading image failed: %s", stbi_failure_reason());
        exit(1);
    } else if (verbose) {
        std::cout << "Image loaded: " << FILE_NAME.c_str() << '\n';
    }

    unload();
    SDL_Texture *new_texture = createTextureFromPixels(renderer, data, w, h, &this->width_, &this->height_);
    if (new_texture == nullptr && verbose) {
        std::cout << "Unable to load image " << path.c_str() << '\n';
    }

    stbi_image_free(data);
    texture_ = new_texture;
    return texture_ != nullptr;
}

// Carga una imagen desde bytes en memoria
auto Texture::loadFromMemory(const uint8_t *data, size_t size, SDL_Renderer *renderer, bool verbose) -> bool {
    int w;
    int h;
    int orig_format;
    unsigned char *pixels = stbi_load_from_memory(data, (int)size, &w, &h, &orig_format, STBI_rgb_alpha);
    if (pixels == nullptr) {
        SDL_Log("Loading image from memory failed: %s", stbi_failure_reason());
        return false;
    }

    unload();
    SDL_Texture *new_texture = createTextureFromPixels(renderer, pixels, w, h, &this->width_, &this->height_);
    if (new_texture == nullptr && verbose) {
        std::cout << "Unable to create texture from memory" << '\n';
    }

    stbi_image_free(pixels);
    texture_ = new_texture;
    return texture_ != nullptr;
}

// Crea una textura en blanco
auto Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access) -> bool {
    // Crea una textura sin inicializar
    texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
    if (texture_ == nullptr) {
        std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << '\n';
    } else {
        this->width_ = width;
        this->height_ = height;
        SDL_SetTextureScaleMode(texture_, current_scale_mode);
    }

    return texture_ != nullptr;
}

// Libera la memoria de la textura
void Texture::unload() {
    // Libera la textura si existe
    if (texture_ != nullptr) {
        SDL_DestroyTexture(texture_);
        texture_ = nullptr;
        width_ = 0;
        height_ = 0;
    }
}

// Establece el color para la modulacion
void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue) {
    SDL_SetTextureColorMod(texture_, red, green, blue);
}

// Establece el blending
void Texture::setBlendMode(SDL_BlendMode blending) {
    SDL_SetTextureBlendMode(texture_, blending);
}

// Establece el alpha para la modulación
void Texture::setAlpha(Uint8 alpha) {
    SDL_SetTextureAlphaMod(texture_, alpha);
}

// Renderiza la textura en un punto específico
void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoom_w, float zoom_h, double angle, SDL_Point *center, SDL_FlipMode flip) {
    // Establece el destino de renderizado en la pantalla
    SDL_FRect render_quad = {(float)x, (float)y, (float)width_, (float)height_};

    // Obtiene las dimesiones del clip de renderizado
    if (clip != nullptr) {
        render_quad.w = (float)clip->w;
        render_quad.h = (float)clip->h;
    }

    render_quad.w = render_quad.w * zoom_w;
    render_quad.h = render_quad.h * zoom_h;

    // Convierte el clip a SDL_FRect
    SDL_FRect src_rect;
    SDL_FRect *src_rect_ptr = nullptr;
    if (clip != nullptr) {
        src_rect = {.x = (float)clip->x, .y = (float)clip->y, .w = (float)clip->w, .h = (float)clip->h};
        src_rect_ptr = &src_rect;
    }

    // Convierte el centro a SDL_FPoint
    SDL_FPoint f_center;
    SDL_FPoint *f_center_ptr = nullptr;
    if (center != nullptr) {
        f_center = {.x = (float)center->x, .y = (float)center->y};
        f_center_ptr = &f_center;
    }

    // Renderiza a pantalla
    SDL_RenderTextureRotated(renderer, texture_, src_rect_ptr, &render_quad, angle, f_center_ptr, flip);
}

// Establece la textura como objetivo de renderizado
void Texture::setAsRenderTarget(SDL_Renderer *renderer) {
    SDL_SetRenderTarget(renderer, texture_);
}

// Obtiene el ancho de la imagen
auto Texture::getWidth() const -> int {
    return width_;
}

// Obtiene el alto de la imagen
auto Texture::getHeight() const -> int {
    return height_;
}

// Recarga la textura
auto Texture::reLoad() -> bool {
    return loadFromFile(path_, renderer_);
}

// Obtiene la textura
auto Texture::getSDLTexture() -> SDL_Texture * {
    return texture_;
}