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

#include "PlaceUnitCommand.hpp"

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

namespace shardok {

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

auto PlaceUnitCommand::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    auto actorAfter = *actor;
    actorAfter.mutable_location() = target;

    ActionResult placeResult{};
    placeResult.set_type(ActionType::PLACE_UNIT);
    placeResult.mutable_player()->set_value(actorAfter.player_id());
    placeResult.mutable_actor()->set_value(actorAfter.unit_id());
    *placeResult.mutable_target_coords() = ToCoordsProto(target);
    AddChangedUnit(placeResult, actorAfter);

    return {placeResult};
}

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

    proto.set_player(GetPlayerId());
    proto.set_type(net::eagle0::shardok::common::PLACE_UNIT_COMMAND);
    proto.mutable_actor()->set_value(actor->unit_id());
    *proto.mutable_target() = ToCoordsProto(target);

    return proto;
}
}  // namespace shardok
