using LiveCharts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.Base.UI.UiModule
{
    public class FixGraphVmUt : INotifyPropertyChanged
    {
        #region 图表控制
        public double YMax { get; set; } = double.NaN;
        public double YMin { get; set; } = double.NaN;

        public Func<double, string> XFormatter { get; set; }
        public Func<double, string> YFormatter { get; set; }

        public double XMax { get; set; } = double.NaN;

        public ChartValues<double> Values { get; } = new ChartValues<double>();
        #endregion
        #region 界面统计值
        /// <summary>
        /// 平均值
        /// </summary>
        public double Average { get; set; }
        /// <summary>
        /// 最大值
        /// </summary>
        public double Max { get; set; }
        /// <summary>
        /// 最小值
        /// </summary>
        public double Min { get; set; }
        /// <summary>
        /// 3sigma
        /// </summary>
        public double Sigma3 { get; set; }
        #endregion

        #region 状态
        public DateTime Time { get; set; }
        public TimeSpan Interval { get; set; }
        public double Intervalms { get; set; }
        public int Mix { get; set; }

        public bool IsPaused { get; set; }
        #endregion


        /// <summary>
        /// 为了画图快，曲线只能显示最多1000个点
        /// </summary>
        const int TotalValueCountInView = 1000;


        public FixGraphVmUt()
        {
            #region 与数据无关界面参数
            XFormatter = (x) =>
            {
                int index = (int)x;
                double s = Interval.TotalSeconds * index;
                return $"{s:F1}s";
            };

            YFormatter = (y) => y.ToString("F1");
            Interval = TimeSpan.FromTicks((long)(TimeSpan.TicksPerMillisecond * 1.28));
            Intervalms = ((double)(Interval.Ticks)) / TimeSpan.TicksPerMillisecond;
            #endregion
            Random random = new Random();
            List<double> values = new List<double>();
            for (int i = 0; i < 1000; i++)
            {
                double v = (Math.Sin(i * Math.PI / 400.0) * 0.6) * 2 + 72 + (random.NextDouble() - 0.5) * 1;
                values.Add(v);
            }

            Values.AddRange(values);

        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}
