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 ScanMmGraphVmUt : ScanMmGraphVm
    {

        public ScanMmGraphVmUt()
        {

            SimuData();
        }


        void SimuData() 
        {
            graphParamMm = new ScanMmGraphItemParam();
            graphParamMm.IsXShowBoltNo = true;
            graphparam = graphParamMm;
            graphparam.IsPercent = true;
            graphparam.IsAutoTarget = true;
            graphparam.YRangePercent = 3;
            graphparam.IsLineMode = false;

            profileParam = new Common.FilmCastingProfileParam();
            profileParam.ScanRange = new RangeStruct(10, 110);
            profileParam.DataRange = new RangeStruct(30, 90);
            profileParam.Target = 150;
            profileParam.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++)
            {
                datas[i] = (Math.Sin(i * Math.PI / nbolts) * 3) * tolerance + target + (random.NextDouble() - 0.5) * 1 - 6;
            }

            for (int i = 0; i < profileParam.DataRange.Begin; i++)
            {
                datas[i] = double.NaN;
            }
            for (int i = profileParam.DataRange.End; i < nbolts; i++)
            {
                datas[i] = double.NaN;
            }

            #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;

            FilmBeginOfBoltIndex = profileParam.DataRange.Begin;
            FilmEndOfBoltIndex = profileParam.DataRange.End;
            SelectedOfBoltIndex = FilmBeginOfBoltIndex + 6;
            SelectedWidthOfBoltIndex = 30;
            SelectedWidthMm = 312;



            Values.Clear();
            Values.AddRange(datas);


            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
        }

    }
}
