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

#include "MeteorTargetCommandFactory.hpp"

#include "src/main/cpp/net/eagle0/shardok/library/commands/MeteorCancelCommand.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/commands/MeteorTargetCommand.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/util/MeteorRangeCoords.hpp"

namespace shardok {

auto MeteorTargetCommandFactory::AddAvailableMeteorTargetCommands(
        CommandList &existingCommands,
        const Unit *unit,
        const HexMap *map) const -> void {
    if (!unit->has_attached_hero()) return;
    if (unit->attached_hero().profession_info().profession() !=
        net::eagle0::shardok::storage::fb::Profession_MAGE)
        return;
    if (unit->attached_hero().profession_info().meteor_cast_state() !=
        net::eagle0::shardok::storage::fb::MultiroundMagicState_TARGET)
        return;
    if (unit->attached_hero().profession_info().cast_target().row() > -1) return;

    existingCommands.emplace_back(std::make_shared<MeteorCancelCommand>(
            settings.ActionCostFor(settings.Backing().meteor_cancel_action_point_cost()),
            settings,
            unit->player_id(),
            unit->unit_id()));

    const Coords &unitLocation = unit->location();
    for (const Coords &meteorCoords : CoordsInMeteorRange(map, unitLocation, settings)) {
        existingCommands.emplace_back(std::make_shared<MeteorTargetCommand>(
                settings.ActionCostFor(settings.Backing().meteor_target_action_point_cost()),
                settings,
                unit->player_id(),
                unit->unit_id(),
                meteorCoords,
                settings.Backing().minimum_knowledge_for_profession()));
    }
}
void MeteorTargetCommandFactory::AddAvailableCommands(
        CommandList &commands,
        const CommandParams &params) const {
    AddAvailableMeteorTargetCommands(commands, params.unit, params.hexMap);
}
}  // namespace shardok
