//                           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.Threading;

namespace Raytheon.Common
{
   /// <summary>
   /// Base class for handling incoming messages
   /// </summary>
   public abstract class MsgDevice : IDisposable
   {
      /// <summary>
      /// A callback def for a completed message
      /// </summary>
      /// <param name="msgId"></param>
      /// <param name="pData"></param>
      /// <param name="numBytes"></param>
      /// <param name="errorCode"></param>
      unsafe public delegate void CompleteMessageCallback(uint msgId, IntPtr pData, uint numBytes, uint errorCode);

      private DataBuffer _dataBuffer;
      private AutoResetEvent _dataInBufferEvent;
      private IWorkerInterface _msgProcessorWorker;
      private bool _isProcessorThreadRunning;
      private Thread _msgProcessorThread;

      /// <summary>
      /// The constructor
      /// </summary>
      /// <param name="msgParser"></param>
      /// <param name="bufferSize"></param>
      public MsgDevice(IMsgParser msgParser, uint bufferSize)
      {
         try
         {
            _isProcessorThreadRunning = false;
            _dataBuffer = new DataBuffer(bufferSize);
            _dataInBufferEvent = new AutoResetEvent(false);
            _msgProcessorWorker = new MsgProcessorWorker(msgParser, ref _dataBuffer, ref _dataInBufferEvent);
            _msgProcessorThread = new Thread(_msgProcessorWorker.DoWork);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// The finalizer
      /// </summary>
      ~MsgDevice()
      {
         Dispose(false);
      }

      /// <summary>
      /// The public dispose function. Necessary for commanding threads to quit
      /// </summary>
      public void Dispose()
      {
         Dispose(true);

         GC.SuppressFinalize(this);
      }

      /// <summary>
      /// Add data to the buffer
      /// </summary>
      /// <param name="data">The data to add</param>
      /// <param name="numBytes">the number of bytes to add</param>
      protected void AddToBuffer(byte[] data, uint numBytes)
      {
         try
         {
            _dataBuffer.Add(data, numBytes);

            _dataInBufferEvent.Set();
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Clear all data from the buffer
      /// </summary>
      protected void ClearBuffer()
      {
         try
         {
            _dataBuffer.RemoveAll();
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// The local dispose function. Necessary for commanding threads to quit
      /// </summary>
      /// <param name="disposing"></param>
      protected virtual void Dispose(bool disposing)
      {
         if (disposing)
         {
            try
            {
               _msgProcessorWorker.QuitWork();

               Thread.Sleep(500);

               _msgProcessorThread.Abort();

               if (_msgProcessorThread.IsAlive)
               {
                  _msgProcessorThread.Join();
               }

               _dataInBufferEvent.Dispose();

               _msgProcessorWorker.Dispose();

               _dataBuffer.Dispose();
            }
            catch (Exception err)
            {
               ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
            }
         }
      }

      /// <summary>
      /// Run the message processor thread
      /// </summary>
      protected void RunThread()
      {
         try
         {
            _msgProcessorThread.Start();
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Set the callback that will be invoked when a complete message is parsed out
      /// </summary>
      /// <param name="callback">The callback function</param>
      protected void SetCompleteMessageCallback(CompleteMessageCallback callback)
      {
         try
         {
            ((MsgProcessorWorker)_msgProcessorWorker).SetCallback(callback);

            // now that the callback is set, we can run the thread
            if (_isProcessorThreadRunning == false)
            {
               _msgProcessorThread.Start();
               _isProcessorThreadRunning = true;
            }
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Stop the message processor thread
      /// </summary>
      protected void QuitThread()
      {
         try
         {
            _msgProcessorWorker.QuitWork();

            if (_msgProcessorThread.IsAlive)
            {
               _msgProcessorThread.Join();
            }
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// abstract function for the children to implement
      /// </summary>
      /// <param name="data">The data to add</param>
      /// <param name="numBytes">The number of bytes to add</param>
      public abstract void AddData(byte[] data, uint numBytes);
   }
}
