using GalaSoft.MvvmLight.Command;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace FLY.Thick.Blowing360.UI
{
    public class PgBlowingExtVmUt : PgBlowingExtVm
    {
        public event PropertyChangedEventHandler PropertyChanged;


        public PgBlowingExtVmUt()
        {
            Cfl = new CalFilmLenUt();

            init_virtualdata();
        }



        /// <summary>
        /// 产生虚拟数据
        /// </summary>
        void init_virtualdata()
        {
            #region 虚拟数据
            int[] thicks = new int[]{
                3518    ,
                3484    ,
                3634    ,
                3620    ,
                3579    ,
                3625    ,
                3635    ,
                3638    ,
                3635    ,
                3606    ,
                3611    ,
                3603    ,
                3590    ,
                3631    ,
                3646    ,
                3647    ,
                3710    ,
                3705    ,
                3607    ,
                3720    ,
                3680    ,
                3637    ,
                3659    ,
                3703    ,
                3860    ,
                3720    ,
                3615    ,
                3810    ,
                3772    ,
                3666    ,
                3614    ,
                3627    ,
                3618    ,
                3502    ,
                3575    ,
                3583    ,
                3615    ,
                3680    ,
                3730    ,
                3840    ,
                3927    ,
                3946    ,
                3923    ,
                3841    ,
                3797    ,
                3865    ,
                3810    ,
                3711    ,
                3754    ,
                3705    ,
                3723    ,
                3699    ,
                3723    ,
                3801    ,
                3786    ,
                3798    ,
                3907    ,
                3945    ,
                3828    ,
                3860    ,
                3846    ,
                3903    ,
                3871    ,
                3844    ,
                3759    ,
                3816    ,
                3886    ,
                3815    ,
                3789    ,
                3876    ,
                3942    ,
                3854    ,
                3842    ,
                3921    ,
                3846    ,
                3835    ,
                3886    ,
                3881    ,
                3820    ,
                3787    ,
                3730    ,
                3617    ,
                3650    ,
                3610    ,
                3536    ,
                3545    ,
                3564    ,
                3584    ,
                3580    ,
                3572    ,
                3571    ,
                3579    ,
                3620    ,
                3634    ,
                3484    ,
                3518    ,
                };
            //辊周长 m
            double RollPerimeter = 0.314;
            //线速度 m/min
            double FilmVelocity = 20;
            //旋转周期
            TimeSpan RInterval = TimeSpan.FromMinutes(6.1);
            //速度震荡周期 m
            double FV_Range = 2;
            TimeSpan DataLen = TimeSpan.FromMinutes(30);

            DateTime dt_begin = DateTime.Now;
            DateTime dt_end = dt_begin + DataLen;

            List<TimeValue> vList = new List<TimeValue>();
            List<TimeValue> tList = new List<TimeValue>();
            List<TimeValue> lList = new List<TimeValue>();

            for (TimeSpan ts = TimeSpan.Zero; ts < DataLen; ts += TimeSpan.FromSeconds(1))
            {
                DateTime dt = dt_begin + ts;
                double min = ts.TotalMinutes;

                //速度以1/2旋转周期变化
                double v = FilmVelocity + Math.Sin(Math.PI * 2 * min / (RInterval.TotalMinutes / 2)) * FV_Range;

                vList.Add(new TimeValue() { Time = dt, Value = v });

                int n = ((int)(min / RInterval.TotalMinutes)) % 2;

                double m = min % RInterval.TotalMinutes;
                int idx = (int)(thicks.Count() * m / RInterval.TotalMinutes);
                if (idx < 0) idx = 0;
                else if (idx > thicks.Count()) idx = thicks.Count() - 1;

                double thick;

                if (n == 0)
                    thick = thicks[idx] / 100.0;
                else
                    thick = thicks[thicks.Count() - 1 - idx] / 100.0;


                tList.Add(new TimeValue()
                {
                    Time = dt,
                    Value = thick
                }) ;

                if (lList.Count() == 0)
                {
                    lList.Add(new TimeValue() { Time = dt_begin + RInterval, Value = thick });
                }
                else if (dt >= lList.Last().Time)
                {
                    lList.Add(new TimeValue() { Time = lList.Last().Time + RInterval, Value = thick });
                }
            }

            List<TimeValue> fList = new List<TimeValue>();
            double sum = 0;
            double avg = vList.Average(tv => tv.Value);

            foreach (var tv in vList)
            {
                sum += (tv.Value - avg) * TimeSpan.FromSeconds(1).TotalMinutes;

                fList.Add(new TimeValue() { Time = tv.Time, Value = sum });
            }
            {
                double min = fList.Min(tv => tv.Value);
                foreach (var tv in vList)
                {
                    tv.Value -= min;
                }
            }

            List<double> setFilmLength = new List<double>();
            foreach (var tv in fList)
            {
                if (tv.Time >= lList[0].Time && tv.Time <= lList[1].Time)
                {
                    setFilmLength.Add(tv.Value);
                }
                else if (tv.Time > lList[1].Time)
                {
                    break;
                }
            }
            Random r = new Random();
            List<double> currFilmLength = new List<double>();
            foreach (var f in setFilmLength)
            {
                currFilmLength.Add(f + r.NextDouble() - 0.5);
            }


            List<List<double>> frames = new List<List<double>>();
            for (int i = 0; i < lList.Count() - 1; i++)
            {
                List<double> frame = new List<double>();
                frame.AddRange(thicks.Select(t => (t + (r.NextDouble() - 0.5) * t * 2 / 100) / 100.0));
                frames.Add(frame);
            }
            #endregion



            //            LimitValues.AddRange(lList);

            for (int i = 0; i < lList.Count(); i++)
            {

                var value = lList[i];
                var axisSection = new AxisSection()
                {
                    StrokeThickness = 1,
                    DataLabel = true,
                    DisableAnimations = true,
                    DataLabelForeground = new SolidColorBrush(Colors.White),
                    Opacity = 1,
                    Stroke = new SolidColorBrush(Colors.Orange),
                };
                if (value.Value == 1)
                    axisSection.Stroke = new SolidColorBrush(Colors.DarkBlue);
                axisSection.Value = value.Time.Ticks;
                LimitSections.Add(axisSection);
            }


            VelocityValues.AddRange(GetLimitedList(vList, 100));
            ThicknessValues.AddRange(GetLimitedList(tList, 100));

            for (int i = 0; i < frames.Count(); i++)
            {
                var frame = frames[i];
                FrameSeries.Add(
                    new LineSeries()
                    {
                        Title = i.ToString(),
                        Values = new ChartValues<double>(frame),
                        LineSmoothness = 1,
                        StrokeThickness = 3,
                        PointGeometrySize = 0
                    });
            }
        }
    }

    public class CalFilmLenUt : CalFilmLen360
    {
        public CalFilmLenUt()
        {
            IsCanDownload = true;
            Msg = "下载中";
            IsDataReady = true;
        }
    }
}