using System;
using System.Collections.Generic;
using Autodesk.Navisworks.Api;

namespace NavisworksTransport.Core
{
    /// <summary>
    /// PathPlanningManager事件参数基类
    /// 提供所有路径规划相关事件的基础信息
    /// </summary>
    public abstract class PathPlanningEventArgsBase : EventArgs
    {
        /// <summary>
        /// 事件发生的时间戳
        /// </summary>
        public DateTime Timestamp { get; }

        /// <summary>
        /// 事件来源的PathPlanningManager实例ID
        /// </summary>
        public string ManagerId { get; }

        protected PathPlanningEventArgsBase(string managerId = null)
        {
            Timestamp = DateTime.Now;
            ManagerId = managerId ?? "Default";
        }
    }

    /// <summary>
    /// 状态变更事件参数
    /// </summary>
    public class PathPlanningStatusChangedEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 状态消息
        /// </summary>
        public string StatusMessage { get; }

        /// <summary>
        /// 状态类型
        /// </summary>
        public PathPlanningStatusType StatusType { get; }

        /// <summary>
        /// 附加数据
        /// </summary>
        public object AdditionalData { get; }

        public PathPlanningStatusChangedEventArgs(string statusMessage, PathPlanningStatusType statusType = PathPlanningStatusType.Info, object additionalData = null, string managerId = null)
            : base(managerId)
        {
            StatusMessage = statusMessage;
            StatusType = statusType;
            AdditionalData = additionalData;
        }
    }

    /// <summary>
    /// 错误发生事件参数
    /// </summary>
    public class PathPlanningErrorOccurredEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; }

        /// <summary>
        /// 异常对象
        /// </summary>
        public Exception Exception { get; }

        /// <summary>
        /// 错误级别
        /// </summary>
        public PathPlanningErrorLevel ErrorLevel { get; }

        public PathPlanningErrorOccurredEventArgs(string errorMessage, Exception exception = null, PathPlanningErrorLevel errorLevel = PathPlanningErrorLevel.Error, string managerId = null)
            : base(managerId)
        {
            ErrorMessage = errorMessage;
            Exception = exception;
            ErrorLevel = errorLevel;
        }
    }

    /// <summary>
    /// 路径编辑状态变更事件参数
    /// </summary>
    public class PathEditStateChangedEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 旧状态
        /// </summary>
        public PathEditState PreviousState { get; }

        /// <summary>
        /// 新状态
        /// </summary>
        public PathEditState NewState { get; }

        /// <summary>
        /// 当前编辑的路径
        /// </summary>
        public PathRoute EditingRoute { get; }

        public PathEditStateChangedEventArgs(PathEditState previousState, PathEditState newState, PathRoute editingRoute = null, string managerId = null)
            : base(managerId)
        {
            PreviousState = previousState;
            NewState = newState;
            EditingRoute = editingRoute;
        }
    }

    /// <summary>
    /// 通道选择变更事件参数
    /// </summary>
    public class ChannelSelectionChangedEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 选中的通道集合
        /// </summary>
        public List<ModelItem> SelectedChannels { get; }

        /// <summary>
        /// 选择方式
        /// </summary>
        public string SelectionMethod { get; }

        public ChannelSelectionChangedEventArgs(List<ModelItem> selectedChannels, string selectionMethod = "Unknown", string managerId = null)
            : base(managerId)
        {
            SelectedChannels = selectedChannels ?? new List<ModelItem>();
            SelectionMethod = selectionMethod;
        }
    }

    /// <summary>
    /// 路径点操作事件参数
    /// </summary>
    public class PathPointOperationEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 操作类型
        /// </summary>
        public PathPointOperationType OperationType { get; }

        /// <summary>
        /// 相关的路径点
        /// </summary>
        public PathPoint PathPoint { get; }

        /// <summary>
        /// 相关的路径
        /// </summary>
        public PathRoute Route { get; }

        public PathPointOperationEventArgs(PathPointOperationType operationType, PathPoint pathPoint, PathRoute route = null, string managerId = null)
            : base(managerId)
        {
            OperationType = operationType;
            PathPoint = pathPoint;
            Route = route;
        }
    }

    /// <summary>
    /// 路径生成事件参数
    /// </summary>
    public class RouteGeneratedEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 生成的路径
        /// </summary>
        public PathRoute Route { get; }

        /// <summary>
        /// 生成方式
        /// </summary>
        public RouteGenerationMethod GenerationMethod { get; }

        /// <summary>
        /// 路径名称
        /// </summary>
        public string Name { get; }

        /// <summary>
        /// 使用的网格大小（米），-1表示自动选择，用于自动路径规划
        /// </summary>
        public double GridSize { get; }

        public RouteGeneratedEventArgs(PathRoute route, RouteGenerationMethod generationMethod = RouteGenerationMethod.Manual, double gridSize = -1, string managerId = null)
            : base(managerId)
        {
            Route = route;
            GenerationMethod = generationMethod;
            Name = route?.Name ?? string.Empty;
            GridSize = gridSize;
        }
    }

    /// <summary>
    /// 当前路径变更事件参数
    /// </summary>
    public class CurrentRouteChangedEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 旧路径
        /// </summary>
        public PathRoute PreviousRoute { get; }

        /// <summary>
        /// 新路径
        /// </summary>
        public PathRoute NewRoute { get; }

        public CurrentRouteChangedEventArgs(PathRoute previousRoute, PathRoute newRoute, string managerId = null)
            : base(managerId)
        {
            PreviousRoute = previousRoute;
            NewRoute = newRoute;
        }
    }

    /// <summary>
    /// 路径列表更新事件参数
    /// </summary>
    public class PathPointsListUpdatedEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 更新的路径
        /// </summary>
        public PathRoute Route { get; }

        /// <summary>
        /// 更新原因
        /// </summary>
        public string UpdateReason { get; }

        public PathPointsListUpdatedEventArgs(PathRoute route, string updateReason = "Unknown", string managerId = null)
            : base(managerId)
        {
            Route = route;
            UpdateReason = updateReason;
        }
    }

    /// <summary>
    /// 路径加载完成事件参数
    /// 当从数据库加载路径完成时触发
    /// </summary>
    public class RoutesLoadedEventArgs : PathPlanningEventArgsBase
    {
        /// <summary>
        /// 加载的路径列表
        /// </summary>
        public IReadOnlyList<PathRoute> Routes { get; }

        /// <summary>
        /// 加载的路径数量
        /// </summary>
        public int RouteCount { get; }

        /// <summary>
        /// 加载来源
        /// </summary>
        public string LoadSource { get; }

        public RoutesLoadedEventArgs(IReadOnlyList<PathRoute> routes, string loadSource = "Database", string managerId = null)
            : base(managerId)
        {
            Routes = routes ?? new List<PathRoute>();
            RouteCount = Routes.Count;
            LoadSource = loadSource;
        }
    }

    #region 枚举定义

    /// <summary>
    /// 状态类型
    /// </summary>
    public enum PathPlanningStatusType
    {
        /// <summary>
        /// 信息
        /// </summary>
        Info,

        /// <summary>
        /// 警告
        /// </summary>
        Warning,

        /// <summary>
        /// 成功
        /// </summary>
        Success,

        /// <summary>
        /// 进度更新
        /// </summary>
        Progress
    }

    /// <summary>
    /// 错误级别
    /// </summary>
    public enum PathPlanningErrorLevel
    {
        /// <summary>
        /// 警告
        /// </summary>
        Warning,

        /// <summary>
        /// 错误
        /// </summary>
        Error,

        /// <summary>
        /// 严重错误
        /// </summary>
        Critical
    }

    /// <summary>
    /// 路径点操作类型
    /// </summary>
    public enum PathPointOperationType
    {
        /// <summary>
        /// 添加
        /// </summary>
        Added,

        /// <summary>
        /// 插入
        /// </summary>
        Inserted,

        /// <summary>
        /// 删除
        /// </summary>
        Removed,

        /// <summary>
        /// 修改
        /// </summary>
        Modified,

        /// <summary>
        /// 选中
        /// </summary>
        Selected
    }

    /// <summary>
    /// 路径生成方式
    /// </summary>
    public enum RouteGenerationMethod
    {
        /// <summary>
        /// 手工创建
        /// </summary>
        Manual,

        /// <summary>
        /// 自动规划
        /// </summary>
        AutoPlanning,

        /// <summary>
        /// 导入
        /// </summary>
        Import,

        /// <summary>
        /// 优化
        /// </summary>
        Optimization,

        /// <summary>
        /// 从数据库加载
        /// </summary>
        DatabaseLoad
    }

    #endregion
}