using System;
using DebugToolInstrumentInterface;

namespace SimpleSignalGenerator;

[SupportedResourceName("QTI_SCPI")]
[SupportedResourceName("E4438C")]
[SupportedResourceName("Agilent_8960")]
[SupportedResourceName("IFR 20XX")]
public class SG : ISignalGenerator, Instrument, IDisposable
{
	private enum SGType
	{
		IFR_20XX,
		E4438C,
		QTI_SCPI,
		Agilent_8960,
		Unknown
	}

	private string msg;

	private SGType sg = SGType.Unknown;

	private TE_Handle te_handle;

	private string m_resourceString;

	public void setupTEDriverType(TEDriverType type)
	{
		te_handle.SetupDriverType(type);
	}

	public SG()
	{
		te_handle = new TE_Handle();
	}

	public void Dispose()
	{
	}

	public bool Setup(string resourceString)
	{
		try
		{
			string[] array = resourceString.Split(',');
			m_resourceString = array[0];
			if (array[0] == "IFR 20XX")
			{
				sg = SGType.IFR_20XX;
			}
			else if (array[0] == "E4438C")
			{
				sg = SGType.E4438C;
			}
			else if (array[0] == "QTI_SCPI")
			{
				sg = SGType.QTI_SCPI;
			}
			else if (array[0] == "Agilent_8960")
			{
				sg = SGType.Agilent_8960;
			}
			else
			{
				sg = SGType.Unknown;
			}
			return te_handle.Init(array[1]);
		}
		catch
		{
			return false;
		}
	}

	public string GetResourceString()
	{
		return m_resourceString;
	}

	public bool SetCenterFrequency(double dFrequencyInMHz, double offSetMHz)
	{
		string text = "";
		bool flag = true;
		double num = dFrequencyInMHz + offSetMHz;
		if (sg == SGType.IFR_20XX)
		{
			if (dFrequencyInMHz <= 2400.0)
			{
				object obj = text;
				text = string.Concat(obj, "CFRQ:VALUE ", num, "MHz");
			}
			else
			{
				flag = false;
			}
		}
		else if (sg == SGType.E4438C)
		{
			text = "FREQ " + num + "MHz";
		}
		else if (sg == SGType.QTI_SCPI)
		{
			text = "QCOM:SOUR:FREQ:CENT " + num + "MHz";
		}
		else if (sg == SGType.Agilent_8960)
		{
			text = "CALL:CELL:RFGENERATOR:FREQUENCY " + num + " MHZ";
		}
		if (flag)
		{
			te_handle.write(text);
		}
		return flag;
	}

	public bool EnableOutput(bool onOff)
	{
		string cmd = "";
		if (sg == SGType.IFR_20XX)
		{
			cmd = ((!onOff) ? "RFLV:OFF;*OPC?" : "RFLV:ON;*OPC?");
		}
		else if (sg == SGType.E4438C)
		{
			cmd = ((!onOff) ? ":OUTPUT OFF" : ":OUTPUT ON");
		}
		else if (sg == SGType.QTI_SCPI)
		{
			cmd = ((!onOff) ? "QCOM:SOUR:STATE OFF" : "QCOM:SOUR:STATE ON");
		}
		else if (sg == SGType.Agilent_8960)
		{
			cmd = ((!onOff) ? "CALL:CELL:POWER:STATE:CW 0" : "CALL:OPERATING:MODE CW");
		}
		te_handle.write(cmd);
		return true;
	}

	public bool SetModulationType()
	{
		string text = "";
		if (sg == SGType.QTI_SCPI)
		{
			text = "QCOM:GENErator:WAVeform 'CW'";
			te_handle.write(text);
			return true;
		}
		return false;
	}

	public bool SetPowerLevel(double dPowerLeveldBm)
	{
		string cmd = "";
		if (sg == SGType.IFR_20XX)
		{
			cmd = "RFLV:VALUE " + dPowerLeveldBm + "DBM;RFLV:ON;";
		}
		else if (sg == SGType.E4438C)
		{
			cmd = "POWER " + dPowerLeveldBm + "dBm";
		}
		else if (sg == SGType.QTI_SCPI)
		{
			cmd = "QCOM:SOUR:POW " + dPowerLeveldBm + " dBm";
		}
		else if (sg == SGType.Agilent_8960)
		{
			cmd = "CALL:CELL:POWER:SAMPLITUDE:CW " + dPowerLeveldBm;
		}
		te_handle.write(cmd);
		return true;
	}

	public string GetDriverVersion()
	{
		return typeof(SG).Assembly.GetName().Version.ToString();
	}

	public void SetupGen()
	{
		if (sg == SGType.Agilent_8960)
		{
			msg = "SYSTem:APPLication:FORMat 'WCDMA'";
			te_handle.write(msg);
		}
	}

	public void Close()
	{
		te_handle.Close();
	}
}
