/*-------------------------------------------------------------------------
//                           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;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace Raytheon.Instruments.EthernetSockets
{
	/// <summary>
	/// Class for controlling a TCP client communication device
	/// </summary>
	public class TcpClient : ICommDevice
	{
      #region PrivateClassMembers

      private Socket _sock;
      private string _remoteAddress;
      private int _remotePort;
      private IPEndPoint _remoteEP = null;
      private IPAddress _ipAddress = null;
      private object _syncObj = new object();

      private readonly string _name;
		private State _state;

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

		#endregion

		public bool ClearErrors() => false;
		public bool FrontPanelEnabled {	get => false; set => throw new NotImplementedException(); }
		public bool DisplayEnabled { get => false; set => throw new NotImplementedException(); }
		public string DetailedStatus => $"This is a TCP/IP Device called {_name}";
		public InstrumentMetadata Info => throw new NotImplementedException();
		public State Status => _state;
		public string Name => _name;
		public SelfTestResult PerformSelfTest() => SelfTestResult;
		public SelfTestResult SelfTestResult => SelfTestResult.Unknown;
		public void Open() => Initialize();
		public void Close() => Disconnect();
      public void Shutdown() => Disconnect();
		public void Reset()
		{
			Close();
			Open();
		}

      #region Public Functions

      /// <summary>
		/// CommDevice factory constructor
		/// </summary>
		/// <param name="deviceInstanceName"></param>
		/// <param name="configurationManager"></param>
		public TcpClient(string deviceInstanceName, IConfigurationManager configurationManager, ILogger logger, string remoteAddress = "", int remotePort = 0)
      {
         _name = deviceInstanceName;

         _state = State.Uninitialized;

         _logger = logger;

         _configurationManager = configurationManager;

         // configuration obtained from [deviceInstanceName].xml file
         _configuration = _configurationManager.GetConfiguration(Name);

         if (string.IsNullOrEmpty(remoteAddress))
         {
            _remoteAddress = _configuration.GetConfigurationValue(deviceInstanceName, TcpClientConfigXml.REMOTE_ADDRESS.ToString(), "127.0.0.1");
         }
         else
         {
            _remoteAddress = remoteAddress;
         }

         if (remotePort == 0)
         {
            _remotePort = _configuration.GetConfigurationValue(deviceInstanceName, TcpClientConfigXml.REMOTE_PORT.ToString(), 0);
         }
         else
         {
            _remotePort = remotePort;
         }
      }

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="remoteAddress"></param>
      /// <param name="remotePort"></param>
      public TcpClient(string remoteAddress, int remotePort)
      {
         _remoteAddress = remoteAddress;
         _remotePort = remotePort;
      }

      /// <summary>
      /// initialize instrument
      /// </summary>
      public void Initialize()
      {
         // if remoteAddress is a hostname
         if (!IPAddress.TryParse(_remoteAddress, out _ipAddress))
         {
            string preferredSubnet = "";

            IPHostEntry host = Dns.GetHostEntry(_remoteAddress);
            foreach (IPAddress ip in host.AddressList)
            {
               AddressFamily af = ip.AddressFamily;
               if (af == AddressFamily.InterNetwork)
               {
                  if (preferredSubnet != String.Empty)
                  {
                     if (Regex.IsMatch(ip.ToString(), preferredSubnet, RegexOptions.IgnoreCase))
                        _ipAddress = ip;
                  }
                  else
                     _ipAddress = ip;

                  if (_ipAddress != null)
                     break;
               }
            }
         }

         if (_ipAddress != null)
         {
            if (_remoteEP == null)
               _remoteEP = new IPEndPoint(_ipAddress, _remotePort);
         }
         else
            throw new Exception($"Unable to create IPEndPoint to {_remoteAddress}, port {_remotePort}");

         if (_sock == null)
            _sock = new Socket(_ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
      }

      /// <summary>
		/// Connect to remote host
		/// </summary>
		/// <returns></returns>
      public void Connect()
      {
         lock (_syncObj)
         {
            try
            {
               if (!_sock.Connected && IsRemoteHostAlive())
                  _sock.Connect(_remoteEP);
            }
            catch (ObjectDisposedException)
            {
               _sock = new Socket(_ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
               if (IsRemoteHostAlive())
                  _sock.Connect(_remoteEP);
            }
            catch (Exception) { throw; }
         }
      }

      /// <summary>
		/// Disconnect from remote host
		/// </summary>
		/// <returns></returns>
      public void Disconnect()
      {
         lock (_syncObj)
         {
            if (_sock.Connected)
            {
               _sock.Shutdown(SocketShutdown.Both);
               _sock.Disconnect(true);
               _sock.Close();
            }
         }
      }

      /// <summary>
		/// Ping if remote host is alive
		/// </summary>
		/// <returns>true/false</returns>
      bool IsRemoteHostAlive()
      {
         bool isRemoteHostAlive = true;

         //Do a ping test to see if the server is reachable
         try
         {
            Ping pingTest = new Ping();
            PingReply reply = pingTest.Send(_ipAddress);
            if (reply.Status != IPStatus.Success)
               isRemoteHostAlive = false;
         }
         catch (PingException)
         {
            isRemoteHostAlive = false;
         }

         //See if the tcp state is ok
         if (_sock.Poll(5000, SelectMode.SelectRead) && (_sock.Available == 0))
         {
            isRemoteHostAlive = false;
         }

         return isRemoteHostAlive;
      }

      /// <summary>
      /// Read data from the device.
      /// </summary>
      /// <param name="dataBuf"></param>
      /// <param name="responseTimeOutMs"></param>
      /// <returns>The number of bytes read</returns>
      public uint Read(ref byte[] dataBuf)
      {
         int bytesRec = 0;
         lock (_syncObj)
         {
            try
            {
               bytesRec = _sock.Receive(dataBuf);
            }
            catch (SocketException)
            {
               bytesRec = 0;
            }
         }

         return (uint)bytesRec;
      }

      /// <summary>
		/// Sets the read timeout
		/// </summary>
		/// <param name="timeoutMs"></param>
		public void SetReadTimeout(uint timeoutMs)
      {
         _sock.ReceiveTimeout = (int)timeoutMs;
      }

      /// <summary>
		/// Write data to device.
		/// </summary>
		/// <param name="dataBuf"></param>
		/// <returns>The number of bytes written</returns>
      public uint Write(byte[] dataBuf, uint numBytesToWrite)
      {
         int bytesWritten = 0;
         lock (_syncObj)
         {
            try
            {
               bytesWritten = _sock.Send(dataBuf, (int)numBytesToWrite, SocketFlags.None);
            }
            catch (SocketException)
            {
               bytesWritten = 0;
            }
         }

         return (uint)bytesWritten;
      }

      #endregion
   }
}
