//
// Created by Dan Crosby on 3/19/20.
//

#include <cstdio>

#include "src/main/cpp/net/eagle0/common/FilesystemUtils.hpp"
#include "src/main/cpp/net/eagle0/common/byte_vector.hpp"
#include "src/main/protobuf/net/eagle0/shardok/common/hex_map.pb.h"

using HexMapProto = net::eagle0::shardok::common::HexMap;

void ModifyMap(HexMapProto& map);

int main(const int argc, char** argv) {
    if (argc < 3) {
        printf("Usage: %s input_path output_path\n", argv[0]);
        return -1;
    }

    const std::string inputPath = argv[1];
    const std::string outputPath = argv[2];
    const byte_vector mapProtoBytes = byte_vector::FromPath(inputPath);
    net::eagle0::shardok::common::HexMap mapProto;
    mapProto.ParseFromArray(mapProtoBytes.data(), (int)mapProtoBytes.size());

    ModifyMap(mapProto);
    const std::string outputString = mapProto.SerializeAsString();
    if (FilesystemUtils::AtomicallySaveToPath(outputPath, byte_vector(outputString))) return 0;

    printf("Failed to save.\n");
    return -1;
}

void ModifyMap(HexMapProto& map) {
    for (int i = 0; i < map.terrain_size(); i++) {
        const auto existingTerrainType = map.terrain(i).type();
        const auto newTerrain =
                (net::eagle0::shardok::common::Terrain_Type)(existingTerrainType + 2);
        map.mutable_terrain(i)->set_type(newTerrain);
    }
}
