using System;
using System.Collections.Generic;
using System.Text;
using Medivators.Common.Enums;

namespace Medivators.Common.Settings
{
	public struct CommPortSettings
	{
		public BaudRates BaudRate;
		public DataBits DataBit;
		public Parities Parity;
		public StopBits StopBit;
		public HandShakes HandShake;
		public override string ToString()
		{
			string rtnVal = string.Empty;
			rtnVal += Constants.BaudRateLabel	+ Constants.ValueDelimiter + this.BaudRate.ToString()	+ Constants.StringDelimiter;
			rtnVal += Constants.DataBitLabel	+ Constants.ValueDelimiter + this.DataBit.ToString()	+ Constants.StringDelimiter;
			rtnVal += Constants.ParityLabel		+ Constants.ValueDelimiter + this.Parity.ToString() + Constants.StringDelimiter;
			rtnVal += Constants.StopBitLabel	+ Constants.ValueDelimiter + this.StopBit.ToString() + Constants.StringDelimiter;
			rtnVal += Constants.HandShakeLabel	+ Constants.ValueDelimiter + this.HandShake.ToString();
			return rtnVal;
		}
		public CommPortSettings FromString ( string SettingString )
		{
			try
			{
				string[] elements = SettingString.Split(Constants.StringDelimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
				if (elements.Length > 0)
				{
					foreach (string e in elements)
					{
						string[] args = e.Split( Constants.ValueDelimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
						if (args.Length > 0)
						{
							switch (args[0])
							{
								case Constants.BaudRateLabel:
									this.BaudRate = (BaudRates)Enum.Parse(typeof(BaudRates), args[1]);
									break;
								case Constants.DataBitLabel:
									this.DataBit = (DataBits)Enum.Parse(typeof(DataBits), args[1]);
									break;
								case Constants.StopBitLabel:
									this.StopBit = (StopBits)Enum.Parse(typeof(StopBits), args[1]);
									break;
								case Constants.HandShakeLabel:
									this.HandShake = (HandShakes)Enum.Parse(typeof(HandShakes), args[1]);
									break;
								case Constants.ParityLabel:
									this.Parity = (Parities)Enum.Parse(typeof(Parities), args[1]);
									break;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ErrorHandler.GetInstance().LogException("Error reading CommPortSettings from string!", ex);
			}
			return this;
		}
		public bool Equals(CommPortSettings Settings)
		{
			return (this.BaudRate == Settings.BaudRate &&
					this.DataBit == Settings.DataBit &&
					this.HandShake == Settings.HandShake &&
					this.Parity == Settings.Parity &&
					this.StopBit == Settings.StopBit);
		}
	}
}
