//
// Created by Dan Crosby on 5/31/20.
//

#include "UnitRestCommand.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 UnitRestCommand::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    auto actorAfter = *currentState->units()->Get(actorId);

    if (actorAfter.has_attached_hero()) {
        MutatingAdjustVigor(
                &actorAfter.mutable_attached_hero(),
                actorAfter.remaining_action_points() * actionCostToVigorCost);
    }

    actorAfter.mutate_remaining_action_points(0);

    ActionResult result{};
    result.set_type(net::eagle0::shardok::common::ActionType::UNIT_REST);
    result.mutable_player()->set_value(GetPlayerId());
    result.mutable_actor()->set_value(actorId);
    AddChangedUnit(result, actorAfter);

    return {result};
}

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

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

    return proto;
}
}  // namespace shardok
