namespace NavisworksTransport.Core.UIUpdate
{
    #region 枚举类型

    /// <summary>
    /// UI更新优先级
    /// </summary>
    public enum UIUpdatePriority
    {
        /// <summary>
        /// 低优先级 - 非关键UI更新
        /// </summary>
        Low = 0,

        /// <summary>
        /// 普通优先级 - 标准UI更新
        /// </summary>
        Normal = 1,

        /// <summary>
        /// 高优先级 - 重要UI更新
        /// </summary>
        High = 2,

        /// <summary>
        /// 关键优先级 - 关键UI更新，最优先处理
        /// </summary>
        Critical = 3
    }

    /// <summary>
    /// UI框架类型
    /// </summary>
    public enum UIFrameworkType
    {
        /// <summary>
        /// 自动检测
        /// </summary>
        Auto = 0,

        /// <summary>
        /// Windows Forms
        /// </summary>
        WinForms = 1,

        /// <summary>
        /// WPF (Windows Presentation Foundation)
        /// </summary>
        WPF = 2,

        /// <summary>
        /// 混合模式（WPF + WinForms）
        /// </summary>
        Hybrid = 3
    }

    /// <summary>
    /// UI更新服务状态
    /// </summary>
    public enum UIUpdateServiceStatus
    {
        /// <summary>
        /// 未初始化
        /// </summary>
        NotInitialized = 0,

        /// <summary>
        /// 正在初始化
        /// </summary>
        Initializing = 1,

        /// <summary>
        /// 运行中
        /// </summary>
        Running = 2,

        /// <summary>
        /// 已暂停
        /// </summary>
        Paused = 3,

        /// <summary>
        /// 正在关闭
        /// </summary>
        Shutting = 4,

        /// <summary>
        /// 已关闭
        /// </summary>
        Shutdown = 5,

        /// <summary>
        /// 出错状态
        /// </summary>
        Error = 6
    }

    /// <summary>
    /// UI事务状态
    /// </summary>
    public enum UITransactionStatus
    {
        /// <summary>
        /// 活动状态
        /// </summary>
        Active = 0,

        /// <summary>
        /// 正在提交
        /// </summary>
        Committing = 1,

        /// <summary>
        /// 已提交
        /// </summary>
        Committed = 2,

        /// <summary>
        /// 正在回滚
        /// </summary>
        RollingBack = 3,

        /// <summary>
        /// 已回滚
        /// </summary>
        RolledBack = 4,

        /// <summary>
        /// 已失败
        /// </summary>
        Failed = 5
    }

    /// <summary>
    /// UI更新操作类型
    /// </summary>
    public enum UIUpdateOperationType
    {
        /// <summary>
        /// 属性更新
        /// </summary>
        PropertyUpdate = 0,

        /// <summary>
        /// 集合操作
        /// </summary>
        CollectionOperation = 1,

        /// <summary>
        /// 控件状态更新
        /// </summary>
        ControlStateUpdate = 2,

        /// <summary>
        /// 数据绑定刷新
        /// </summary>
        DataBindingRefresh = 3,

        /// <summary>
        /// 视觉状态更新
        /// </summary>
        VisualStateUpdate = 4,

        /// <summary>
        /// 自定义操作
        /// </summary>
        CustomOperation = 99
    }

    #endregion

    #region 结果类型

    /// <summary>
    /// UI更新验证结果
    /// </summary>
    public class UIUpdateValidationResult
    {
        /// <summary>
        /// 验证是否成功
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 验证消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 验证错误信息
        /// </summary>
        public IList<string> Errors { get; set; } = new List<string>();

        /// <summary>
        /// 验证警告信息
        /// </summary>
        public IList<string> Warnings { get; set; } = new List<string>();

        /// <summary>
        /// 创建成功的验证结果
        /// </summary>
        public static UIUpdateValidationResult Success(string message = "验证通过")
        {
            return new UIUpdateValidationResult { IsValid = true, Message = message };
        }

        /// <summary>
        /// 创建失败的验证结果
        /// </summary>
        public static UIUpdateValidationResult Failure(string message, params string[] errors)
        {
            return new UIUpdateValidationResult 
            { 
                IsValid = false, 
                Message = message,
                Errors = errors ?? new string[0]
            };
        }
    }

    /// <summary>
    /// UI更新执行结果
    /// </summary>
    public class UIUpdateResult
    {
        /// <summary>
        /// 更新是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

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

        /// <summary>
        /// 异常信息（如果有）
        /// </summary>
        public Exception Exception { get; set; }

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

        /// <summary>
        /// 更新操作标识符
        /// </summary>
        public string UpdateId { get; set; }

        /// <summary>
        /// 额外数据
        /// </summary>
        public IDictionary<string, object> Data { get; set; } = new Dictionary<string, object>();

        /// <summary>
        /// 创建成功结果
        /// </summary>
        public static UIUpdateResult Success(string message = "更新成功", string updateId = null)
        {
            return new UIUpdateResult { IsSuccess = true, Message = message, UpdateId = updateId };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        public static UIUpdateResult Failure(string message, Exception exception = null, string updateId = null)
        {
            return new UIUpdateResult 
            { 
                IsSuccess = false, 
                Message = message, 
                Exception = exception,
                UpdateId = updateId
            };
        }
    }

    /// <summary>
    /// 批量UI更新结果
    /// </summary>
    public class UIBatchUpdateResult
    {
        /// <summary>
        /// 批量更新是否全部成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 总更新操作数量
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 成功的更新操作数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败的更新操作数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 批量操作消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 个别更新操作的详细结果
        /// </summary>
        public IList<UIUpdateResult> Results { get; set; } = new List<UIUpdateResult>();

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

        /// <summary>
        /// 聚合异常（包含所有失败操作的异常）
        /// </summary>
        public AggregateException AggregateException { get; set; }
    }

    /// <summary>
    /// UI事务执行结果
    /// </summary>
    public class UITransactionResult
    {
        /// <summary>
        /// 事务是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 事务标识符
        /// </summary>
        public string TransactionId { get; set; }

        /// <summary>
        /// 事务名称
        /// </summary>
        public string TransactionName { get; set; }

        /// <summary>
        /// 事务最终状态
        /// </summary>
        public UITransactionStatus FinalStatus { get; set; }

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

        /// <summary>
        /// 事务中包含的更新操作结果
        /// </summary>
        public UIBatchUpdateResult UpdateResults { get; set; }

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

        /// <summary>
        /// 异常信息（如果有）
        /// </summary>
        public Exception Exception { get; set; }
    }

    #endregion

    #region 上下文和配置

    /// <summary>
    /// UI更新上下文
    /// </summary>
    public class UIUpdateContext
    {
        /// <summary>
        /// 上下文标识符
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 上下文名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 目标UI框架
        /// </summary>
        public UIFrameworkType TargetFramework { get; set; } = UIFrameworkType.Auto;

        /// <summary>
        /// 超时时间（毫秒）
        /// </summary>
        public int TimeoutMilliseconds { get; set; } = 5000;

        /// <summary>
        /// 是否启用重试机制
        /// </summary>
        public bool EnableRetry { get; set; } = true;

        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetryCount { get; set; } = 3;

        /// <summary>
        /// 重试间隔（毫秒）
        /// </summary>
        public int RetryIntervalMilliseconds { get; set; } = 100;

        /// <summary>
        /// 是否启用回滚机制
        /// </summary>
        public bool EnableRollback { get; set; } = false;

        /// <summary>
        /// 上下文数据
        /// </summary>
        public IDictionary<string, object> Data { get; set; } = new Dictionary<string, object>();

        /// <summary>
        /// 创建默认上下文
        /// </summary>
        public static UIUpdateContext Default()
        {
            return new UIUpdateContext();
        }

        /// <summary>
        /// 创建指定框架的上下文
        /// </summary>
        public static UIUpdateContext ForFramework(UIFrameworkType framework)
        {
            return new UIUpdateContext { TargetFramework = framework };
        }
    }

    /// <summary>
    /// UI更新服务配置
    /// </summary>
    public class UIUpdateServiceConfiguration
    {
        /// <summary>
        /// 默认UI框架类型
        /// </summary>
        public UIFrameworkType DefaultFramework { get; set; } = UIFrameworkType.Auto;

        /// <summary>
        /// 最大并发更新操作数
        /// </summary>
        public int MaxConcurrentUpdates { get; set; } = 10;

        /// <summary>
        /// 更新队列最大长度
        /// </summary>
        public int MaxQueueLength { get; set; } = 1000;

        /// <summary>
        /// 默认更新超时时间（毫秒）
        /// </summary>
        public int DefaultTimeoutMilliseconds { get; set; } = 5000;

        /// <summary>
        /// 是否启用性能监控
        /// </summary>
        public bool EnablePerformanceMonitoring { get; set; } = true;

        /// <summary>
        /// 是否启用详细日志
        /// </summary>
        public bool EnableVerboseLogging { get; set; } = false;

        /// <summary>
        /// 队列处理间隔（毫秒）
        /// </summary>
        public int QueueProcessingIntervalMilliseconds { get; set; } = 50;

        /// <summary>
        /// 是否启用事务支持
        /// </summary>
        public bool EnableTransactionSupport { get; set; } = true;

        /// <summary>
        /// 最大事务超时时间（毫秒）
        /// </summary>
        public int MaxTransactionTimeoutMilliseconds { get; set; } = 30000;

        /// <summary>
        /// 自动垃圾回收间隔（毫秒）
        /// </summary>
        public int AutoCleanupIntervalMilliseconds { get; set; } = 60000;

        /// <summary>
        /// 创建默认配置
        /// </summary>
        public static UIUpdateServiceConfiguration Default()
        {
            return new UIUpdateServiceConfiguration();
        }

        /// <summary>
        /// 创建高性能配置
        /// </summary>
        public static UIUpdateServiceConfiguration HighPerformance()
        {
            return new UIUpdateServiceConfiguration
            {
                MaxConcurrentUpdates = 20,
                MaxQueueLength = 2000,
                QueueProcessingIntervalMilliseconds = 25,
                EnablePerformanceMonitoring = true,
                EnableVerboseLogging = false
            };
        }

        /// <summary>
        /// 创建调试配置
        /// </summary>
        public static UIUpdateServiceConfiguration Debug()
        {
            return new UIUpdateServiceConfiguration
            {
                EnableVerboseLogging = true,
                EnablePerformanceMonitoring = true,
                DefaultTimeoutMilliseconds = 10000
            };
        }
    }

    #endregion

    #region 事件参数

    /// <summary>
    /// UI更新开始事件参数
    /// </summary>
    public class UIUpdateStartedEventArgs : EventArgs
    {
        public string UpdateId { get; }
        public string UpdateName { get; }
        public UIUpdatePriority Priority { get; }
        public DateTime StartTime { get; }

        public UIUpdateStartedEventArgs(string updateId, string updateName, UIUpdatePriority priority)
        {
            UpdateId = updateId;
            UpdateName = updateName;
            Priority = priority;
            StartTime = DateTime.UtcNow;
        }
    }

    /// <summary>
    /// UI更新完成事件参数
    /// </summary>
    public class UIUpdateCompletedEventArgs : EventArgs
    {
        public string UpdateId { get; }
        public string UpdateName { get; }
        public UIUpdateResult Result { get; }
        public DateTime CompletedTime { get; }

        public UIUpdateCompletedEventArgs(string updateId, string updateName, UIUpdateResult result)
        {
            UpdateId = updateId;
            UpdateName = updateName;
            Result = result;
            CompletedTime = DateTime.UtcNow;
        }
    }

    /// <summary>
    /// UI更新失败事件参数
    /// </summary>
    public class UIUpdateFailedEventArgs : EventArgs
    {
        public string UpdateId { get; }
        public string UpdateName { get; }
        public Exception Exception { get; }
        public DateTime FailedTime { get; }

        public UIUpdateFailedEventArgs(string updateId, string updateName, Exception exception)
        {
            UpdateId = updateId;
            UpdateName = updateName;
            Exception = exception;
            FailedTime = DateTime.UtcNow;
        }
    }

    /// <summary>
    /// UI更新服务状态变化事件参数
    /// </summary>
    public class UIUpdateServiceStatusChangedEventArgs : EventArgs
    {
        public UIUpdateServiceStatus PreviousStatus { get; }
        public UIUpdateServiceStatus CurrentStatus { get; }
        public string Message { get; }
        public DateTime ChangedTime { get; }

        public UIUpdateServiceStatusChangedEventArgs(UIUpdateServiceStatus previousStatus, UIUpdateServiceStatus currentStatus, string message = null)
        {
            PreviousStatus = previousStatus;
            CurrentStatus = currentStatus;
            Message = message;
            ChangedTime = DateTime.UtcNow;
        }
    }

    #endregion
}