using System;

namespace MultiWheelC.TrajectoryPlanning.EMPlanner;

/// <summary>Caller-captured request and clock value for one rolling cycle.</summary>
public sealed class PlanningCycleInput
{
    public PlanningCycleInput(EmPlanningRequest request, DateTimeOffset now)
    {
        Request = request ?? throw new ArgumentNullException(nameof(request));
        Identity = PlanningCycleIdentity.FromRequest(request);
        Now = now;
        ReplanPeriodSeconds = ReadReplanPeriod(request.Configuration);
    }

    public EmPlanningRequest Request { get; }

    public PlanningCycleIdentity Identity { get; }

    public DateTimeOffset Now { get; }

    public double ReplanPeriodSeconds { get; }

    private static double ReadReplanPeriod(EmPlannerConfiguration configuration)
    {
        double configured = configuration?.Scheduling?.ReplanPeriodSeconds ?? 0.20d;
        return !double.IsNaN(configured) && !double.IsInfinity(configured) && configured > 0d ? configured : 0.20d;
    }
}
