#pragma once
#include <vector>
#include <string>
#include <cstdint>

#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/classes/image.hpp>
#include <godot_cpp/classes/image_texture.hpp>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/packed_byte_array.hpp>

namespace godot {

// ─────────────────────────────────────────────────────────────────────────────
// BLPLoader  —  WoW BLP2 texture → Godot Image
//
// Supports all formats used by WoW 3.3.5a:
//   • DXT1 / DXT3 / DXT5  (encoding=2, most common)
//   • Palettized BGRA      (encoding=1)
//   • Uncompressed BGRA    (encoding=3, rare)
//
// Usage (GDScript):
//   var img = BLPLoader.new().load_image("C:/path/texture.blp")
//   var tex = ImageTexture.create_from_image(img)
// ─────────────────────────────────────────────────────────────────────────────
class BLPLoader : public RefCounted {
    GDCLASS(BLPLoader, RefCounted)

public:
    // Load from absolute filesystem path
    Ref<Image> load_image(const String &path);

    // Load from raw bytes (e.g. from MPQManager::read_file)
    Ref<Image> load_image_from_bytes(const PackedByteArray &bytes);

protected:
    static void _bind_methods();

private:
    static Ref<Image> parse(const uint8_t *data, size_t len);

    // DXT decoders (output RGBA8)
    static void decode_dxt1(const uint8_t *src, int w, int h,
                            std::vector<uint8_t> &dst, bool has_alpha);
    static void decode_dxt3(const uint8_t *src, int w, int h,
                            std::vector<uint8_t> &dst);
    static void decode_dxt5(const uint8_t *src, int w, int h,
                            std::vector<uint8_t> &dst);

    static std::vector<uint8_t> read_file(const std::string &path);
    static std::string          to_std(const String &s);
};

} // namespace godot
