using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using OxyPlot.Series;
using OxyPlot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;

namespace GettingStarted.ViewModels
{
    internal partial class MainViewModel3 : ObservableObject
    {
        [ObservableProperty]
        private PlotModel _myModel;


        [ObservableProperty]
        private LineSeries _lineSeries;


        public MainViewModel3()
        {
            MyModel = new PlotModel() { Title = "Getting Started with RT Plot" };
            LineSeries = new LineSeries() { Title = "Dynamic Data" };

            MyModel.Series.Add(LineSeries);

            _timer = new System.Timers.Timer();
            _timer.Interval = 50;
            _timer.Elapsed -= _timer_Elapsed;
            _timer.Elapsed += _timer_Elapsed;
        }

        private int _lastX = 0;
        private void _timer_Elapsed(object? sender, ElapsedEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                LineSeries.Points.Add(new DataPoint(_lastX, Math.Sin(_lastX * 0.1)));
                MyModel.InvalidatePlot(true);
                _lastX++;
            });
        }

        private bool _isTimerStarted = false;
        private System.Timers.Timer _timer;

        [RelayCommand]
        private void FlipTimer()
        {
            if (!_isTimerStarted)
            {
                _timer.Start();
                _isTimerStarted = true;
            }
            else
            {
                _timer.Stop();
                _isTimerStarted = false;
            }
        }
    }
}
