using System;

namespace QC.QMSCT;

public class PanelStat
{
	public enum TestResult
	{
		Passed,
		Failed
	}

	private DateTime totalTestTime;

	private DateTime startTime;

	private DateTime completionTime;

	private DateTime lastCompletionTime = DateTime.Now;

	private int devicesTested;

	private int devicesPassed;

	private int devicesFailed;

	public void DeviceStartedExecution()
	{
		startTime = DateTime.Now;
		devicesTested++;
	}

	public void DeviceCompleted(TestResult state)
	{
		lastCompletionTime = DateTime.Now;
		completionTime = DateTime.Now;
		long ticks = completionTime.Subtract(startTime).Ticks;
		ticks += totalTestTime.Ticks;
		DateTime dateTime = new DateTime(ticks);
		totalTestTime = dateTime;
		switch (state)
		{
		case TestResult.Passed:
			devicesPassed++;
			break;
		case TestResult.Failed:
			devicesFailed++;
			break;
		}
	}

	public void GetYieldStats(out double yield, out int passed, out int failed)
	{
		yield = (passed = (failed = 0));
		passed = devicesPassed;
		failed = devicesFailed;
		if (devicesTested > 0)
		{
			yield = (double)passed / (double)devicesTested;
			yield *= 100.0;
		}
	}

	public void GetTestTime(out double mins, out double secs)
	{
		TimeSpan timeSpan = completionTime.Subtract(startTime);
		mins = timeSpan.Minutes;
		secs = timeSpan.Seconds;
	}

	public void GetAvgTestTime(out double mins, out double secs)
	{
		long num = totalTestTime.Ticks;
		if (devicesTested > 0)
		{
			num /= devicesTested;
		}
		DateTime dateTime = new DateTime(num);
		mins = dateTime.Minute;
		secs = dateTime.Second;
	}

	public void GetTotalUpTime(out double hours, out double mins)
	{
		hours = totalTestTime.Hour;
		mins = totalTestTime.Minute;
	}

	public double TimeElapsedSinceLastTest()
	{
		return DateTime.Now.Subtract(lastCompletionTime).TotalSeconds;
	}

	public void ResetLastCompletionTime()
	{
		lastCompletionTime = DateTime.Now;
	}

	public void ResetStats()
	{
		DateTime dateTime = new DateTime(0L);
		totalTestTime = dateTime;
		startTime = DateTime.Now;
		completionTime = DateTime.Now;
		lastCompletionTime = DateTime.Now;
		devicesTested = 0;
		devicesPassed = 0;
		devicesFailed = 0;
	}
}
