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

namespace WSDDBBrowser.Core
{
    public class RecordCollection : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public string DeviceID { get; set; }
        public string DeviceName { get; set; }
        public double Target { get; set; }
        public double Tolerance { get; set; }

        /// <summary>
        /// 显示
        /// </summary>
        public bool IsVisibile { get; set; }

        public Brush ColorBrush { get; set; }
        public ChartValues<Record> Values { get; } = new ChartValues<Record>();

        public double YMax
        {
            get
            {
                if (double.IsNaN(Target) || double.IsNaN(Tolerance) || Tolerance <= 0)
                {
                    return double.NaN;
                }
                else
                {
                    return Target + Tolerance * 2;
                }
            }
        }
        public double YMin
        {
            get
            {
                if (double.IsNaN(Target) || double.IsNaN(Tolerance) || Tolerance <= 0)
                {
                    return double.NaN;
                }
                else
                {
                    return Target - Tolerance * 2;
                }
            }
        }

        public double ToleranceMax
        {
            get
            {
                if (double.IsNaN(Target) || double.IsNaN(Tolerance) || Tolerance <= 0)
                {
                    return double.NaN;
                }
                else
                {
                    return Target + Tolerance;
                }
            }
        }
        public double ToleranceMin
        {
            get
            {
                if (double.IsNaN(Target) || double.IsNaN(Tolerance) || Tolerance <= 0)
                {
                    return double.NaN;
                }
                else
                {
                    return Target - Tolerance;
                }
            }
        }

        /// <summary>
        /// 最大值
        /// </summary>
        public double Max { get; set; }
        /// <summary>
        /// 最小值
        /// </summary>
        public double Min { get; set; }
        /// <summary>
        /// 平均值
        /// </summary>
        public double Average { get; set; }
        /// <summary>
        /// 3σ
        /// </summary>
        public double Sigma3{ get; set; }

        /// <summary>
        /// 超过限制了
        /// </summary>
        public bool IsError
        {
            get {
                if (Average > ToleranceMax)
                    return true;
                else if (Average < ToleranceMin)
                {
                    return true;
                }
                else {
                    return false;
                }
            }
        }

        public object Mapper { get; private set; }
        public Func<double, string> XFormatter { get; set; }
        public RecordCollection()
        {
            Mapper = Mappers.Xy<Record>()
                .X((value, index) => index)
                .Y(value => value.MValue);

            XFormatter = (x) =>
            {
                int index = (int)x;
                if (index < Values.Count() && index >= 0)
                    return $"{Values[index].ProcessLen:F2}码\n{Values[index].Time:HH:mm}";
                else
                    return "";
            };
        }
    }
    public class Record
    {
        public DateTime Time { get; set; }
        public double ProcessLen { get; set; }
        public double MValue { get; set; }
    }
}
