using System;
using NavisworksTransport.Core;

namespace NavisworksTransport.UI.WPF.ViewModels
{
    /// <summary>
    /// 物流控制面板主ViewModel
    /// </summary>
    public class LogisticsControlViewModel : ViewModelBase
    {
        #region 私有字段

        private string _statusText;
        private bool _isProcessing = false;
        
        // UI状态管理和命令框架
        private readonly UIStateManager _uiStateManager;
        private readonly NavisworksTransport.Commands.CommandManager _commandManager;

        #endregion

        #region 公共属性


        /// <summary>
        /// 状态文本
        /// </summary>
        public string StatusText
        {
            get => _statusText;
            set => SetProperty(ref _statusText, value);
        }

        /// <summary>
        /// 是否正在处理中（用于统一状态栏进度条显示）
        /// </summary>
        public bool IsProcessing
        {
            get => _isProcessing;
            set => SetProperty(ref _isProcessing, value);
        }

        private double _progressPercentage = 0;
        
        /// <summary>
        /// 进度百分比（0-100，-1表示不确定进度）
        /// </summary>
        public double ProgressPercentage
        {
            get => _progressPercentage;
            set 
            { 
                if (SetProperty(ref _progressPercentage, value))
                {
                    OnPropertyChanged(nameof(IsIndeterminateProgress));
                }
            }
        }

        /// <summary>
        /// 是否使用不确定进度模式
        /// </summary>
        public bool IsIndeterminateProgress => ProgressPercentage < 0;

        #endregion

        #region 构造函数

        public LogisticsControlViewModel() : base()
        {
            try
            {
                // 获取UI状态管理器和命令管理器实例
                _uiStateManager = UIStateManager.Instance;
                _commandManager = NavisworksTransport.Commands.CommandManager.Instance;
                
                // 验证关键组件是否正常初始化
                if (_uiStateManager == null)
                {
                    LogManager.Error("UIStateManager初始化失败");
                    throw new InvalidOperationException("UIStateManager初始化失败");
                }
                
                if (_commandManager == null)
                {
                    LogManager.Error("CommandManager初始化失败");
                    throw new InvalidOperationException("CommandManager初始化失败");
                }

                // 初始化状态
                StatusText = "插件已就绪";
                
                LogManager.Info("LogisticsControlViewModel构造函数执行完成");
            }
            catch (Exception ex)
            {
                LogManager.Error($"LogisticsControlViewModel构造函数异常: {ex.Message}", ex);
                
                // 在构造函数中尽量保证对象处于可用状态
                StatusText = "初始化失败，请检查日志";
                throw;
            }
        }

        #endregion

        #region 统一状态栏方法

        /// <summary>
        /// 更新统一状态栏信息（简单版本，不显示进度条）
        /// </summary>
        /// <param name="message">状态消息</param>
        public void UpdateStatus(string message)
        {
            SafeExecute(() =>
            {
                StatusText = message ?? "就绪";
                IsProcessing = false; // 简单状态更新不显示进度条
            }, "更新统一状态栏", runOnUIThread: true);
        }

        /// <summary>
        /// 更新统一状态栏信息（完整版本）
        /// </summary>
        /// <param name="message">状态消息</param>
        /// <param name="progress">进度值 (0-100)，默认不显示进度条</param>
        /// <param name="isProcessing">是否正在处理中（显示进度条动画）</param>
        public void UpdateStatus(string message, double progress = -1, bool? isProcessing = null)
        {
            SafeExecute(() =>
            {
                StatusText = message ?? "就绪";
                if (progress >= 0)
                {
                    ProgressPercentage = Math.Max(0, Math.Min(100, progress));
                }
                if (isProcessing.HasValue)
                {
                    IsProcessing = isProcessing.Value;
                }
            }, "更新统一状态栏", runOnUIThread: true);
        }

        /// <summary>
        /// 更新统一状态栏信息（支持百分比进度）
        /// </summary>
        /// <param name="message">状态消息</param>
        /// <param name="percentage">进度百分比 (0-100)</param>
        public void UpdateStatus(string message, double percentage)
        {
            SafeExecute(() =>
            {
                StatusText = message ?? "就绪";
                ProgressPercentage = Math.Max(0, Math.Min(100, percentage));
                IsProcessing = true; // 有百分比进度时显示进度条
            }, "更新统一状态栏（百分比模式）", runOnUIThread: true);
        }

        /// <summary>
        /// 重置状态栏到默认状态
        /// </summary>
        public void ResetStatus()
        {
            UpdateStatus("插件已就绪");
        }

        #endregion
    }
}