using ScottPlot;
using ScottPlot.Plottable;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectManagementSystem.UI
{
    public partial class SignalMonitorUI : UserControl, IDisplayUI
    {
        readonly double[] signalValues = new double[60];

        private FormsPlot formsPlot1 = new FormsPlot();
        private SignalPlot signalPlot;

        public string PlotTitle { get; set; }

        public SignalMonitorUI()
        {
            InitializeComponent();
        }

        public bool Init()
        {
            formsPlot1.Dock = DockStyle.Fill;
            this.panel1.Controls.Add(formsPlot1);

            formsPlot1.Plot.Title(PlotTitle);
            signalPlot = formsPlot1.Plot.AddSignal(signalValues);
            signalPlot.StepDisplay = true;
            signalPlot.MarkerSize = 0;

            return true;
        }

        public void ClearSignal()
        {
            for (int i = 0; i < signalValues.Length; i++)
            {
                signalValues[i] = signalValues[i + 1];
            }
        }

        public void AddSignal(bool value)
        {
            AddSignal(value ? 1 : 0);
        }

        public void AddSignal(double value)
        {
            int maxIndex = signalValues.Length - 1;
            for (int i = 0; i < maxIndex; i++)
            {
                signalValues[i] = signalValues[i + 1];
            }

            signalValues[maxIndex] = value;
        }

        public void UpdateDisplay()
        {
            formsPlot1.Render();
            formsPlot1.Plot.AxisAuto();
        }
    }
}
