using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using MathNet.Numerics;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace FLY.Thick.FilmCasting.UI.UiModule
{
    public class CompareGraphVmUt : CompareGraphVm
    {

        public CompareGraphVmUt()
        {

            SimuData();
        }


        void SimuData() 
        {
            graphparam = new CompareGraphItemParam
            {
                IsPercent = true,
                IsAutoTarget = true,
                YRangePercent = 3,
                IsLineMode = false
            };
            profileParam = new Common.FilmCastingProfileParam
            {
                ScanRange = new RangeStruct(10, 110),
                DataRange = new RangeStruct(30, 90),
                Target = 150,
                TolerancePercent = 0.02
            };
            #region 数据


            double target = profileParam.Target;
            double tolerance = target * profileParam.TolerancePercent;
            double yrangepercent = graphparam.YRangePercent;

            int nbolts = 120;
            double[] datas = new double[nbolts];
            Random random = new Random();
            for (int i = 0; i < nbolts; i++)
            {
                if(i< profileParam.DataRange.Begin)
                    datas[i] = double.NaN;
                else if (i > profileParam.DataRange.End)
                    datas[i] = double.NaN;
                else
                    datas[i] = (Math.Sin(i * Math.PI / nbolts) * 3) * tolerance + target + (random.NextDouble() - 0.5) * 1 - 6;
            }

            double[] datas2 = new double[nbolts];
            for (int i = 0; i < nbolts; i++)
            {
                if (!double.IsNaN(datas[i]))
                    datas2[i] = datas[i] + (random.NextDouble() - 0.5) * 2;
                else
                    datas2[i] = datas[i];
            }
            #endregion
            #region 界面数据
            UpdateSeriesType();
            UpdateX();

            XStep = 10;
            Mix = 2;
            Time = DateTime.Now;
            EndTime = DateTime.Now;
            Id = 312312;

            FilmWidth = profileParam.DataRange.Width * 25.4 * (1-0.03);
            Velocity = 40.1;
            Position = 800.4;

            DataBegin = profileParam.DataRange.Begin;
            DataEnd = profileParam.DataRange.End;

            Values.AddRange(datas);
            Values2.AddRange(datas2);

            double[] values = datas;

            List<double> list = new List<double>();
            for (int i = profileParam.DataRange.Begin; i <= profileParam.DataRange.End; i++)
            {
                if (i < 0)
                    i = 0;
                if (i >= values.Count())
                    break;
                double v = values[i];
                if (!double.IsNaN(v))
                    list.Add(values[i]);
            }

            if (list.Count() > 0)
            {
                Average = list.Average();
                Max = list.Max();
                Min = list.Min();
                Sigma2 = list.Sigma() * 2;
            }
            else
            {
                Average = double.NaN;
                Max = double.NaN;
                Min = double.NaN;
                Sigma2 = double.NaN;
            }


            if (graphparam.IsPercent)
            {
                MaxText = $"+{((Max - Average) / Average) * 100.0:F1}%";
                MinText = $"-{((Average - Min) / Average) * 100.0:F1}%";
                Sigma2Text = $"{(Sigma2 / Average) * 100.0:F1}%";
            }
            else
            {
                MaxText = $"{Max:F1}";
                MinText = $"{Min:F1}";
                Sigma2Text = $"{Sigma2:F1}";
            }

            if (graphparam.IsPercent || graphparam.IsAutoTarget)
            {
                Target = Average;
            }
            else
            {
                Target = target;
            }

            ToleranceYMax = Target + tolerance;
            ToleranceYMin = Target - tolerance;
            Tolerance2YMax = Target + tolerance * 2;
            Tolerance2YMin = Target - tolerance * 2;
            YMax = Target + tolerance * yrangepercent;
            YMin = Target - tolerance * yrangepercent;

            #endregion
        }

    }
}
