/*
Crafter.Build
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;


AssetEntry::AssetEntry() {
    
}


Asset::Asset() {}
Asset::Asset(std::vector<AssetEntry> entries): entries(entries) {}

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

void Asset::LoadFull(fs::path path) {
    std::ifstream f(path, std::ios::binary);
    if(!f) {
        throw std::runtime_error(std::format("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;
    }
    for(std::uint32_t i = 0; i < entryCount; i++) {
        entries[i].data.resize(lenghts[i]);
        f.read(reinterpret_cast<char*>(entries[i].data.data()), lenghts[i]);
    }
}
void Asset::LoadHeaders(fs::path path) {

}
void Asset::LoadSpecific(fs::path path, std::string entry) {

}
void Asset::LoadSpecific(fs::path path, std::vector<std::string> entries) {

}
void Asset::LoadSpecific(fs::path path, std::uint32_t entry) {

}
void Asset::LoadSpecific(fs::path path, std::vector<std::uint32_t> entries) {

}
void Asset::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));
        std::uint32_t dataSize = entry.data.size();
        f.write(reinterpret_cast<const char*>(&dataSize), sizeof(std::uint32_t));
        offset+=dataSize;
        f.write(reinterpret_cast<const char*>(&entry.name[0]), entry.name.size());
        f.write(reinterpret_cast<const char*>(&entry.type[0]), entry.type.size());
    }
    for(const AssetEntry& entry : entries) {
        f.write(reinterpret_cast<const char*>(entry.data.data()), entry.data.size());
    }
    f.close();
}