using System;
using System.Threading;
using System.Threading.Tasks;

namespace NavisworksTransport.Commands
{
    /// <summary>
    /// 路径规划命令接口
    /// 定义了标准化的命令执行接口，支持异步执行和结果返回
    /// </summary>
    public interface IPathPlanningCommand
    {
        /// <summary>
        /// 命令的唯一标识符
        /// </summary>
        string CommandId { get; }

        /// <summary>
        /// 命令的显示名称
        /// </summary>
        string DisplayName { get; }

        /// <summary>
        /// 命令的描述信息
        /// </summary>
        string Description { get; }

        /// <summary>
        /// 命令当前的执行状态
        /// </summary>
        CommandExecutionStatus Status { get; }

        /// <summary>
        /// 执行进度（0-100）
        /// </summary>
        int Progress { get; }

        /// <summary>
        /// 当命令状态改变时触发的事件
        /// </summary>
        event EventHandler<CommandStatusChangedEventArgs> StatusChanged;

        /// <summary>
        /// 当命令进度更新时触发的事件
        /// </summary>
        event EventHandler<CommandProgressChangedEventArgs> ProgressChanged;

        /// <summary>
        /// 异步执行命令
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>命令执行结果</returns>
        Task<PathPlanningResult> ExecuteAsync(CancellationToken cancellationToken = default);

        /// <summary>
        /// 验证命令是否可以执行
        /// </summary>
        /// <returns>验证结果</returns>
        PathPlanningResult CanExecute();

        /// <summary>
        /// 取消命令执行
        /// </summary>
        void Cancel();
    }

    /// <summary>
    /// 命令执行状态
    /// </summary>
    public enum CommandExecutionStatus
    {
        /// <summary>
        /// 未开始
        /// </summary>
        NotStarted,

        /// <summary>
        /// 正在验证
        /// </summary>
        Validating,

        /// <summary>
        /// 正在执行
        /// </summary>
        Executing,

        /// <summary>
        /// 执行成功
        /// </summary>
        Completed,

        /// <summary>
        /// 执行失败
        /// </summary>
        Failed,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled
    }

    /// <summary>
    /// 命令状态改变事件参数
    /// </summary>
    public class CommandStatusChangedEventArgs : EventArgs
    {
        public CommandExecutionStatus PreviousStatus { get; }
        public CommandExecutionStatus CurrentStatus { get; }
        public string Message { get; }
        public Exception Exception { get; }

        public CommandStatusChangedEventArgs(CommandExecutionStatus previousStatus, 
                                           CommandExecutionStatus currentStatus, 
                                           string message = null, 
                                           Exception exception = null)
        {
            PreviousStatus = previousStatus;
            CurrentStatus = currentStatus;
            Message = message;
            Exception = exception;
        }
    }

    /// <summary>
    /// 命令进度改变事件参数
    /// </summary>
    public class CommandProgressChangedEventArgs : EventArgs
    {
        public int Progress { get; }
        public string Message { get; }

        public CommandProgressChangedEventArgs(int progress, string message = null)
        {
            Progress = Math.Max(0, Math.Min(100, progress)); // 确保进度在0-100范围内
            Message = message;
        }
    }
}