//
// Created by Dan Crosby on 1/9/22.
//

#include "src/main/cpp/net/eagle0/common/byte_vector.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/GameStateW.hpp"
#include "src/main/protobuf/net/eagle0/common/shardok_internal_interface.pb.h"
#include "src/main/protobuf/net/eagle0/shardok/storage/game.pb.h"

auto main(int argc, char** argv) -> int {
    char* path = argv[1];

    printf("Checking file %s\n", path);

    auto bytes = byte_vector::FromPath(path);

    net::eagle0::shardok::storage::Game game;
    game.ParseFromArray(bytes.data(), bytes.size());

    printf("The game ID is %s\n", game.game_id().c_str());
    printf("The size is %lu\n", bytes.size());

    const int arCount = game.action_result_size();
    printf("There are %d results\n", arCount);

    for (int arIndex = 0; arIndex < arCount; arIndex++) {
        shardok::GameStateW gameState =
                shardok::GameStateW::FromByteString(game.action_result(arIndex).state_after_fb());
        const auto* hexMap = gameState->hex_map();

        for (int terrainIndex = 0; terrainIndex < hexMap->terrain()->size(); terrainIndex++) {
            const auto* terrain = hexMap->terrain()->Get(terrainIndex);
            if (terrain->type() == net::eagle0::shardok::storage::fb::Terrain_::Type_STILL_WATER)
                continue;
            if (terrain->type() == net::eagle0::shardok::storage::fb::Terrain_::Type_RIVER)
                continue;

            if (terrain->modifier().bridge().present()) {
                printf("Caught one! arIndex %d, terrainIndex %d\n", arIndex, terrainIndex);
                printf("Action result was %s\n",
                       game.action_result(arIndex).action_result().DebugString().c_str());
                exit(0);
            }
        }
    }
}