//                           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 NLog;
using Raytheon.Common;
using System;

namespace Raytheon.Instruments
{
	/// <summary>
	/// A sim communication device
	/// </summary>
	public class CommDeviceSim : ICommDevice
	{
		#region PrivateClassMembers
		private static object _syncObj = new Object();
		private readonly string _name;
		private SelfTestResult _selfTestResult;
		private State _state;

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

		#endregion

		#region PrivateFunctions
		/// <summary>
		/// The Finalizer
		/// </summary>
		~CommDeviceSim()
		{
			Dispose(false);
		}

		/// <summary>
		/// Dispose of the resources contained by this object
		/// </summary>
		/// <param name="disposing"></param>
		protected virtual void Dispose(bool disposing)
		{
		}

		#endregion

		#region PublicFuctions

		/// <summary>
		/// CommDevice factory constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="configurationManager"></param>
		public CommDeviceSim(string name, IConfigurationManager configurationManager, ILogger logger)
		{
			_name = name;
			_selfTestResult = SelfTestResult.Unknown;
			_state = State.Uninitialized;

			_logger = logger;

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


		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		public CommDeviceSim(string name)
		{
			_name = name;
			_selfTestResult = SelfTestResult.Unknown;
			_state = State.Uninitialized;
			_logger = LogManager.GetCurrentClassLogger();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool ClearErrors()
		{
			throw new NotImplementedException();
		}

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

			set
			{
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DetailedStatus
		{
			get
			{
				return "This is a Comm Device Sim called " + _name;
			}
		}

		/// <summary>
		/// Dispose of the resources
		/// </summary>
		public void Dispose()
		{
			Dispose(true);

			GC.SuppressFinalize(this);
		}

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

			set
			{
				throw new NotImplementedException();
			}
		}

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

		/// <summary>
		/// 
		/// </summary>
		public void Initialize()
		{
			lock (_syncObj)
			{
				if (_state == State.Uninitialized)
				{
					_state = State.Ready;
				}
				else
				{
					throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString() + " on device " + _name);
				}
			}
		}

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

		/// <summary>
		/// 
		/// </summary>
		/*public void Open()
		{
		   lock (_syncObj)
		   {
		   }
		}*/

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

		/// <summary>
		/// Read data from the device.
		/// </summary>
		/// <param name="dataRead">The buffer to put the data in</param>
		/// <returns>The number of bytes read</returns>
		public uint Read(ref byte[] dataRead)
		{
			lock (_syncObj)
			{
				return (uint)dataRead.Length;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void Reset()
		{
			lock (_syncObj)
			{
			}
		}

		/// <summary>
		/// Sets the read timeout
		/// </summary>
		/// <param name="timeout"></param>
		public void SetReadTimeout(uint timeout)
		{
			lock (_syncObj)
			{
			}
		}

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

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

		/// <summary>
		/// 
		/// </summary>
		public void Shutdown()
		{
			lock (_syncObj)
			{
				_state = State.Uninitialized;
			}
		}

		/// <summary>
		/// Write data to the device
		/// </summary>
		/// <param name="dataToSend">The data to write</param>
		/// <param name="numBytesToWrite">The number of bytes to write</param>
		/// <returns>THe number of bytes that were written</returns>
		public uint Write(byte[] dataToSend, uint numBytesToWrite)
		{
			lock (_syncObj)
			{
				return numBytesToWrite;
			}
		}

		public void Close()
		{
			throw new NotImplementedException();
		}

		public void Open()
		{
			throw new NotImplementedException();
		}
		#endregion
	}
}
