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

#include "ControlCommandFactory.hpp"

#include "src/main/cpp/net/eagle0/shardok/library/commands/ControlCommand.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/commands/DismissUnitCommand.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/commands/ReleaseUnitCommand.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/unit/Unit.hpp"

namespace shardok {

[[nodiscard]] auto GetDismissOdds(
        const SettingsGetter &settings,
        double charisma,
        double intelligence) -> PercentileRollOdds;

auto ControlCommandFactory::AddAvailableControlCommands(
        CommandList &existingCommands,
        const Unit *unit,
        const ActionPoints remainingActionPoints) const -> void {
    if (unit->has_attached_hero() &&
        unit->attached_hero().control_info().controlled_unit_id() != -1 &&
        remainingActionPoints > 0) {
        const UnitId controlledUnitId = unit->attached_hero().control_info().controlled_unit_id();

        existingCommands.push_back(std::make_shared<ControlCommand>(
                unit->player_id(),
                unit->unit_id(),
                controlledUnitId));

        existingCommands.push_back(std::make_shared<ReleaseUnitCommand>(
                unit->player_id(),
                unit->unit_id(),
                controlledUnitId));

        existingCommands.push_back(std::make_shared<DismissUnitCommand>(
                unit->player_id(),
                unit->unit_id(),
                controlledUnitId,
                GetDismissOdds(
                        settings,
                        unit->attached_hero().charisma(),
                        unit->attached_hero().wisdom())));
    }
}
void ControlCommandFactory::AddAvailableCommands(CommandList &commands, const CommandParams &params)
        const {
    AddAvailableControlCommands(commands, params.unit, params.remainingActionPoints);
}

auto GetDismissOdds(const SettingsGetter &settings, double charisma, double intelligence)
        -> PercentileRollOdds {
    return MakeOdds(
            settings.Backing().dismiss_base_odds(),
            0,
            0,
            0,
            {charisma, charisma, intelligence});
}

}  // namespace shardok
