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

namespace MultiWheelC.TrajectoryPlanning.PathSmoothing.Comparison;

/// <summary>一次离线比较的不可变基线、方法条目和推荐结论。</summary>
public sealed class PathSmoothingComparisonResult
{
    internal PathSmoothingComparisonResult(
        PathSmoothingComparisonEntry rawPathBaseline,
        IReadOnlyList<PathSmoothingComparisonEntry> entries,
        SmoothingMethod? recommendedMethod,
        bool isCancelled,
        string diagnostic)
    {
        RawPathBaseline = rawPathBaseline ?? throw new ArgumentNullException(nameof(rawPathBaseline));
        Entries = CopyReadOnly(entries);
        RecommendedMethod = recommendedMethod;
        IsCancelled = isCancelled;
        Diagnostic = diagnostic ?? string.Empty;
    }

    /// <summary>独立分析的原始粗路径；不属于任何候选方法。</summary>
    public PathSmoothingComparisonEntry RawPathBaseline { get; }

    /// <summary>每个请求方法恰有一个条目；取消时可能只包含已完成的方法。</summary>
    public IReadOnlyList<PathSmoothingComparisonEntry> Entries { get; }

    /// <summary>按公开字典序选择的方法；没有合格方法或取消时为空。</summary>
    public SmoothingMethod? RecommendedMethod { get; }

    /// <summary>比较是否在启动后续方法前被取消。</summary>
    public bool IsCancelled { get; }

    /// <summary>整个比较的稳定状态说明。</summary>
    public string Diagnostic { get; }

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