using OxyPlot;
using OxyPlot.Axes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VSview
{
    public class Graph : INotifyPropertyChanged
    {
        private object _lock = new object();
        public event PropertyChangedEventHandler PropertyChanged;


        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public MainViewModel ViewModel;

        public double Leak;
        public double Pressure;


        public List<DataPoint> Leakrate { get; set; }
        public List<DataPoint> Testpressure { get; set; }

        //Date time axis min and max
        private double _XaxisMax;
        public double XaxisMax
        {
            get { return _XaxisMax; }
            set { _XaxisMax = value; OnPropertyChanged("XaxisMax"); }
        }
        private double _XaxisMin;
        public double XaxisMin
        {
            get { return _XaxisMin; }
            set { _XaxisMin = value; OnPropertyChanged("XaxisMin"); }
        }

        //Time variable
        private DateTime Time;
        //Graph thread
        private Thread GraphThread;

        public Graph(MainViewModel viewModel)
        {
            ViewModel = viewModel;

            Time = DateTime.Now;
            Leakrate = new List<DataPoint>();
            Testpressure = new List<DataPoint>();


            XaxisMax = DateTimeAxis.ToDouble(Time.AddSeconds(55));
            XaxisMin = DateTimeAxis.ToDouble(Time.AddSeconds(-5));


            GraphThread = new Thread(UpdatePlot)
            {
                IsBackground = true
            };
            GraphThread.Start();
        }

        /// <summary>
        /// Formater for the Y axes
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string Valueformatter(double d)
        {
            if (d < 1)
            {
                return d.ToString("0.0#E0");
            }
            else
            {
                return String.Format("{0}", d);
            }
        }

        private void AddPoints()
        {
            Time = DateTime.Now;
            Random rnd = new Random();

            Leakrate.Add(new DataPoint(DateTimeAxis.ToDouble(Time), 0.0001 * rnd.NextDouble()));//Leak));
            Testpressure.Add(new DataPoint(DateTimeAxis.ToDouble(Time), 0.01 * rnd.NextDouble()));// Pressure));


           /* for (int i = 4; i < 8; i++)
            {
                if ((int)ViewModel.DataBindings.GraphSeriesValues[i].Value == 1)
                {
                    ViewModel.DataBindings.GraphSeriesValues[i - 4].Value = Valueformatter(Leakrate.Last<DataPoint>().Y);
                }
                else if ((int)ViewModel.DataBindings.GraphSeriesValues[i].Value == 2)
                {
                    ViewModel.DataBindings.GraphSeriesValues[i - 4].Value = Valueformatter(Testpressure.Last<DataPoint>().Y);
                }
            }*/
        }

        /// <summary>
        /// Removes points from the series
        /// </summary>
        private void RemovePoints()
        {
            if (Leakrate.Count > 1000)
            {
                Leakrate.RemoveAt(0);
            }
        }


        public void UpdatePlot()
        {
            while (true)
            {
                lock (_lock)
                {
                    RemovePoints();
                    AddPoints();
                    Time = DateTime.Now;

                    XaxisMax = DateTimeAxis.ToDouble(Time.AddSeconds(5));
                    XaxisMin = DateTimeAxis.ToDouble(Time.AddSeconds(-50));

                    Thread.Sleep(500);
                }
            }

        }



    }
}
