using Prism.Commands;
using System.Collections.ObjectModel;
using System.Windows.Input;
using UIShare.PubEvent;
using UIShare.ViewModelBase;

namespace CurveModule.ViewModels.Dialogs
{
    /// <summary>
    /// 曲线统计结果弹窗 ViewModel。
    /// </summary>
    public class CurveStatisticsViewModel : DialogViewModelBase
    {
        #region 属性

        private string _title = "曲线数学统计";
        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }

        public ObservableCollection<CurveStatisticsResult> Results { get; } = new();

        #endregion

        #region 命令
        public ICommand CloseCommand { get; }
        #endregion

        public CurveStatisticsViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            CloseCommand = new DelegateCommand(Close);
        }

        private void Close()
        {
            RequestClose.Invoke(ButtonResult.OK);
        }

        #region Prism Dialog 规范

        public override void OnDialogClosed()
        {
            _eventAggregator.GetEvent<OverlayEvent>().Publish(false);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            _eventAggregator.GetEvent<OverlayEvent>().Publish(true);
            if (parameters.ContainsKey("Results"))
            {
                var list = parameters.GetValue<ObservableCollection<CurveStatisticsResult>>("Results");
                Results.Clear();
                foreach (var item in list)
                    Results.Add(item);
            }
        }

        #endregion
    }

    /// <summary>
    /// 单条曲线的数学统计结果。
    /// </summary>
    public class CurveStatisticsResult
    {
        /// <summary>曲线名称</summary>
        public string DisplayName { get; set; } = string.Empty;

        /// <summary>数据点数</summary>
        public int PointCount { get; set; }

        /// <summary>最大值</summary>
        public double Max { get; set; }

        /// <summary>最小值</summary>
        public double Min { get; set; }

        /// <summary>平均值</summary>
        public double Average { get; set; }

        /// <summary>线性回归斜率（单位/秒）</summary>
        public double Slope { get; set; }

        /// <summary>梯形积分值（单位·秒）</summary>
        public double Integral { get; set; }

        public string MaxText => Max.ToString("G6");
        public string MinText => Min.ToString("G6");
        public string AverageText => Average.ToString("G6");
        public string SlopeText => Slope.ToString("G6");
        public string IntegralText => Integral.ToString("G6");
    }
}
