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

#include "FortifyCommand.hpp"

#include "src/main/cpp/net/eagle0/shardok/library/unit/Unit.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/util/ActionResultFlatbufferHelpers.hpp"

namespace shardok {

auto FortifyCommand::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    ActionResult fortifyResult{};
    fortifyResult.set_type(net::eagle0::shardok::common::FORTIFIED);
    fortifyResult.mutable_player()->set_value(GetPlayerId());
    fortifyResult.mutable_actor()->set_value(fortifyingUnitId);

    auto unitAfter = *currentState->units()->Get(fortifyingUnitId);
    MutatingSpendActionPoints(&unitAfter, cost);
    unitAfter.mutate_fortified(true);
    MutatingBumpStrengthXp(&unitAfter, strengthXp);
    MutatingBumpAgilityXp(&unitAfter, agilityXp);
    MutatingBumpAllKnowledgeToMinimum(&unitAfter, minimumKnowledgeAfter);

    AddChangedUnit(fortifyResult, unitAfter);

    return {fortifyResult};
}

FortifyCommand::FortifyCommand(
        const ActionCost& cost,
        const PlayerId playerId,
        const UnitId fortifyingUnitId,
        const int strengthXp,
        const int agilityXp,
        const int professionKnowledgeBump)
    : ShardokCommand(playerId),
      cost(cost),
      fortifyingUnitId(fortifyingUnitId),
      strengthXp(strengthXp),
      agilityXp(agilityXp),
      minimumKnowledgeAfter(professionKnowledgeBump) {}

auto FortifyCommand::GetCommandProto() const -> CommandProto {
    CommandProto proto;

    proto.set_player(GetPlayerId());
    proto.set_type(net::eagle0::shardok::common::FORTIFY_COMMAND);
    proto.mutable_actor()->set_value(fortifyingUnitId);

    return proto;
}
}  // namespace shardok
