using System;
using System.Collections.Generic;
using System.Linq;

namespace NavisworksTransport.Commands
{
    /// <summary>
    /// 路径规划操作结果
    /// 标准化的操作结果格式，包含成功/失败状态、数据、错误信息
    /// </summary>
    public class PathPlanningResult
    {
        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 结果消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 错误信息（当IsSuccess为false时）
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 异常信息（当发生异常时）
        /// </summary>
        public Exception Exception { get; set; }

        /// <summary>
        /// 操作执行的时间戳
        /// </summary>
        public DateTime Timestamp { get; set; }

        /// <summary>
        /// 操作执行耗时（毫秒）
        /// </summary>
        public long ElapsedMilliseconds { get; set; }

        /// <summary>
        /// 创建成功结果
        /// </summary>
        public static PathPlanningResult Success(string message = "操作成功")
        {
            return new PathPlanningResult
            {
                IsSuccess = true,
                Message = message,
                Timestamp = DateTime.Now
            };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        public static PathPlanningResult Failure(string errorMessage, Exception exception = null)
        {
            return new PathPlanningResult
            {
                IsSuccess = false,
                ErrorMessage = errorMessage,
                Exception = exception,
                Timestamp = DateTime.Now
            };
        }

        /// <summary>
        /// 创建验证失败结果
        /// </summary>
        public static PathPlanningResult ValidationFailure(string message)
        {
            return new PathPlanningResult
            {
                IsSuccess = false,
                ErrorMessage = $"验证失败: {message}",
                Timestamp = DateTime.Now
            };
        }
    }

    /// <summary>
    /// 泛型路径规划结果，支持强类型的数据返回
    /// </summary>
    /// <typeparam name="T">结果数据类型</typeparam>
    public class PathPlanningResult<T> : PathPlanningResult
    {
        /// <summary>
        /// 结果数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 创建成功结果（带数据）
        /// </summary>
        public static PathPlanningResult<T> Success(T data, string message = "操作成功")
        {
            return new PathPlanningResult<T>
            {
                IsSuccess = true,
                Data = data,
                Message = message,
                Timestamp = DateTime.Now
            };
        }

        /// <summary>
        /// 创建失败结果（泛型版本）
        /// </summary>
        public static new PathPlanningResult<T> Failure(string errorMessage, Exception exception = null)
        {
            return new PathPlanningResult<T>
            {
                IsSuccess = false,
                ErrorMessage = errorMessage,
                Exception = exception,
                Data = default,
                Timestamp = DateTime.Now
            };
        }

        /// <summary>
        /// 创建验证失败结果（泛型版本）
        /// </summary>
        public static new PathPlanningResult<T> ValidationFailure(string message)
        {
            return new PathPlanningResult<T>
            {
                IsSuccess = false,
                ErrorMessage = $"验证失败: {message}",
                Data = default,
                Timestamp = DateTime.Now
            };
        }

        /// <summary>
        /// 将非泛型结果转换为泛型结果
        /// </summary>
        public static PathPlanningResult<T> FromResult(PathPlanningResult result, T data = default)
        {
            return new PathPlanningResult<T>
            {
                IsSuccess = result.IsSuccess,
                Message = result.Message,
                ErrorMessage = result.ErrorMessage,
                Exception = result.Exception,
                Timestamp = result.Timestamp,
                ElapsedMilliseconds = result.ElapsedMilliseconds,
                Data = data
            };
        }
    }

    /// <summary>
    /// 路径规划特定结果类型
    /// </summary>
    public class PathRouteResult : PathPlanningResult<PathRoute>
    {
        /// <summary>
        /// 创建路径结果
        /// </summary>
        public static PathRouteResult WithRoute(PathRoute route, string message = "路径规划成功")
        {
            return new PathRouteResult
            {
                IsSuccess = route != null,
                Data = route,
                Message = route != null ? message : "未找到可行路径",
                ErrorMessage = route == null ? "路径规划失败" : null,
                Timestamp = DateTime.Now
            };
        }
    }

    /// <summary>
    /// 路径点集合结果类型
    /// </summary>
    public class PathPointsResult : PathPlanningResult<List<PathPoint>>
    {
        /// <summary>
        /// 创建路径点集合结果
        /// </summary>
        public static PathPointsResult WithPoints(List<PathPoint> points, string message = "路径点操作成功")
        {
            return new PathPointsResult
            {
                IsSuccess = points != null && points.Any(),
                Data = points ?? new List<PathPoint>(),
                Message = points != null && points.Any() ? message : "没有找到路径点",
                ErrorMessage = points == null || !points.Any() ? "路径点操作失败" : null,
                Timestamp = DateTime.Now
            };
        }
    }

    /// <summary>
    /// 通道选择结果类型
    /// </summary>
    public class ChannelSelectionResult : PathPlanningResult<List<Autodesk.Navisworks.Api.ModelItem>>
    {
        /// <summary>
        /// 创建通道选择结果
        /// </summary>
        public static ChannelSelectionResult WithChannels(List<Autodesk.Navisworks.Api.ModelItem> channels, string message = "通道选择成功")
        {
            return new ChannelSelectionResult
            {
                IsSuccess = channels != null && channels.Any(),
                Data = channels ?? new List<Autodesk.Navisworks.Api.ModelItem>(),
                Message = channels != null && channels.Any() ? message : "没有选择通道",
                ErrorMessage = channels == null || !channels.Any() ? "通道选择失败" : null,
                Timestamp = DateTime.Now
            };
        }
    }
}