using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using QC.QsprShared.Trace;
using QC.QTMInterface;
using QsprMiscShared.Utilities;

namespace QC.QTMApplication;

public class QTMStatusBar
{
	private const string DEFAULT_DEBUG_MSG = "Debug Messages Appear here";

	private StatusBar statusBar;

	private string strTestMsg = "No tests running...";

	private string strLoopMsg = "";

	private string strUnitMsg = "";

	private string strDebugMsg = "";

	private string strPluginMsg = "";

	private int iTotalTestsSoFar;

	private int iMax;

	private DateTime startTime = DateTime.Now;

	private DateTime currTestStartTime;

	private bool showElapsedTime;

	private QSPRMainWindow mainWindow;

	public string TestMessage
	{
		get
		{
			return strTestMsg;
		}
		set
		{
			strTestMsg = value;
			statusBar.FindName<TextBlock>("testName").Text = strTestMsg;
		}
	}

	public string ElapsedTime
	{
		get
		{
			if (!showElapsedTime)
			{
				return "00:00";
			}
			return GetElapsedTime(startTime);
		}
		set
		{
		}
	}

	public string CurrentTestElapsedTime
	{
		get
		{
			if (!showElapsedTime)
			{
				return "00:00";
			}
			return GetElapsedTime(currTestStartTime);
		}
		set
		{
		}
	}

	public string DebugMessage
	{
		get
		{
			return strDebugMsg;
		}
		set
		{
			strDebugMsg = value;
			statusBar.FindName<TextBlock>("debug").Text = strDebugMsg;
		}
	}

	public string LoopMessage
	{
		get
		{
			return strLoopMsg;
		}
		set
		{
			strLoopMsg = value;
			statusBar.FindName<TextBlock>("loop").Text = strLoopMsg;
		}
	}

	public string PluginMessage
	{
		get
		{
			return strPluginMsg;
		}
		set
		{
			strPluginMsg = value;
			statusBar.FindName<TextBlock>("PluginStatus").Text = strPluginMsg;
		}
	}

	public string UnitMessage
	{
		get
		{
			return strUnitMsg;
		}
		set
		{
			strUnitMsg = value;
			statusBar.FindName<TextBlock>("unitName").Text = strUnitMsg;
		}
	}

	public int NoOfTests
	{
		get
		{
			return iMax;
		}
		set
		{
			iMax = value;
			iTotalTestsSoFar = 0;
			ProgressBar progressBar = statusBar.FindName<ProgressBar>("Progress");
			statusBar.FindName<TextBlock>("TextBlockPercentage").Text = "0%";
			progressBar.Maximum = iMax;
			progressBar.Value = 0.0;
			if (iMax > 0)
			{
				progressBar.Visibility = Visibility.Visible;
			}
			DebugMessage = "";
		}
	}

	public QTMStatusBar(StatusBar statusBar, QSPRMainWindow window)
	{
		iTotalTestsSoFar = 0;
		this.statusBar = statusBar;
		mainWindow = window;
	}

	private string GetElapsedTime(DateTime startingTime)
	{
		TimeSpan timeSpan = DateTime.Now - startingTime;
		string text = "";
		if (timeSpan.Days > 0)
		{
			return timeSpan.ToString("d\\.hh\\:mm\\:ss");
		}
		if (timeSpan.Hours > 0)
		{
			return timeSpan.ToString("hh\\:mm\\:ss");
		}
		return timeSpan.ToString("mm\\:ss");
	}

	public void OnAllTestsDone()
	{
		try
		{
			iTotalTestsSoFar = iMax - 1;
			OnTestDone();
			statusBar.FindName<TextBlock>("loop").Text = "";
			statusBar.FindName<TextBlock>("debug").Text = "";
			statusBar.FindName<TextBlock>("TextBlockPercentage").Text = "0%";
			TestMessage = "No tests running...";
			NoOfTests = 0;
			statusBar.FindName<ProgressBar>("Progress").Visibility = Visibility.Collapsed;
			PluginMessage = "";
		}
		catch
		{
		}
	}

	public void OnTestDone()
	{
		iTotalTestsSoFar++;
		try
		{
			ProgressBar progressBar = statusBar.FindName<ProgressBar>("Progress");
			if (progressBar.Maximum >= (double)iTotalTestsSoFar)
			{
				progressBar.Value = iTotalTestsSoFar;
				statusBar.FindName<TextBlock>("TextBlockPercentage").Text = $"{(int)((double)(100 * iTotalTestsSoFar) / progressBar.Maximum)}%";
			}
			statusBar.FindName<TextBlock>("testName").Text = "";
		}
		catch (Exception ex)
		{
			_ = ex.Message;
		}
	}

	public void HandleTestMessage(TestMsgType msgType, string strMsg, string testIndex, TestResult Result, object Param, string serialNumber)
	{
		try
		{
			switch (msgType)
			{
			case TestMsgType.OnBreak:
				TestMessage = "Tree is paused";
				break;
			case TestMsgType.OnStartOfDebugTesting:
				NoOfTests = (int)Param;
				startTime = DateTime.Now;
				showElapsedTime = true;
				break;
			case TestMsgType.OnEndOfDebugTesting:
				showElapsedTime = false;
				OnAllTestsDone();
				break;
			case TestMsgType.OnUnitStart:
				showElapsedTime = true;
				startTime = DateTime.Now;
				UnitMessage = strMsg;
				NoOfTests = (int)((PhoneStart)Param).count;
				break;
			case TestMsgType.OnUnitEnd:
				showElapsedTime = false;
				UnitMessage = strMsg;
				OnAllTestsDone();
				break;
			case TestMsgType.OnLoopStart:
				LoopMessage = mainWindow._scheduler.GlobalStatus.LoopDetails;
				break;
			case TestMsgType.OnLoopEnd:
				LoopMessage = mainWindow._scheduler.GlobalStatus.LoopDetails;
				break;
			case TestMsgType.OnTestStart:
				TestMessage = strMsg;
				LoopMessage = mainWindow._scheduler.GlobalStatus.LoopDetails;
				currTestStartTime = (DateTime)Param;
				break;
			case TestMsgType.OnTestResult:
				UnitMessage = serialNumber;
				OnTestDone();
				break;
			case TestMsgType.OnDebugMsg:
				DebugMessage = strMsg;
				break;
			case TestMsgType.OnTestStop:
				OnAllTestsDone();
				break;
			case TestMsgType.OnSessionStart:
				UnitMessage = strMsg;
				break;
			case TestMsgType.OnSessionStop:
				UnitMessage = strMsg;
				break;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "OnTestMessages " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void Clear()
	{
		statusBar.FindName<TextBlock>("loop").Text = "";
		statusBar.FindName<TextBlock>("debug").Text = "";
		statusBar.FindName<ProgressBar>("Progress").Visibility = Visibility.Collapsed;
		TestMessage = " No tests running...";
		UnitMessage = "";
	}
}
