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

#include "ExtinguishFireCommandFactory.hpp"

#include "src/main/cpp/net/eagle0/shardok/library/FireUtils.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/commands/ExtinguishFireCommand.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/util/HexMapUtils.hpp"

namespace shardok {

auto ExtinguishFireCommandFactory::AddAvailableExtinguishFireCommands(
        CommandList &existingCommands,
        const Unit *unit,
        const ActionPoints remainingActionPoints,
        const Coords &position,
        const HexMap *hexMap,
        const WeatherFb *weather) const -> void {
    if (!unit->has_attached_hero()) return;

    const auto &hero = unit->attached_hero();

    const ActionCost cost =
            ActionCost::UsesAllActionCost(settings.Backing().extinguish_action_point_cost());

    if (cost.IsPossible(remainingActionPoints)) {
        if (GetTerrain(hexMap, position)->modifier().fire().present()) {
            PercentileRollOdds odds = GetExtinguishOdds(
                    settings,
                    GetTerrain(hexMap, position),
                    weather,
                    hero.agility(),
                    hero.wisdom(),
                    true /* ownTile */);
            if (GetTotalOdds(odds) > 1) {
                existingCommands.push_back(std::make_shared<ExtinguishFireCommand>(
                        settings,
                        unit->player_id(),
                        unit->unit_id(),
                        position,
                        odds));
            }
        }

        for (const Coords &adjCoords : HexMapUtils::GetAdjacentCoords(hexMap, position)) {
            if (GetTerrain(hexMap, adjCoords)->modifier().fire().present()) {
                PercentileRollOdds odds = GetExtinguishOdds(
                        settings,
                        GetTerrain(hexMap, adjCoords),
                        weather,
                        hero.agility(),
                        hero.wisdom(),
                        false /* ownTile */);
                if (GetTotalOdds(odds) > 1) {
                    existingCommands.push_back(std::make_shared<ExtinguishFireCommand>(
                            settings,
                            unit->player_id(),
                            unit->unit_id(),
                            adjCoords,
                            odds));
                }
            }
        }
    }
}
void ExtinguishFireCommandFactory::AddAvailableCommands(
        CommandList &commands,
        const CommandFactory::CommandParams &params) const {
    AddAvailableExtinguishFireCommands(
            commands,
            params.unit,
            params.remainingActionPoints,
            params.position,
            params.hexMap,
            params.weatherFb);
}
}  // namespace shardok