using System.Media;
using System.Threading;
using System.Windows.Forms;
using QC.QTMDotNetKernelInterface;
using QC.QTMDotNetKernelInterface.Environment;

namespace QC.QTMToolKit;

[QTMTestClass]
internal class MessageBoxWithStringBuilderInput : QSPRTest
{
	private string message = "";

	private string caption = "";

	private MessageBoxButtons buttons;

	private int result;

	private string wavfilename;

	private bool enableSound;

	[QTMParameter(ParameterIOType.Input, DefaultValue = "String Builder Message", Description = "Message displayed on the Message Box.  Use <GV> format to embed GV values in your message", TestList = "QMessageBoxWithStringBuilder")]
	public string Message
	{
		get
		{
			return message;
		}
		set
		{
			message = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, DefaultValue = "QSPR", Description = "Caption the Message Box.Use <GV> format to embed GV values in your message", TestList = "QMessageBoxWithStringBuilder")]
	public string Caption
	{
		get
		{
			return caption;
		}
		set
		{
			caption = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, DefaultValue = "OK", Description = "Message Box Buttons", TestList = "QMessageBoxWithStringBuilder")]
	public MessageBoxButtons Buttons
	{
		get
		{
			return buttons;
		}
		set
		{
			buttons = value;
		}
	}

	[QTMParameter(ParameterIOType.Output, DefaultValue = "0", UpperLimit = "7", LowerLimit = "0", Description = "OK=1,Cancel=2,Abort=3,Retry=4,Ignore=5,Yes=6,No=7", TestList = "QMessageBoxWithStringBuilder")]
	public int Result
	{
		get
		{
			return result;
		}
		set
		{
			result = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, DefaultValue = "c:\\windows\\media\\chimes.wav", DisplayHint = DisplayHints.FILE, TestList = "QMessageBoxWithStringBuilder")]
	public string WavFileName
	{
		get
		{
			return wavfilename;
		}
		set
		{
			wavfilename = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, DefaultValue = "True", TestList = "QMessageBoxWithStringBuilder")]
	public bool EnableSound
	{
		get
		{
			return enableSound;
		}
		set
		{
			enableSound = value;
		}
	}

	public MessageBoxWithStringBuilderInput(QSPREnvironment qsprEnv)
		: base(qsprEnv)
	{
	}

	[QTMTest(Description = "Creates a messagebox where caption and message can be built using values of global vbles. Also, plays sound till a button is pressed in the message box.", CanAddToTestPlan = true)]
	public bool QMessageBoxWithStringBuilder()
	{
		string FinalMessage = "";
		MessageBoxIcon icon = MessageBoxIcon.None;
		switch (buttons)
		{
		case MessageBoxButtons.AbortRetryIgnore:
		case MessageBoxButtons.RetryCancel:
			icon = MessageBoxIcon.Hand;
			break;
		case MessageBoxButtons.OK:
			icon = MessageBoxIcon.Asterisk;
			break;
		case MessageBoxButtons.OKCancel:
		case MessageBoxButtons.YesNoCancel:
		case MessageBoxButtons.YesNo:
			icon = MessageBoxIcon.Question;
			break;
		}
		message = message.Replace("\\n", "\n");
		BuildMessageFromGV(message, out FinalMessage);
		BuildMessageFromGV(caption, out var FinalMessage2);
		SoundPlayer soundPlayer = new SoundPlayer();
		if (enableSound && wavfilename.Trim() != "")
		{
			try
			{
				soundPlayer.SoundLocation = wavfilename;
				soundPlayer.PlayLooping();
			}
			catch
			{
				soundPlayer = null;
			}
		}
		string gvValue = "";
		base.Env.GlobalVariable.GetGlobalVariable("RunByApp", out gvValue);
		if (string.IsNullOrEmpty(gvValue))
		{
			DialogResult dialogResult = MessageBox.Show(FinalMessage, FinalMessage2, buttons, icon);
			result = (int)dialogResult;
		}
		else
		{
			base.Env.GlobalVariable.SetGlobalVariable("WaitingForApp", "1");
			base.Env.GlobalVariable.SetGlobalVariable("AppMSG", FinalMessage);
			string gvValue2 = "1";
			while (gvValue2.Equals("1"))
			{
				Thread.Sleep(200);
				base.Env.GlobalVariable.GetGlobalVariable("WaitingForApp", out gvValue2);
			}
			string gvValue3 = "";
			base.Env.GlobalVariable.GetGlobalVariable("AppMSG", out gvValue3);
			result = int.Parse(gvValue3);
		}
		soundPlayer?.Stop();
		return true;
	}

	[QTMTest(CanAddToTestPlan = true)]
	public bool BuildMessageFromGV(string message, out string FinalMessage)
	{
		FinalMessage = "";
		string gvValue = "";
		if (message.Contains("<"))
		{
			string[] array = message.Split('<');
			string[] array2 = array;
			foreach (string text in array2)
			{
				if (text.Contains(">"))
				{
					string[] array3 = text.Split('>');
					base.Env.GlobalVariable.GetGlobalVariable(array3[0], out gvValue);
					FinalMessage += gvValue;
					if (array3.GetLength(0) > 1)
					{
						FinalMessage += array3[1];
					}
				}
				else
				{
					FinalMessage += text;
				}
			}
		}
		else
		{
			FinalMessage = message;
		}
		return true;
	}
}
