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

#include "AIScoreUtilities.hpp"

namespace shardok {

using Coords = net::eagle0::shardok::storage::fb::Coords;
using GameState = net::eagle0::shardok::storage::fb::GameState;

auto HasAttachedHeroWithProfession(
        const Unit *unit,
        const net::eagle0::shardok::storage::fb::Profession profession) -> bool {
    return unit->has_attached_hero() &&
           unit->attached_hero().profession_info().profession() == profession;
}

auto CastleClaimCapableAttackerUnitCount(const GameState *gameState) -> int {
    int count = 0;

    for (const auto *unit : *gameState->units()) {
        const auto *pi = PlayerInfoForPid(gameState, unit->player_id());
        if (pi && !pi->is_defender() &&
            (unit->status() == net::eagle0::shardok::storage::fb::UnitStatus_RESERVE_UNIT ||
             unit->status() == net::eagle0::shardok::storage::fb::UnitStatus_NORMAL_UNIT) &&
            unit->battalion().type() != net::eagle0::shardok::storage::fb::BattalionTypeId_UNDEAD) {
            ++count;
        }
    }

    return count;
}

auto PlayerInfoForPid(const GameState *gs, const PlayerId pid) -> const PlayerInfo * {
    if (gs->player_infos()) {
        for (const auto &pi : *gs->player_infos()) {
            if (pi->player_id() == pid) return pi;
        }
    }
    return nullptr;
}

}  // namespace shardok
