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

#include "PlaceHiddenUnitCommand.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;
using net::eagle0::shardok::common::GameStatus;

[[nodiscard]] auto PlaceHiddenUnitCommand::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    auto actorAfter = *currentState->units()->Get(actorId);
    actorAfter.mutable_location() = target;
    actorAfter.mutate_hidden(true);
    MutatingBumpAllKnowledgeToMinimum(&actorAfter, minimumKnowledge);

    ActionResult placeResult{};
    placeResult.set_type(ActionType::PLACE_HIDDEN_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 PlaceHiddenUnitCommand::GetCommandProto() const -> CommandProto {
    CommandProto proto{};

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

    return proto;
}

}  // namespace shardok
