//
// Created by Dan Crosby on 4/7/24.
//

#include "HexMapHasher.hpp"

#include <exception>

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

namespace shardok {

class BadHashException : public std::exception {
public:
    BadHashException() : std::exception() {}

    [[nodiscard]] auto what() const noexcept -> const char* override { return "Bad map hash!"; };
};

auto GetModifierHash(const HexMap* map) -> uint64_t {
    if (map->base_hash() == 0) { throw BadHashException(); }

    uint64_t modifierId = FNV_OFFSET_BASIS;
    for (const auto& t : *map->terrain()) {
        const auto modByte = shardok::ComputeModifierByte(t->modifier());
        MixIn(modifierId, modByte);
        if (t->modifier().snow().present()) {
            MixIn(modifierId, static_cast<char>(t->modifier().snow().integrity()));
        }
        MixIn(modifierId, '/');
    }

    return modifierId;
}
}  // namespace shardok