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

#include "RetreatCommand.hpp"

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

namespace shardok {
auto RetreatCommand::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    ActionResult result{};
    result.set_type(net::eagle0::shardok::common::ActionType::RETREATED);
    result.mutable_player()->set_value(GetPlayerId());
    result.mutable_actor()->set_value(retreatingUnitId);
    net::eagle0::shardok::storage::ResolvedUnit* resolvedUnit = result.add_resolved_units();
    AddUnitToResolved(*resolvedUnit, *currentState->units()->Get(retreatingUnitId));
    resolvedUnit->set_status(net::eagle0::shardok::storage::ResolvedUnit_UnitStatus_RETREATED_UNIT);

    return {result};
}

RetreatCommand::RetreatCommand(
        const ActionCost& cost,
        const PlayerId playerId,
        const UnitId retreatingUnitId)
    : ShardokCommand(playerId),
      cost(cost),
      retreatingUnitId(retreatingUnitId) {}

CommandProto RetreatCommand::GetCommandProto() const {
    CommandProto proto;

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

    return proto;
}
}  // namespace shardok
