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

#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/variant/dictionary.hpp>
#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/packed_vector3_array.hpp>
#include <godot_cpp/variant/packed_vector2_array.hpp>
#include <godot_cpp/variant/packed_int32_array.hpp>
#include <godot_cpp/variant/packed_color_array.hpp>
#include <godot_cpp/variant/packed_byte_array.hpp>

#include "wow_chunk_reader.h"

namespace godot {

// ─────────────────────────────────────────────────────────────────────────────
// WMOLoader
//
// Loads WoW 3.3.5a WMO files (root + group files).
//
// Usage (GDScript):
//   var wmo = WMOLoader.new()
//   var data = wmo.load_wmo("C:/wow/Data/World/wmo/Azeroth/Buildings/StormwindHouse/StormwindHouse.wmo")
//
// Return value is a Dictionary:
//   {
//     "textures": PackedStringArray,   # texture filenames referenced by materials
//     "materials": Array[Dictionary],  # material definitions
//     "groups": Array[Dictionary],     # one entry per group file
//   }
//
// Each group Dictionary:
//   {
//     "name":     String,
//     "vertices": PackedVector3Array,
//     "normals":  PackedVector3Array,
//     "uvs":      PackedVector2Array,
//     "colors":   PackedColorArray,    # may be empty
//     "indices":  PackedInt32Array,
//     "batches":  Array[Dictionary],   # render batches
//   }
//
// Each batch Dictionary:
//   {
//     "index_start":  int,
//     "index_count":  int,
//     "vertex_start": int,
//     "vertex_count": int,
//     "material_id":  int,
//   }
//
// Each material Dictionary:
//   {
//     "texture0":     int,   # index into textures array (-1 = none)
//     "texture1":     int,
//     "texture2":     int,
//     "blend_mode":   int,   # 0=opaque 1=alpha key 2=alpha blend
//     "flags":        int,
//     "flags2":       int,
//     "diffuse_color":  Color,
//     "emissive_color": Color,
//   }
// ─────────────────────────────────────────────────────────────────────────────
class WMOLoader : public RefCounted {
    GDCLASS(WMOLoader, RefCounted)

public:
    // Load root WMO + all group files from the same directory.
    // path must be an absolute OS path.
    Dictionary load_wmo(const String &path);

    // Load only the root file (no groups).
    Dictionary load_root(const String &path);

    // Load a single group file.
    Dictionary load_group(const String &path);

    // Load from raw bytes (e.g. from MPQManager::read_file).
    // group_bytes: Array of PackedByteArray, one per group file.
    Dictionary load_wmo_from_bytes(const PackedByteArray &root_bytes,
                                   const Array &group_bytes_array);

protected:
    static void _bind_methods();

private:
    Dictionary parse_root(const std::vector<uint8_t> &buf);
    Dictionary parse_group(const std::vector<uint8_t> &buf);

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

} // namespace godot
