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

#include "Terrain.hpp"

namespace shardok {

auto AllowsHiding(const Terrain* terrain) -> bool {
    const auto v = AllowsHiding(terrain->type()) && AllowsHiding(terrain->modifier());
    return v;
}

auto AllowsHiding(const net::eagle0::shardok::storage::fb::Terrain_::Type type) -> bool {
    return type == net::eagle0::shardok::storage::fb::Terrain_::Type_SWAMP ||
           type == net::eagle0::shardok::storage::fb::Terrain_::Type_FOREST;
}

auto AllowsHiding(const net::eagle0::shardok::storage::fb::TileModifier& modifier) -> bool {
    return !modifier.castle().present() && !modifier.fire().present();
}

auto AllowsHiding(const TerrainProto& terrain) -> bool {
    return AllowsHiding(terrain.type()) && AllowsHiding(terrain.modifier());
}

auto AllowsHiding(const TerrainType type) -> bool {
    return type == TerrainProto::SWAMP || type == TerrainProto::FOREST;
}

auto AllowsHiding(const TileModifierProto& modifier) -> bool {
    return !modifier.has_castle() && !modifier.has_fire();
}

auto AllowsFortification(const Terrain* terrain) -> bool {
    if (IsWater(terrain->type())) return false;
    return !terrain->modifier().castle().present();
}

auto IsWater(const net::eagle0::shardok::storage::fb::Terrain_::Type type) -> bool {
    return type == net::eagle0::shardok::storage::fb::Terrain_::Type_STILL_WATER ||
           type == net::eagle0::shardok::storage::fb::Terrain_::Type_RIVER;
}

auto IsWater(const TerrainType type) -> bool {
    return type == TerrainProto::STILL_WATER || type == TerrainProto::RIVER;
}

auto IsTraversible(const net::eagle0::shardok::storage::fb::Terrain& terrain) -> bool {
    return terrain.type() != net::eagle0::shardok::storage::fb::Terrain_::Type_MOUNTAIN &&
           (!IsWater(terrain.type()) || terrain.modifier().bridge().present() ||
            terrain.modifier().ice().present());
}

auto MakeHexMapTerrain(const TerrainType tp, TileModifierProto mod) -> TerrainProto {
    TerrainProto hmt{};

    hmt.set_type(tp);
    *hmt.mutable_modifier() = std::move(mod);

    return hmt;
}

auto MakeHexMapTerrain(
        const net::eagle0::shardok::storage::fb::Terrain_::Type tp,
        const net::eagle0::shardok::storage::fb::TileModifier& mod) -> Terrain {
    return Terrain(tp, mod);
}
}  // namespace shardok
