using mseedChart.Main.Models;
using mseedChart.Main.Views;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace mseedChart.Main.ViewModels
{
    public class ViewAViewModel : BindableBase
    {
        public ViewAViewModel(ChartPlotViewModel chartPlotViewModel)
        {
            this._chartPlotViewModel = chartPlotViewModel;
            Message = "View A from your Prism Module";
            Timer time = new Timer(1000);
            time.Elapsed += Time_Elapsed;
            time.Start();

        }
        private string _message;
        private ChartPlotViewModel _chartPlotViewModel;
        private BlockingCollection<List<double[]>> queueDatas = new BlockingCollection<List<double[]>>(100);

        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }
        public DelegateCommand<object> LoadedCommand => new(Loaded);

        List<LineDatas> lines = new List<LineDatas>();
        private void Loaded(object obj)
        {
            var chart = obj as ChartPlotView;
            _chartPlotViewModel = chart.DataContext as ChartPlotViewModel;
            DirectoryInfo root = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "data");
            FileInfo[] files = root.GetFiles();
            List<string> SHE = new List<string>();
            int step = 500;
            for (int i = 0; i < 24; i++)
            {
                foreach (var file in files)
                {
                    SHE = File.ReadAllLines(file.FullName).ToList();
                    //第一个非数据移除掉
                    SHE.RemoveAt(0);
                    var data = SHE.Select(x => Convert.ToDouble(x)+step*i);
                    lines.Add(new LineDatas
                    {
                        Lable = "line" + file.Name,
                        YData = data.ToArray(),
                        //  XData = DateTime.Now.ToOADate()
                    });

                }
            }
            _chartPlotViewModel.InializeChartPlotChange?.Invoke(lines);
            Task.Run(() =>
            {
                while (true)
                {

                    var data = lines.Select(x => x.YData).ToList();
                    for (int i = 0; i < 4; i++)
                    {
                        List<double[]> values = new List<double[]>();
                        foreach (var item in data)
                        {
                            var SH= item.Skip(i * 500).Take(500).ToArray();
                            values.Add(SH);
                        }
                        queueDatas.Add(values);
                    }
                    Debug.WriteLine("start:{0}",DateTime.Now);
                    System.Threading.Thread.Sleep(100);
                }
            });
        }

        private void Time_Elapsed(object sender, ElapsedEventArgs e)
        {

            _chartPlotViewModel.PlotLineDataChanged?.Invoke(queueDatas.Take());
        }
    }
}
