using System.Diagnostics;
using System.Threading;

namespace RFCalibration_CSharp;

internal class Worker
{
	public class TestObject
	{
		public string m_testName;

		public int m_index;
	}

	public int m_DUT_Index = 1;

	public string UUTID;

	private volatile bool _shouldStop;

	public int time = 5;

	private RFCalServer m_rfcal;

	public MainForm m_form;

	public TestObject[] m_TestObjects;

	public int m_iterations;

	public void DoWork()
	{
		Process.GetCurrentProcess();
		if (m_rfcal == null)
		{
			return;
		}
		m_rfcal.SetEnvironmentVar("SN", UUTID);
		m_rfcal.SetEnvironmentVar("DUT_INDEX", m_DUT_Index.ToString());
		m_rfcal.SetEnvironmentVar("Multi_DUT_Index", m_DUT_Index.ToString());
		if (m_TestObjects == null)
		{
			return;
		}
		_shouldStop = false;
		for (int i = 0; i < m_iterations; i++)
		{
			m_form.PrintMemoryUsage(m_DUT_Index, "Iteration " + i + " Started");
			m_form.ClearLogTextBoxes();
			for (int j = 0; j < m_TestObjects.Length; j++)
			{
				if (_shouldStop)
				{
					break;
				}
				m_form.SetStatusText(m_DUT_Index, m_TestObjects[j].m_testName + "...");
				short num = m_rfcal.ExecuteTestId(m_TestObjects[j].m_index);
				m_form.SetStatusText(m_DUT_Index, "Status " + num);
			}
			m_form.PrintMemoryUsage(m_DUT_Index, "Iteration " + i + " Stopped");
		}
		_shouldStop = true;
		if (m_DUT_Index == 1)
		{
			m_form.EnableExecute(bEnable: true);
		}
		else
		{
			m_form.EnableExecute2(bEnable: true);
		}
	}

	public void PlotUsage()
	{
		if (m_rfcal != null && m_TestObjects != null)
		{
			do
			{
				Thread.Sleep(1000);
				m_form.PlotMemory(Process.GetCurrentProcess().PrivateMemorySize64, Process.GetCurrentProcess().PeakVirtualMemorySize64, Process.GetCurrentProcess().PagedSystemMemorySize64, Process.GetCurrentProcess().PagedMemorySize64, Process.GetCurrentProcess().NonpagedSystemMemorySize64);
			}
			while (!_shouldStop);
			m_form.PlotMemory(Process.GetCurrentProcess().PrivateMemorySize64, Process.GetCurrentProcess().PeakVirtualMemorySize64, Process.GetCurrentProcess().PagedSystemMemorySize64, Process.GetCurrentProcess().PagedMemorySize64, Process.GetCurrentProcess().NonpagedSystemMemorySize64);
		}
	}

	public void RequestStop()
	{
		_shouldStop = true;
	}

	public void SetServer(ref RFCalServer testServer)
	{
		m_rfcal = testServer;
	}
}
