#include "core/resources/resource_pack.hpp"

#include <algorithm>
#include <array>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <numeric>

const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "AEE_RESOURCES__2026";

namespace {
    constexpr const char* MAGIC = "AEE1";
    constexpr uint32_t VERSION = 1;
}  // namespace

ResourcePack::ResourcePack() = default;

ResourcePack::~ResourcePack() {
    clear();
}

auto ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
    // djb2-like hash, seed 0x12345678 (idèntic a CCAE).
    return std::accumulate(data.begin(), data.end(), uint32_t{0x12345678}, [](uint32_t acc, unsigned char b) { return ((acc << 5) + acc) + b; });
}

void ResourcePack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
    if (key.empty()) {
        return;
    }
    for (size_t i = 0; i < data.size(); ++i) {
        data[i] ^= static_cast<uint8_t>(key[i % key.length()]);
    }
}

void ResourcePack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
    encryptData(data, key);  // XOR és simètric
}

auto ResourcePack::loadPack(const std::string& pack_file) -> bool {
    std::ifstream file(pack_file, std::ios::binary);
    if (!file) {
        return false;  // No imprimim error: el caller decideix si cal fallback
    }

    std::array<char, 4> header{};
    file.read(header.data(), 4);
    if (std::string(header.data(), 4) != MAGIC) {
        std::cerr << "ResourcePack: invalid pack file format (bad magic): " << pack_file << '\n';
        return false;
    }

    uint32_t version = 0;
    file.read(reinterpret_cast<char*>(&version), sizeof(version));
    if (version != VERSION) {
        std::cerr << "ResourcePack: unsupported pack version: " << version << '\n';
        return false;
    }

    uint32_t resource_count = 0;
    file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));

    resources_.clear();
    resources_.reserve(resource_count);

    for (uint32_t i = 0; i < resource_count; ++i) {
        uint32_t filename_length = 0;
        file.read(reinterpret_cast<char*>(&filename_length), sizeof(filename_length));

        std::string filename(filename_length, '\0');
        file.read(filename.data(), filename_length);

        ResourceEntry entry;
        entry.filename = filename;
        file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
        file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
        file.read(reinterpret_cast<char*>(&entry.checksum), sizeof(entry.checksum));

        resources_[filename] = entry;
    }

    uint64_t data_size = 0;
    file.read(reinterpret_cast<char*>(&data_size), sizeof(data_size));

    data_.resize(data_size);
    file.read(reinterpret_cast<char*>(data_.data()), static_cast<std::streamsize>(data_size));

    decryptData(data_, DEFAULT_ENCRYPT_KEY);

    loaded_ = true;
    return true;
}

auto ResourcePack::savePack(const std::string& pack_file) -> bool {
    std::ofstream file(pack_file, std::ios::binary);
    if (!file) {
        std::cerr << "ResourcePack: could not create pack file: " << pack_file << '\n';
        return false;
    }

    file.write(MAGIC, 4);

    uint32_t version = VERSION;
    file.write(reinterpret_cast<const char*>(&version), sizeof(version));

    auto resource_count = static_cast<uint32_t>(resources_.size());
    file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));

    for (const auto& [filename, entry] : resources_) {
        auto filename_length = static_cast<uint32_t>(filename.length());
        file.write(reinterpret_cast<const char*>(&filename_length), sizeof(filename_length));
        file.write(filename.c_str(), filename_length);
        file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(entry.offset));
        file.write(reinterpret_cast<const char*>(&entry.size), sizeof(entry.size));
        file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(entry.checksum));
    }

    std::vector<uint8_t> encrypted = data_;
    encryptData(encrypted, DEFAULT_ENCRYPT_KEY);

    uint64_t data_size = encrypted.size();
    file.write(reinterpret_cast<const char*>(&data_size), sizeof(data_size));
    file.write(reinterpret_cast<const char*>(encrypted.data()), static_cast<std::streamsize>(data_size));

    return true;
}

auto ResourcePack::addFile(const std::string& filename, const std::string& filepath) -> bool {
    std::ifstream file(filepath, std::ios::binary | std::ios::ate);
    if (!file) {
        std::cerr << "ResourcePack: could not open file: " << filepath << '\n';
        return false;
    }

    std::streamsize file_size = file.tellg();
    file.seekg(0, std::ios::beg);

    std::vector<uint8_t> file_data(file_size);
    if (!file.read(reinterpret_cast<char*>(file_data.data()), file_size)) {
        std::cerr << "ResourcePack: could not read file: " << filepath << '\n';
        return false;
    }

    ResourceEntry entry;
    entry.filename = filename;
    entry.offset = data_.size();
    entry.size = file_data.size();
    entry.checksum = calculateChecksum(file_data);

    data_.insert(data_.end(), file_data.begin(), file_data.end());
    resources_[filename] = entry;

    return true;
}

auto ResourcePack::addDirectory(const std::string& directory) -> bool {
    if (!std::filesystem::exists(directory)) {
        std::cerr << "ResourcePack: directory does not exist: " << directory << '\n';
        return false;
    }

    namespace fs = std::filesystem;
    return std::all_of(fs::recursive_directory_iterator(directory),
        fs::recursive_directory_iterator{},
        [&](const fs::directory_entry& entry) {
            if (!entry.is_regular_file()) {
                return true;
            }
            std::string filepath = entry.path().string();
            std::string filename = fs::relative(entry.path(), directory).string();
            std::ranges::replace(filename, '\\', '/');
            return addFile(filename, filepath);
        });
}

auto ResourcePack::getResource(const std::string& filename) -> std::vector<uint8_t> {
    auto it = resources_.find(filename);
    if (it == resources_.end()) {
        return {};
    }

    const ResourceEntry& entry = it->second;
    if (entry.offset + entry.size > data_.size()) {
        std::cerr << "ResourcePack: invalid resource data: " << filename << '\n';
        return {};
    }

    std::vector<uint8_t> result(data_.begin() + entry.offset,
        data_.begin() + entry.offset + entry.size);

    uint32_t checksum = calculateChecksum(result);
    if (checksum != entry.checksum) {
        std::cerr << "ResourcePack: checksum mismatch for: " << filename << '\n';
    }

    return result;
}

auto ResourcePack::hasResource(const std::string& filename) const -> bool {
    return resources_.contains(filename);
}

void ResourcePack::clear() {
    resources_.clear();
    data_.clear();
    loaded_ = false;
}

auto ResourcePack::getResourceCount() const -> size_t {
    return resources_.size();
}

auto ResourcePack::getResourceList() const -> std::vector<std::string> {
    std::vector<std::string> result;
    result.reserve(resources_.size());
    for (const auto& [filename, entry] : resources_) {
        result.push_back(filename);
    }
    return result;
}
