/*
Crafter®.Asset
Copyright (C) 2025 Catcrafts®
Catcrafts.net

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
*/

module;

#include <cstdint>
#include <string>
#include <vector>
#include <fstream>
#include <exception>
#include <filesystem>
#include <iostream>
#include <fstream>

module Crafter.Asset;
using namespace Crafter;
namespace fs = std::filesystem;

struct HeaderData {
    std::uint32_t nameLenght;
    std::uint32_t typeLenght;
    std::uint32_t offset;
    std::uint32_t lenght;
};

AssetLoad::AssetLoad(fs::path path) : f(path, std::ios::binary) {
    if(!f) {
        throw std::runtime_error(std::format("Error opening file {} not found", path.generic_string()));
    }
    std::uint32_t version;
    f.read(reinterpret_cast<char*>(&version), sizeof(std::uint32_t));
    std::uint32_t entryCount;
    f.read(reinterpret_cast<char*>(&entryCount), sizeof(std::uint32_t));
    entries.resize(entryCount);
    std::vector<std::uint32_t> lenghts(entryCount);
    for(std::uint32_t i = 0; i < entryCount; i++) {
        HeaderData header;
        f.read(reinterpret_cast<char*>(&header), sizeof(std::uint32_t)*4);
        entries[i].name.resize(header.nameLenght);
        f.read(reinterpret_cast<char*>(&entries[i].name[0]), header.nameLenght);
        entries[i].type.resize(header.typeLenght);
        f.read(reinterpret_cast<char*>(&entries[i].type[0]), header.typeLenght);
        lenghts[i] = header.lenght;
    }
    headerLength = sizeof(std::uint32_t)*2 + (sizeof(std::uint32_t)*4*entryCount);
}

std::vector<char> AssetLoad::Load(std::string_view name) {
    auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) {
        return entry.name == name;
    });
    return LoadOffset(it->offset, it->lenght);
}
std::vector<char> AssetLoad::Load(std::string_view name, uint32_t lenght) {
    auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) {
        return entry.name == name;
    });
    return LoadOffset(it->offset, lenght);
}
void AssetLoad::Load(std::string_view name, void* data) {
    auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) {
        return entry.name == name;
    });
    return LoadOffset(it->offset, it->lenght, data);
}
void AssetLoad::Load(std::string_view name, void* data, uint32_t lenght) {
    auto it = std::find_if(entries.begin(), entries.end(), [&](const auto& entry) {
        return entry.name == name;
    });
    return LoadOffset(it->offset, lenght, data);
}

std::vector<char> AssetLoad::Load(uint32_t index) {
    return LoadOffset(entries[index].offset, entries[index].lenght);
}
std::vector<char> AssetLoad::Load(uint32_t index, uint32_t lenght) {
    return LoadOffset(entries[index].offset, lenght);
}
void AssetLoad::Load(uint32_t index, void* data) {
    return LoadOffset(entries[index].offset, entries[index].lenght, data);
}
void AssetLoad::Load(uint32_t index, void* data, uint32_t lenght) {
    return LoadOffset(entries[index].offset, lenght, data);
}

std::vector<char> AssetLoad::Load(const AssetEntry& entry) {
    return LoadOffset(entry.offset, entry.lenght);
}
std::vector<char> AssetLoad::Load(const AssetEntry& entry, uint32_t lenght) {
    return LoadOffset(entry.offset, lenght);
}
void AssetLoad::Load(const AssetEntry& entry, void* data) {
    return LoadOffset(entry.offset, entry.lenght, data);
}
void AssetLoad::Load(const AssetEntry& entry, void* data, uint32_t lenght) {
    return LoadOffset(entry.offset, lenght, data);
}

std::vector<char> AssetLoad::LoadOffset(uint32_t offset, uint32_t lenght) {
    f.seekg(offset);
    std::vector<char> vector(lenght);
    f.read(vector.data(), lenght);
    return vector;
}
void AssetLoad::LoadOffset(uint32_t offset, uint32_t lenght, void* data) {
    f.seekg(offset);
    f.read(reinterpret_cast<char*>(data), lenght);
}

std::vector<char> AssetLoad::LoadAll() {
    f.seekg(headerLength);
    return std::vector<char>((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
}

AssetSave::AssetSave(std::span<AssetEntry const> entries, std::span<char const> buffer) : entries(entries.begin(), entries.end()), buffer(buffer.begin(), buffer.end()) {

}

void AssetSave::AddEntry(std::string_view name, std::string_view type, std::span<char const> data) {
    entries.emplace_back(name, type, data.size());
    buffer.insert(buffer.end(), data.begin(), data.end());
}

void AssetSave::Save(fs::path path) {
    std::ofstream f(path, std::ios::binary);
    std::uint32_t version = 0;
    f.write(reinterpret_cast<const char*>(&version), sizeof(std::uint32_t));
    std::uint32_t entriesCount = entries.size();
    f.write(reinterpret_cast<const char*>(&entriesCount), sizeof(std::uint32_t));
    std::uint32_t offset = 0;
    for(const AssetEntry& entry : entries) {
        std::uint32_t nameSize = entry.name.size();
        f.write(reinterpret_cast<const char*>(&nameSize), sizeof(std::uint32_t));
        std::uint32_t typeSize = entry.type.size();
        f.write(reinterpret_cast<const char*>(&typeSize), sizeof(std::uint32_t));
        f.write(reinterpret_cast<const char*>(&offset), sizeof(std::uint32_t));
        f.write(reinterpret_cast<const char*>(&entry.lenght), sizeof(std::uint32_t));
        offset+=entry.lenght;
        f.write(reinterpret_cast<const char*>(&entry.name[0]), entry.name.size());
        f.write(reinterpret_cast<const char*>(&entry.type[0]), entry.type.size());
    }
     f.write(reinterpret_cast<const char*>(buffer.data()), buffer.size());
}