namespace ReactorMaintenance.Simulation;

public static class RobotStateExtensions
{
    public static int Count(this RobotState robot, ERemedyType remedy)
    {
        return remedy switch {
            ERemedyType.FuelNeutralizer => robot.FuelNeutralizers,
            ERemedyType.CoolantNeutralizer => robot.CoolantNeutralizers,
            ERemedyType.ElectricityNeutralizer => robot.ElectricityNeutralizers,
            ERemedyType.HeatShield => robot.HeatShields,
            _ => throw new ArgumentOutOfRangeException(nameof(remedy), remedy, "Unsupported remedy.")
        };
    }

    public static RobotState Add(this RobotState robot, ERemedyType remedy, int amount)
    {
        return remedy switch {
            ERemedyType.FuelNeutralizer => robot with { FuelNeutralizers = ClampInventory(robot.FuelNeutralizers + amount) },
            ERemedyType.CoolantNeutralizer => robot with { CoolantNeutralizers = ClampInventory(robot.CoolantNeutralizers + amount) },
            ERemedyType.ElectricityNeutralizer => robot with { ElectricityNeutralizers = ClampInventory(robot.ElectricityNeutralizers + amount) },
            ERemedyType.HeatShield => robot with { HeatShields = ClampInventory(robot.HeatShields + amount) },
            _ => throw new ArgumentOutOfRangeException(nameof(remedy), remedy, "Unsupported remedy.")
        };
    }

    public static RobotState Spend(this RobotState robot, ERemedyType remedy)
    {
        return robot.Count(remedy) <= 0 ? robot : robot.Add(remedy, -1);
    }

    private static int ClampInventory(int value)
    {
        return Math.Clamp(value, 0, Balancing.Current.InventoryCapacityPerRemedy);
    }
}