using System;
using System.Runtime.InteropServices;

namespace CM2
{
	[ClassInterface(ClassInterfaceType.None)]
	[Guid("AA028363-D20E-4d1a-B6D5-59A697D822E9")]
	public class CM2Gen_CsTest : CM2.ICM2GenPlug
	{
		private const int E_FAIL = unchecked((int)0x80004005);
		
		private IntPtr hParent;
		private string lastErrorMessage;
		private int max;

		public CM2Gen_CsTest()
		{
		}

		#region ICM2GenPlug Members
		public void Name(out string name)
		{
			name = "CsTest";
		}

		public void Version(out ushort version)
		{
			version = 0x000A;
		}

		public void RequiredCoreVersion(out ushort requiredCoreVersion)
		{
			requiredCoreVersion = 0x01D6;
		}

		public void SetParentHandle(IntPtr windowHandle)
		{
			this.hParent = windowHandle;
		}

		public void GetLastErrorMsg(out string errorMessage)
		{
			if(this.lastErrorMessage == String.Empty)
				errorMessage = "No error";
			else
				errorMessage = this.lastErrorMessage;
		}

		public void GetInfoCount(out int infoCount)
		{
			infoCount = 3;
		}

		public void GetInfo(int index, out string infoName, out string infoValue)
		{
			infoName = null;
			infoValue = null;

			switch(index)
			{
				case 0:
					infoName = "Ported by";
					infoValue = "O. Westman";
					break;

				case 1:
					infoName = "Created";
					infoValue = "26th May 2004";
					break;

				case 2:
					infoName = "Original version by";
					infoValue = "C. Aaangel";
					break;

				default:
					this.lastErrorMessage = "Index out of bounds";
					Marshal.ThrowExceptionForHR(E_FAIL);
					break;
			}
		}

		public void GetSensorCount(out int sensorCount)
		{
			sensorCount = 2;
		}

		public void GetSensorNameOnly(int index, out string sensorName, out byte sensorType)
		{
			sensorName = null;
			sensorType = 0;

			switch(index)
			{
				case 0:
					sensorName = "Random Numbers";
					sensorType = 0;
					break;

				case 1:
					sensorName = "Return Parameters";
					sensorType = 1;
					break;

				default:
					this.lastErrorMessage = "Index out of bounds";
					Marshal.ThrowExceptionForHR(E_FAIL);
					break;
			}
		}

		public void GetSensor(int index, string parameters, out string sensorName, out byte sensorType, out string sensorString, out double sensorValue)
		{
			sensorName = null;
			sensorType = 0;
			sensorString = null;
			sensorValue = 0;

			string[] param = parameters.Split(new char[] {'|', '='});

			for(int i = 0; i < param.Length; i++)
			{
				if(String.Compare(param[i], "RANGE", true) == 0 || String.Compare(param[i], "MAX", true) == 0)
				{
					this.max = Convert.ToInt32(param[++i]);
				}
			}

			switch(index)
			{
				case 0:
					sensorName = "Random Numbers";
					sensorType = 0;
					sensorString = "";
					sensorValue = new Random().Next(this.max);
					break;

				case 1:
					sensorName = "Return Parameters";
					sensorType = 1;
					sensorString = parameters;
					sensorValue = 0;
					break;

				default:
					this.lastErrorMessage = "Index out of bounds";
					Marshal.ThrowExceptionForHR(E_FAIL);
					break;
			}
		}

		public void Init()
		{
			this.max = 255;
		}

		public void Terminating()
		{
		}

		public void Setup()
		{
			System.Windows.Forms.MessageBox.Show("Nothing to setup.", "CsTest", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
		}

		public void GetDefaultCount(int sensorIndex, out int count)
		{
			count = 0;
		}

		public void GetDefault(int sensorIndex, int index, out string name, out string defaultValue, out string description)
		{
			name = null;
			defaultValue = null;
			description = null;
		}

		public void GetOptionalCount(int sensorIndex, out int count)
		{
			count = 0;

			switch(sensorIndex)
			{
				case 0:
					count = 1;
					break;
			}
		}

		public void GetOptional(int sensorIndex, int index, out string name, out string defaultValue, out string description)
		{
			name = null;
			defaultValue = null;
			description = null;

			switch(sensorIndex)
			{
				case 0:
				{
					switch(index)
					{
						case 0:
						{
							name = "range";
							defaultValue = "255";
							description = "Generate random number between 0 - \"range\", default value is 255";
							break;
						}
					}
					break;
				}
			}
		}

		#endregion
	}
}
