using FLY.Thick.FilmCasting.Server.Model;
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;
            graphparam.Mix = 100;
            graphparam.CellMix = 3;


            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++)
            {
                if (i < profileParam.DataRange.Begin - 3)
                {
                    datas[i] = 0 + (random.NextDouble() - 0.5) * 1;
                }
                else if (i >= profileParam.DataRange.End + 3)
                {
                    datas[i] = 0 + (random.NextDouble() - 0.5) * 1;
                }
                else
                {
                    datas[i] = Math.Sin(i * Math.PI / nbolts) * 3 * tolerance + target + (random.NextDouble() - 0.5) * 1 - 6;
                }
            }

            DateTime time = DateTime.Now - TimeSpan.FromSeconds(50 * 0.5);
            long id = 1;
            double velocity = 30;
            double filmPosition = 0;
            for (int j = 0; j < 50; j++)
            {
                var datas2 = (double[])datas.Clone();
                for (int i = 0; i < datas2.Count(); i++) {
                    datas2[i] += (random.NextDouble() - 0.5) * 3;
                }
                DateTime endTime = time + TimeSpan.FromSeconds(0.5);

                Lc_ScanData lc_ScanData = new Lc_ScanData()
                {
                    ID = id,
                    ProfileID = 123,
                    Time = time,
                    EndTime = endTime,
                    DataBegin = profileParam.DataRange.Begin,
                    DataEnd = profileParam.DataRange.End,
                    Thicks = datas2,
                    FilmPosition = filmPosition,
                    FilmVelocity = velocity,
                    FilmWidth = 1400,
                };
                scanDatas.Add(lc_ScanData);
                id++;
                time = endTime;
                filmPosition += 0.5 * velocity;
            }

            Averages = datas;


            #endregion
            #region 界面数据

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


            UpdateSeriesType();
            UpdateX();

            XStep = 10;
            Mix = 2;
            IsLineMode = graphparam.IsLineMode;

            UpdateAverage();
            UpdateMaxMinText();

            
            UpdateValues();
            UpdateCellValues();
            UpdateY();

            #endregion
        }
    }
}
