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

#include "RetreatCommandFactory.hpp"

#include "src/main/cpp/net/eagle0/shardok/library/commands/RetreatCommand.hpp"

namespace shardok {

RetreatCommandFactory::RetreatCommandFactory(const SettingsGetter &getter) : settings(getter) {}

void RetreatCommandFactory::AddAvailableRetreatCommands(
        CommandList &commands,
        const Unit *unit,
        const ActionPoints remainingActionPoints,
        const bool isAttacker) const {
    if (!settings.ActionCostFor(settings.Backing().retreat_action_point_cost())
                 .IsPossible(remainingActionPoints)) {
        return;
    }
    // only defenders can retreat
    if (isAttacker) { return; }
    // Only heroes can retreat
    if (!unit->has_attached_hero()) { return; }
    commands.push_back(std::make_shared<RetreatCommand>(
            settings.ActionCostFor(settings.Backing().retreat_action_point_cost()),
            unit->player_id(),
            unit->unit_id()));
}
void RetreatCommandFactory::AddAvailableCommands(CommandList &commands, const CommandParams &params)
        const {
    AddAvailableRetreatCommands(
            commands,
            params.unit,
            params.remainingActionPoints,
            params.isAttacker);
}

}  // namespace shardok
