using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Markup;
using DebugToolInstrumentInterface;

namespace QFDT_Components;

public partial class PowerSupply_V2 : UserControl, IComponentConnector
{
	public delegate void SendDebugMessage(MessageLevel level, string message);

	public delegate void PrintCurrent(double Amp);

	private delegate void UpdateCurrentCB(double current);

	private delegate void UpdateCurrentChartCB(double current);

	private const double UpdateInterval = 0.2;

	private int maximumNumX = 100;

	public PrintCurrent PrintCurrentCB;

	public SendDebugMessage DebugMessageCB;

	private ChartArea ca = new ChartArea();

	private Queue<double> currentSeries = new Queue<double>();

	public IPowerSupply ptrPs;

	private Thread measureCurrentThread;

	private Chart chart;

	private volatile bool _stopCurrentThread;

	private double m_Voltage = 3.7;

	private double m_Current = 2.0;

	private int y;

	public void Register_DebugMessageCB(SendDebugMessage me)
	{
		DebugMessageCB = me;
	}

	public PowerSupply_V2()
	{
		InitializeComponent();
		base.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
	}

	public void SetUpChart()
	{
		Dictionary<double, double> dictionary = new Dictionary<double, double>();
		for (int i = 0; i < maximumNumX; i++)
		{
			dictionary.Add((double)(i + 1) * 0.2, 0.0);
		}
		chart = FindName("CurrentChart") as Chart;
		for (int j = 0; j < maximumNumX; j++)
		{
			currentSeries.Enqueue(0.0);
		}
		chart.DataSource = dictionary;
		chart.Series["Series"].XValueMember = "Key";
		chart.Series["Series"].YValueMembers = "Value";
		ca.AxisY.Maximum = m_Current + 1.0;
		ca.AxisY.Interval = (m_Current + 1.0) / 5.0;
		ca.AxisY.MajorTickMark.Interval = (m_Current + 1.0) / 5.0;
		ca.AxisY.MajorGrid.Interval = (m_Current + 1.0) / 5.0;
		ca.AxisY.MajorGrid.LineColor = Color.Green;
		ca.AxisX.MajorGrid.Interval = 5.0;
		ca.AxisX.MajorGrid.LineColor = Color.Green;
		ca.AxisY.Minimum = 0.0;
		ca.BackColor = Color.Black;
		chart.Series["Series"].BorderWidth = 3;
		chart.Series["Series"].Color = Color.Green;
		chart.ChartAreas["Series"] = ca;
	}

	private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
	{
		if (ptrPs != null)
		{
			_stopCurrentThread = true;
		}
	}

	public void SetTxOnOff(bool OnOff)
	{
		if (OnOff)
		{
			On_Click(null, null);
		}
		else
		{
			OffV2_Click(null, null);
		}
	}

	private void On_Click(object sender, RoutedEventArgs e)
	{
		if (ptrPs != null)
		{
			ptrPs.TurnOnOff(OnOff: true);
			ptrPs.SetVoltage(Convert.ToDouble(txtVolt.Text.ToString()));
			ptrPs.SetCurrentLimit(Convert.ToDouble(txtAmp.Text.ToString()));
			DebugMessageCB(MessageLevel.SUCCESS, base.Resources["TurnSupplyOn"].ToString());
			measureCurrentThread = new Thread(MeasurecurrentThread);
			measureCurrentThread.Name = "Measure Current";
			_stopCurrentThread = false;
			measureCurrentThread.Start();
		}
	}

	private void MeasurecurrentThread()
	{
		double num = 200.0;
		if (ptrPs == null)
		{
			return;
		}
		double num2 = 0.0;
		while (!_stopCurrentThread)
		{
			num2 = ptrPs.GetCurrent();
			UpdateCurrent(num2);
			UpdateChartValue(num2);
			if (PrintCurrentCB != null)
			{
				PrintCurrentCB(num2);
			}
			Thread.Sleep((int)num);
		}
	}

	private void OffV2_Click(object sender, RoutedEventArgs e)
	{
		if (ptrPs != null)
		{
			ptrPs.TurnOnOff(OnOff: false);
			_stopCurrentThread = true;
			DebugMessageCB(MessageLevel.SUCCESS, base.Resources["TurnSupplyOff"].ToString());
		}
	}

	private void Measure_Click(object sender, RoutedEventArgs e)
	{
		if (ptrPs != null)
		{
			txtCurrent.Text = ptrPs.GetCurrent().ToString();
		}
	}

	private void Amp_Click(object sender, RoutedEventArgs e)
	{
		if (ptrPs != null)
		{
			ptrPs.SetCurrentLimit(Convert.ToDouble(txtAmp.Text.ToString()));
			double num = Convert.ToDouble(txtAmp.Text.ToString());
			ca.AxisY.Maximum = num + 1.0;
			ca.AxisY.Interval = (num + 1.0) / 5.0;
			ca.AxisY.MajorGrid.Interval = (num + 1.0) / 5.0;
			ca.AxisY.MajorTickMark.Interval = (num + 1.0) / 5.0;
			DebugMessageCB(MessageLevel.SUCCESS, base.Resources["SetCurrentLimit"].ToString() + " " + txtAmp.Text.ToString());
		}
	}

	private void Voltage_Click(object sender, RoutedEventArgs e)
	{
		if (ptrPs != null)
		{
			ptrPs.SetVoltage(Convert.ToDouble(txtVolt.Text.ToString()));
			DebugMessageCB(MessageLevel.SUCCESS, base.Resources["SetVoltage"].ToString() + " " + txtVolt.Text.ToString());
		}
	}

	public void UpdateCurrent(double current)
	{
		if (txtCurrent.Dispatcher.CheckAccess())
		{
			txtCurrent.Text = $"{current:0.###}";
			return;
		}
		UpdateCurrentCB method = UpdateCurrent;
		base.Dispatcher.BeginInvoke(method, current);
	}

	private void UpdateChartValue(double current)
	{
		if (base.Dispatcher.CheckAccess())
		{
			currentSeries.Dequeue();
			currentSeries.Enqueue(current);
			y++;
			if (y % 5 == 0)
			{
				chart.Series[0].Points.Clear();
				for (int i = 0; i < maximumNumX; i++)
				{
					chart.Series[0].Points.AddXY((double)(i + 1) * 0.2, currentSeries.ElementAt(i));
				}
			}
		}
		else
		{
			UpdateCurrentChartCB method = UpdateChartValue;
			base.Dispatcher.BeginInvoke(method, current);
		}
	}

	private void On_Unloaded(object sender, RoutedEventArgs e)
	{
	}

	public void SetVoltnCurrentLimitOnUI(double voltage, double currentLimitA)
	{
		txtVolt.Text = voltage.ToString();
		m_Voltage = voltage;
		txtAmp.Text = currentLimitA.ToString();
		m_Current = currentLimitA;
	}
}
