//
// Created by Dan Crosby on 9/15/20.
//

#include "MapLoader.hpp"

#include "src/main/cpp/net/eagle0/common/FilesystemUtils.hpp"

const std::string kShardokMapExtension = ".e0m";
using std::string;

auto LoadMap(const string& mapName) -> HexMapProto {
    const string mapPath = FilesystemUtils::MapFilesDirectory() + mapName + kShardokMapExtension;
    const byte_vector mapProtoBytes = byte_vector::FromPath(mapPath);

    net::eagle0::shardok::common::HexMap mapProto{};
    mapProto.ParseFromArray(mapProtoBytes.data(), (int)mapProtoBytes.size());

    return mapProto;
}

auto LoadMapFromBytes(const string& mapBytes) -> HexMapProto {
    net::eagle0::shardok::common::HexMap mapProto{};
    mapProto.ParseFromString(mapBytes);

    return mapProto;
}

auto GetMapNames() -> vector<string> {
    const auto& mapFileNames = FilesystemUtils::FilesInDirectory(
            FilesystemUtils::MapFilesDirectory(),
            kShardokMapExtension);

    vector<string> names{};

    for (const auto& fileName : mapFileNames) {
        const size_t lastindex = fileName.find_last_of('.');
        names.push_back(fileName.substr(0, lastindex));
    }

    return names;
}
