//
//  ReinforceCommand.cpp
//  eagle0
//
//  Created by Dan Crosby on 12/30/19.
//  Copyright © 2019 none. All rights reserved.
//

#include "ReinforceCommand.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 {

using net::eagle0::shardok::common::ActionType;

auto ReinforceCommand::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    const Unit* actingUnit = currentState->units()->Get(actingUnitId);
    auto actorAfter = *actingUnit;
    MutatingSpendActionPoints(&actorAfter, cost);

    const Unit* joiningUnit = currentState->units()->Get(joiningUnitId);
    auto joinerAfter = *joiningUnit;
    joinerAfter.mutable_location() = location;

    ActionResult placeResult{};
    placeResult.set_type(ActionType::REINFORCED);
    placeResult.mutable_player()->set_value(actorAfter.player_id());
    placeResult.mutable_actor()->set_value(actorAfter.unit_id());
    *placeResult.mutable_target_coords() = ToCoordsProto(location);

    AddChangedUnit(placeResult, actorAfter);
    AddChangedUnit(placeResult, joinerAfter);

    return {placeResult};
}

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

    proto.set_player(GetPlayerId());
    proto.set_type(net::eagle0::shardok::common::REINFORCE_COMMAND);
    proto.mutable_actor()->set_value(actingUnitId);
    proto.mutable_target_unit()->set_value(joiningUnitId);
    *proto.mutable_target() = ToCoordsProto(location);

    return proto;
}
}  // namespace shardok
