using Misc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.FilmCasting.UI.UiModule
{
    public class GiantMixScanGraph2VmUt : GiantMixScanGraph2Vm
    {
        public GiantMixScanGraph2VmUt()
        {

            SimuData();
        }
        void SimuData()
        {
            graphparam = new GiantMixScanGraph2ItemParam();
            graphparam.IsPercent = true;
            graphparam.IsAutoTarget = true;
            graphparam.YRangePercent = 3;
            graphparam.IsLineMode = true;

            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];
            double[] sigmas = 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;
                sigmas[i] = Math.Sin(4*i * Math.PI / nbolts) * tolerance * 0.3;
            }

            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;
            }
            Averages = datas;
            Sigmas = sigmas;
            #endregion
            #region 界面数据

            UpdateSeriesType();
            UpdateX();

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

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


            ClearValues();
            UpdateValues(); 


            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
        }
    }
}
