
using System;
using Medivators.Common;
using Medivators.Common.Settings;
using Medivators.Common.Enums;

namespace Medivators.Comm
{

	#region event delegates

	public delegate void CommErrorEventHandler		(object sender, CommErrorEventArgs			e);
	public delegate void DataArrivedEventHandler	(object sender, DataArrivedEventArgs		e);
	public delegate void SettingsChangedEventHandler(object sender, SettingsChangedEventArgs	e);

	#endregion

	#region		event args

	public enum CommEvent
	{
		Break,
		Frame,
		IOE,
		Mode,
		Overrun,
		RxOver,
		RxParity,
		TxFull,
		Timeout,
		Unknown
	}
	public class CommErrorEventArgs : System.EventArgs
	{
		private CommEvent	mCommEvent;
		private string		mMessage;

		public CommErrorEventArgs (CommEvent commEvent, string Message)
		{
			mCommEvent = commEvent;
			mMessage   = Message;
		}

		public CommEvent CommEvent
		{
			get { return mCommEvent;  }
			set { mCommEvent = value; }
		}
		public string	  Message
		{
			get { return mMessage; }
			set { mMessage = value;}
		}
		public override string ToString()
		{
			return "CommError: " + mCommEvent.ToString() +
				   "Message:   " + mMessage;
		}
	}

	public class DataArrivedEventArgs : System.EventArgs
	{
		private Byte[]	mData;
			
		public DataArrivedEventArgs( Byte[] Data )
		{
			mData = Data;
		}
		public Byte[] Data
		{
			get { return mData; }
			set { mData = value;}
		}
	}
	public class SettingsChangedEventArgs : System.EventArgs
	{
		private	CommPortSettings	mSettings;
			
		public SettingsChangedEventArgs ( CommPortSettings Settings )
		{
			mSettings = Settings;
		}
		public CommPortSettings Settings
		{
			get { return mSettings; }
			set { mSettings = value;}
		}
	}
		

	#endregion

	#region  interface

	//TODO update with any newly support serial devices
	public enum SupportedCommDevices
	{
		CommPort,
		CommPortSoftwareSimulation
	}

	//   all new comm ports will need to implement 
	//   this interface

	public interface ICommDevice : IDisposable
	{			
		bool								IsOpen		{get;}
		SupportedCommDevices				Device		{get;} 
		CommPortSettings					Settings	{get; set;}
		CommPorts							Port { get; set;} 
		void								OpenCommPort();
		void								CloseCommPort();
		void								WriteByte(Byte Value);
		void								WriteString(string Value);
		System.Windows.Forms.DialogResult	ShowControlDialog();
		event DataArrivedEventHandler		DataArrived	;
		event CommErrorEventHandler			CommError ;
		event SettingsChangedEventHandler	SettingsChanged ;
	}

	#endregion

	public class CommDevices
	{
	
		public CommDevices()
		{
		}

		public static ICommDevice CommDevice(SupportedCommDevices Device)
		{
			ICommDevice rtnDevice = null;
			try
			{
				//TODO update with any newly support Comm devices
				switch (Device)
				{
					case SupportedCommDevices.CommPort:
						rtnDevice = (ICommDevice)new CommPort();
						break;
					case SupportedCommDevices.CommPortSoftwareSimulation:
						rtnDevice = (ICommDevice)(new CommPortSimulation());
						break;
				}
				return rtnDevice;
			}
			catch (Exception DeviceErr)
			{
				ErrorHandler.GetInstance().LogException("CommDevices.CommDevice", DeviceErr);
				throw DeviceErr;
			}
		}
	}
}
