namespace NavisworksTransport.Core.UIUpdate
{
    /// <summary>
    /// UI更新服务接口
    /// 提供统一的UI更新抽象，支持不同UI框架（WPF、WinForms）和线程安全操作
    /// </summary>
    public interface IUIUpdateService
    {
        #region 基础更新操作

        /// <summary>
        /// 执行单个UI更新操作（异步）
        /// </summary>
        /// <param name="update">UI更新操作</param>
        /// <param name="context">更新上下文</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>更新结果</returns>
        Task<UIUpdateResult> ExecuteUpdateAsync(IUIUpdate update, UIUpdateContext context = null, CancellationToken cancellationToken = default);

        /// <summary>
        /// 执行批量UI更新操作（原子性）
        /// </summary>
        /// <param name="updates">UI更新操作集合</param>
        /// <param name="context">更新上下文</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>批量更新结果</returns>
        Task<UIBatchUpdateResult> ExecuteBatchUpdateAsync(IEnumerable<IUIUpdate> updates, UIUpdateContext context = null, CancellationToken cancellationToken = default);

        /// <summary>
        /// 排队UI更新操作（延迟执行）
        /// </summary>
        /// <param name="update">UI更新操作</param>
        /// <param name="priority">操作优先级</param>
        /// <param name="context">更新上下文</param>
        void QueueUpdate(IUIUpdate update, UIUpdatePriority priority = UIUpdatePriority.Normal, UIUpdateContext context = null);

        #endregion

        #region 事务性更新

        /// <summary>
        /// 开始UI更新事务
        /// </summary>
        /// <param name="transactionName">事务名称</param>
        /// <returns>事务句柄</returns>
        IUIUpdateTransaction BeginTransaction(string transactionName = null);

        /// <summary>
        /// 在事务中执行UI更新操作
        /// </summary>
        /// <param name="transaction">事务句柄</param>
        /// <param name="updates">UI更新操作集合</param>
        /// <param name="context">更新上下文</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>事务执行结果</returns>
        Task<UITransactionResult> ExecuteInTransactionAsync(IUIUpdateTransaction transaction, IEnumerable<IUIUpdate> updates, UIUpdateContext context = null, CancellationToken cancellationToken = default);

        #endregion

        #region 状态管理

        /// <summary>
        /// 获取当前是否在UI线程中
        /// </summary>
        bool IsUIThread { get; }

        /// <summary>
        /// 获取当前待处理的更新操作数量
        /// </summary>
        int PendingUpdatesCount { get; }

        /// <summary>
        /// 获取当前正在执行的更新操作数量
        /// </summary>
        int ActiveUpdatesCount { get; }

        /// <summary>
        /// 获取服务当前状态
        /// </summary>
        UIUpdateServiceStatus Status { get; }

        #endregion

        #region 事件

        /// <summary>
        /// UI更新操作开始时触发
        /// </summary>
        event EventHandler<UIUpdateStartedEventArgs> UpdateStarted;

        /// <summary>
        /// UI更新操作完成时触发
        /// </summary>
        event EventHandler<UIUpdateCompletedEventArgs> UpdateCompleted;

        /// <summary>
        /// UI更新操作失败时触发
        /// </summary>
        event EventHandler<UIUpdateFailedEventArgs> UpdateFailed;

        /// <summary>
        /// 服务状态变化时触发
        /// </summary>
        event EventHandler<UIUpdateServiceStatusChangedEventArgs> StatusChanged;

        #endregion

        #region 生命周期

        /// <summary>
        /// 初始化UI更新服务
        /// </summary>
        /// <param name="configuration">服务配置</param>
        Task InitializeAsync(UIUpdateServiceConfiguration configuration = null);

        /// <summary>
        /// 暂停UI更新服务
        /// </summary>
        Task PauseAsync();

        /// <summary>
        /// 恢复UI更新服务
        /// </summary>
        Task ResumeAsync();

        /// <summary>
        /// 关闭UI更新服务并清理资源
        /// </summary>
        Task ShutdownAsync();

        #endregion
    }

    /// <summary>
    /// UI更新操作接口
    /// </summary>
    public interface IUIUpdate
    {
        /// <summary>
        /// 更新操作的唯一标识符
        /// </summary>
        string Id { get; }

        /// <summary>
        /// 更新操作名称
        /// </summary>
        string Name { get; }

        /// <summary>
        /// 更新操作描述
        /// </summary>
        string Description { get; }

        /// <summary>
        /// 操作优先级
        /// </summary>
        UIUpdatePriority Priority { get; }

        /// <summary>
        /// 目标UI框架类型
        /// </summary>
        UIFrameworkType TargetFramework { get; }

        /// <summary>
        /// 是否需要在UI线程上执行
        /// </summary>
        bool RequiresUIThread { get; }

        /// <summary>
        /// 验证更新操作是否可以执行
        /// </summary>
        /// <param name="context">更新上下文</param>
        /// <returns>验证结果</returns>
        UIUpdateValidationResult Validate(UIUpdateContext context);

        /// <summary>
        /// 执行UI更新操作
        /// </summary>
        /// <param name="context">更新上下文</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>执行结果</returns>
        Task<UIUpdateResult> ExecuteAsync(UIUpdateContext context, CancellationToken cancellationToken = default);

        /// <summary>
        /// 回滚UI更新操作（如果支持）
        /// </summary>
        /// <param name="context">更新上下文</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>回滚结果</returns>
        Task<UIUpdateResult> RollbackAsync(UIUpdateContext context, CancellationToken cancellationToken = default);
    }

    /// <summary>
    /// UI更新事务接口
    /// </summary>
    public interface IUIUpdateTransaction : IDisposable
    {
        /// <summary>
        /// 事务唯一标识符
        /// </summary>
        string Id { get; }

        /// <summary>
        /// 事务名称
        /// </summary>
        string Name { get; }

        /// <summary>
        /// 事务状态
        /// </summary>
        UITransactionStatus Status { get; }

        /// <summary>
        /// 事务中包含的更新操作数量
        /// </summary>
        int UpdateCount { get; }

        /// <summary>
        /// 事务开始时间
        /// </summary>
        DateTime StartTime { get; }

        /// <summary>
        /// 提交事务
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        Task<UITransactionResult> CommitAsync(CancellationToken cancellationToken = default);

        /// <summary>
        /// 回滚事务
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        Task<UITransactionResult> RollbackAsync(CancellationToken cancellationToken = default);

        /// <summary>
        /// 添加更新操作到事务
        /// </summary>
        /// <param name="update">UI更新操作</param>
        void AddUpdate(IUIUpdate update);

        /// <summary>
        /// 批量添加更新操作到事务
        /// </summary>
        /// <param name="updates">UI更新操作集合</param>
        void AddUpdates(IEnumerable<IUIUpdate> updates);
    }
}