//                           UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.

THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.

UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/

using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
using NLog;
using Raytheon.Common;

namespace Raytheon.Instruments
{
	/// <summary>
	/// A class the provides an interface for read/write to registers on an FPGA that implement a custom ascii serial interface
	/// </summary>
	public class CommFpgaCustomAsciiSerial : IFpgaComm, IDisposable
	{
		#region PrivateClassMembers
		private SerialPort _serialPort;
		private byte[] _readBuf;
		private static object _syncObj = new Object();
		private uint _delayBeforeReadMs;
		private string _readFormat;
		private string _writeFormat;
		private SelfTestResult _selfTestResult;
		private State _state;
		private string _name;

		/// <summary>
		/// NLog logger
		/// </summary>
		private readonly ILogger _logger;
		/// <summary>
		/// Raytheon configuration
		/// </summary>
		private readonly IConfigurationManager _configurationManager;
		private readonly IConfiguration _configuration;

		#endregion

		#region PrivateFuctions

		/// <summary>
		/// The Finalizer
		/// </summary>
		~CommFpgaCustomAsciiSerial()
		{
			Dispose(false);
		}


		/// <summary>
		/// Reads the serial port until it is empty
		/// </summary>
		private void ClearBuffer()
		{
			lock (_syncObj)
			{
				bool isClearComplete = false;

				while (isClearComplete == false)
				{
					try
					{
						int numBytesRead = _serialPort.Read(_readBuf, 0, _readBuf.Length);

						if (numBytesRead < _readBuf.Length)
						{
							isClearComplete = true;
						}
					}
					catch (Exception)
					{
						//expected if buffer is already cleared
						isClearComplete = true;
					}
				}
			}
		}


		/// <summary>
		/// Dispose of this object
		/// </summary>
		/// <param name="disposing"></param>
		protected virtual void Dispose(bool disposing)
		{
			try
			{
				if (disposing)
				{
					if (_state == State.Ready)
					{
						_serialPort.Dispose();
						_state = State.Uninitialized;
					}
				}
			}
			catch (Exception)
			{
				try
				{
					//ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
				}
				catch (Exception)
				{
					//Do not rethrow. Exception from error logger that has already been garbage collected
				}
			}
		}

		#endregion

		#region PublicFuctions

		/// <summary>
		/// ELoadScpiKeysight factory constructor
		/// </summary>
		/// <param name="deviceName"></param>
		/// <param name="configurationManager"></param>
		public CommFpgaCustomAsciiSerial(string deviceName, IConfigurationManager configurationManager, ILogger logger)
		{
			Name = deviceName;

			_logger = logger;

			_configurationManager = configurationManager;
			_configuration = _configurationManager.GetConfiguration(Name);

			const int READ_BUF_SIZE = 1024;

			try
			{
				_selfTestResult = SelfTestResult.Unknown;

				_state = State.Uninitialized;

				string comPortName = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "ComPortName", "COM1");
				int baudRate = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "BaudRate", 9600);
				Parity parity = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "Parity", Parity.None);
				int dataBits = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "DataBits", 8);
				StopBits stopBits = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "StopBits", StopBits.None);

				_serialPort = new SerialPort(comPortName, baudRate, parity, dataBits, stopBits);
				_readFormat = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "ReadFormat", "");
				_writeFormat = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "WriteFormat", "");
				_serialPort.ReadTimeout = _configuration.GetConfigurationValue("CommFpgaCustomAsciiSerial", "ReadTimeout", 100);
				_delayBeforeReadMs = _configuration.GetConfigurationValue<uint>("CommFpgaCustomAsciiSerial", "DelayBeforeReadMs", 0);

				_readBuf = new byte[READ_BUF_SIZE];

				//check format of readFormat and writeFormat
				if (!_readFormat.Contains("<ADDRESS>"))
				{
					throw new Exception("the read format input for card " + _name + " does not contain the <ADDRESS> tag");
				}

				if (!_writeFormat.Contains("<ADDRESS>"))
				{
					throw new Exception("the write format input for card " + _name + "  does not contain the <ADDRESS> tag");
				}

				if (!_writeFormat.Contains("<DATA>"))
				{
					throw new Exception("the write format input for card " + _name + "  does not contain the <DATA> tag");
				}
			}
			catch (Exception ex)
			{
				_logger.Error(ex);

				if (_serialPort.IsOpen == true)
				{
					_serialPort.Close();
				}

				throw;
			}
		}

		/// <summary>
		/// The constructor which opens up a serial port
		/// </summary>
		/// <param name="comPortName">The port name. "Com1' for example</param>
		/// <param name="delayBeforeReadMs">The num of ms to wait before a read</param>
		/// <param name="baudRate">The baud rate</param>
		/// <param name="parity">The parity</param>
		/// <param name="dataBits">Number of data bits</param>
		/// <param name="stopBits">Number of Stop Bits</param>
		public CommFpgaCustomAsciiSerial(string name, string comPortName, uint delayBeforeReadMs, string readFormat, string writeFormat, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
		{
			const int READ_BUF_SIZE = 1024;

			try
			{
				_name = name;

				_selfTestResult = SelfTestResult.Unknown;

				_state = State.Uninitialized;

				_serialPort = new SerialPort(comPortName, baudRate, parity, dataBits, stopBits);

				_readFormat = readFormat;

				_writeFormat = writeFormat;

				_serialPort.ReadTimeout = 100;

				_delayBeforeReadMs = delayBeforeReadMs;

				_readBuf = new byte[READ_BUF_SIZE];

				Array.Clear(_readBuf, 0, _readBuf.Length);

				_logger = LogManager.GetCurrentClassLogger();

				//check format of readFormat and writeFormat
				if (readFormat.Contains("<ADDRESS>") == false)
				{
					throw new Exception("the read format input for card " + _name + " does not contain the <ADDRESS> tag");
				}

				if (writeFormat.Contains("<ADDRESS>") == false)
				{
					throw new Exception("the write format input for card " + _name + "  does not contain the <ADDRESS> tag");
				}

				if (writeFormat.Contains("<DATA>") == false)
				{
					throw new Exception("the write format input for card " + _name + "  does not contain the <DATA> tag");
				}
			}
			catch (Exception ex)
			{
				_logger.Error(ex);

				if (_serialPort.IsOpen == true)
				{
					_serialPort.Close();
				}

				throw;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool ClearErrors()
		{
			return false;
		}

		/// <summary>
		/// 
		/// </summary>
		public string DetailedStatus
		{
			get
			{
				return "This is a FPGA Custom Ascii called " + _name;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool DisplayEnabled
		{
			get
			{
				return false;
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// Dispose of this object
		/// </summary>
		public void Dispose()
		{
			try
			{
				Dispose(true);

				GC.SuppressFinalize(this);
			}
			catch (Exception)
			{
				try
				{
					//ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
				}
				catch (Exception)
				{
					//Do not rethrow. Exception from error logger that has already been garbage collected
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool FrontPanelEnabled
		{
			get
			{
				return false;
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public InstrumentMetadata Info
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Initialize()
		{
			Initialize(string.Empty);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fpga"></param>
		public void Initialize(string fpga)
		{
			lock (_syncObj)
			{
				if (_state == State.Uninitialized)
				{
					if (_serialPort.IsOpen == false)
					{
						_serialPort.Open();
					}

					//ClearBuffer();

					_state = State.Ready;
				}
				else
				{
					throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString() + " on card " + _name);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Name
		{
			get
			{
				return _name;
			}
			set { _name = value; }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public SelfTestResult PerformSelfTest()
		{
			_selfTestResult = SelfTestResult.Unknown;
			return _selfTestResult;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fpga"></param>
		/// <param name="address"></param>
		/// <returns></returns>
		public uint Read(string fpga, uint address)
		{
			// lock up the FPGA resource
			lock (_syncObj)
			{

				string hexAddress = "0x" + address.ToString("X8");

				string commandToSend = _readFormat.Replace("<ADDRESS>", hexAddress);

				commandToSend = commandToSend.Trim();

				_serialPort.Write(commandToSend);

				Thread.Sleep((int)_delayBeforeReadMs);

				int numBytesRead = _serialPort.Read(_readBuf, 0, _readBuf.Length);

				string data = Encoding.UTF8.GetString(_readBuf, 0, _readBuf.Length);

				// get rid of new lines
				data = data.Replace("\r", " ");
				data = data.Replace("\n", " ");
				data = data.Trim();

				uint dataToReturn = Convert.ToUInt32(data, 16);

				return dataToReturn;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fpgaName"></param>
		/// <param name="address"></param>
		/// <param name="numberOfWordsToRead"></param>
		/// <param name="shallWeIncrementAddress"></param>
		/// <param name="dataRead"></param>
		public void ReadBlock(string fpgaName, uint address, uint numberOfWordsToRead, bool shallWeIncrementAddress, ref uint[] dataRead)
		{
			// lock up the FPGA resource
			lock (_syncObj)
			{		
				Thread.Sleep((int)_delayBeforeReadMs);
				
				int bytesRead = _serialPort.Read(_readBuf, 0, _readBuf.Length);

				int bytesToCopy = Math.Min(bytesRead, dataRead.Length);

				Array.Copy(_readBuf, dataRead, bytesToCopy);
				
				Array.Clear(_readBuf, 0, _readBuf.Length);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Reset()
		{
			Shutdown();

			Initialize();
		}

		/// <summary>
		/// 
		/// </summary>
		public SelfTestResult SelfTestResult
		{
			get
			{
				return _selfTestResult;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public State Status
		{
			get
			{
				return _state;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Shutdown()
		{
			// lock up the FPGA resource
			lock (_syncObj)
			{
				if (_state == State.Ready)
				{
					_serialPort.Dispose();
					_state = State.Uninitialized;
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fpga"></param>
		/// <param name="address"></param>
		/// <param name="data"></param>
		public void Write(string fpga, uint address, uint data)
		{
			// lock up the FPGA resource
			lock (_syncObj)
			{
				string hexAddress = "0x" + address.ToString("X8");

				string hexData = "0x" + data.ToString("X8");

				string commandToSend = _writeFormat.Replace("<ADDRESS>", hexAddress);

				commandToSend = commandToSend.Replace("<DATA>", hexData);

				commandToSend = commandToSend.Trim();

				_serialPort.Write(commandToSend);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fpgaName"></param>
		/// <param name="address"></param>
		/// <param name="numberOfWordsToWrite"></param>
		/// <param name="data"></param>
		/// <param name="shallWeIncrementAddress"></param>
		public void WriteBlock(string fpgaName, uint address, uint numberOfWordsToWrite, uint[] data, bool shallWeIncrementAddress)
		{
			// lock up the FPGA resource
			lock (_syncObj)
			{
				throw new Exception("Not Implemented");
			}
		}

		/// <summary>
		/// Loads firmware
		/// </summary>
		/// <param name="fpgaName"></param>
		public void LoadFirmware(string fpgaName)
		{
			Initialize(fpgaName);
		}

		#endregion
	}
}
