using System;
using System.Collections.Generic;

namespace MultiWheelC.TrajectoryPlanning.Mapping;

/// <summary>直接提供世界 mm 几何列表的手工障碍物快照来源。</summary>
public sealed class ManualObstacleSource : IMapObstacleSource
{
    private readonly IReadOnlyList<IMapObstacle> _obstacles;
    /// <summary>
    /// 创建手工障碍物来源。
    ///
    /// 参数：sourceId 为唯一来源标识；sourceVersion 为非负快照版本；isRequired 指示失败是否阻断建图；obstacles 为世界 mm 几何列表。
    /// 注意：几何内容变化时，调用方必须同步提高 sourceVersion。
    /// </summary>
    public ManualObstacleSource(string sourceId, long sourceVersion, bool isRequired, IReadOnlyList<IMapObstacle> obstacles)
    {
        SourceId = sourceId; SourceVersion = sourceVersion; IsRequired = isRequired; _obstacles = obstacles;
    }
    /// <summary>该快照的唯一来源标识。</summary>
    public string SourceId { get; }
    /// <summary>该快照的版本号；内容变化时必须递增。</summary>
    public long SourceVersion { get; }
    /// <summary>true 表示无效来源会阻断建图；false 表示只记录来源状态。</summary>
    public bool IsRequired { get; }
    /// <summary>
    /// 返回手工障碍物的世界几何。
    ///
    /// 返回：列表为空时为 Empty；列表为空引用或含无效几何时为 Invalid；其余情况为 Applied。
    /// </summary>
    public ObstacleProjectionResult ProjectToWorld()
    {
        if (_obstacles == null) return ObstacleProjectionResult.Invalid("Manual obstacle collection is null.");
        if (_obstacles.Count == 0) return ObstacleProjectionResult.Empty("Manual obstacle collection is empty.");
        for (int i = 0; i < _obstacles.Count; i++)
            if (_obstacles[i] == null || !_obstacles[i].IsValid) return ObstacleProjectionResult.Invalid("Manual obstacle geometry is invalid.");
        return ObstacleProjectionResult.Applied(_obstacles, "Manual obstacle snapshot applied.");
    }
}
