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

#include "MeteorCancelCommand.hpp"

#include "src/main/cpp/net/eagle0/shardok/library/actions/ActionRequiresHeroException.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 Coords = net::eagle0::shardok::storage::fb::Coords;

MeteorCancelCommand::MeteorCancelCommand(
        const ActionCost& cost,
        const SettingsGetter& settingsGetter,
        const PlayerId playerId,
        const UnitId casterId)
    : ShardokCommand(playerId),
      cost(cost),
      settings(settingsGetter),
      casterId(casterId) {}

auto MeteorCancelCommand::InternalExecute(
        const GameStateW& currentState,
        const std::shared_ptr<RandomGenerator>& generator) const -> vector<ActionResult> {
    const Unit* caster = currentState->units()->Get(casterId);
    auto casterAfter = *caster;

    MutatingSpendActionPoints(&casterAfter, cost);
    auto& mutableAttHero = casterAfter.mutable_attached_hero();

    mutableAttHero.mutable_profession_info().mutate_meteor_cast_state(
            net::eagle0::shardok::storage::fb::MultiroundMagicState_NONE);
    mutableAttHero.mutable_profession_info().mutable_cast_target() = Coords(-1, -1);

    ActionResult result{};
    result.set_type(net::eagle0::shardok::common::METEOR_CANCEL);
    result.mutable_player()->set_value(GetPlayerId());
    result.mutable_actor()->set_value(casterId);
    AddChangedUnit(result, casterAfter);

    return {result};
}

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

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

    return proto;
}

}  // namespace shardok
