using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace MultiWheelC.TrajectoryPlanning.PathSmoothing;

/// <summary>单个局部 G2 平滑区域的不可变发布报告。</summary>
public sealed class PathSmoothingRegionReport
{
    public PathSmoothingRegionReport(
        int segmentIndex,
        double startArcLengthMeters,
        double endArcLengthMeters,
        IReadOnlyList<double> curvatureJumpsPerMeter,
        double plannedWindowLengthMeters,
        double actualWindowLengthMeters,
        double leftWindowLengthMeters,
        double rightWindowLengthMeters,
        int candidateCount,
        int selectedCandidateIndex,
        PathSmoothingRegionStatus status,
        PathSmoothingRegionFailureReason failureReason,
        double rawPeakCurvatureDerivativePerSquareMeter,
        double resultPeakCurvatureDerivativePerSquareMeter,
        double rawCurvatureVariationCost,
        double resultCurvatureVariationCost,
        double maximumDeviationMeters,
        double minimumBodyClearanceMeters,
        double maximumAbsoluteVehicleCurvaturePerMeter)
    {
        SegmentIndex = segmentIndex;
        StartArcLengthMeters = startArcLengthMeters;
        EndArcLengthMeters = endArcLengthMeters;
        CurvatureJumpsPerMeter = CopyReadOnly(curvatureJumpsPerMeter);
        PlannedWindowLengthMeters = plannedWindowLengthMeters;
        ActualWindowLengthMeters = actualWindowLengthMeters;
        LeftWindowLengthMeters = leftWindowLengthMeters;
        RightWindowLengthMeters = rightWindowLengthMeters;
        CandidateCount = candidateCount;
        SelectedCandidateIndex = status == PathSmoothingRegionStatus.Improved
            ? selectedCandidateIndex
            : -1;
        Status = status;
        FailureReason = failureReason;
        RawPeakCurvatureDerivativePerSquareMeter = rawPeakCurvatureDerivativePerSquareMeter;
        ResultPeakCurvatureDerivativePerSquareMeter = resultPeakCurvatureDerivativePerSquareMeter;
        RawCurvatureVariationCost = rawCurvatureVariationCost;
        ResultCurvatureVariationCost = resultCurvatureVariationCost;
        MaximumDeviationMeters = maximumDeviationMeters;
        MinimumBodyClearanceMeters = minimumBodyClearanceMeters;
        MaximumAbsoluteVehicleCurvaturePerMeter = maximumAbsoluteVehicleCurvaturePerMeter;
    }

    public int SegmentIndex { get; }
    public double StartArcLengthMeters { get; }
    public double EndArcLengthMeters { get; }
    public IReadOnlyList<double> CurvatureJumpsPerMeter { get; }
    public double PlannedWindowLengthMeters { get; }
    public double ActualWindowLengthMeters { get; }
    public double LeftWindowLengthMeters { get; }
    public double RightWindowLengthMeters { get; }
    public int CandidateCount { get; }
    public int SelectedCandidateIndex { get; }
    public PathSmoothingRegionStatus Status { get; }
    public PathSmoothingRegionFailureReason FailureReason { get; }
    public double RawPeakCurvatureDerivativePerSquareMeter { get; }
    public double ResultPeakCurvatureDerivativePerSquareMeter { get; }
    public double RawCurvatureVariationCost { get; }
    public double ResultCurvatureVariationCost { get; }
    public double MaximumDeviationMeters { get; }
    public double MinimumBodyClearanceMeters { get; }
    public double MaximumAbsoluteVehicleCurvaturePerMeter { get; }

    private static IReadOnlyList<double> CopyReadOnly(IReadOnlyList<double> source)
    {
        var copy = new List<double>(source == null ? 0 : source.Count);
        if (source != null)
        {
            for (int index = 0; index < source.Count; index++)
                copy.Add(source[index]);
        }
        return new ReadOnlyCollection<double>(copy);
    }
}
