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

namespace Raytheon.Instruments
{
   /// <summary>
   /// Handles received messages from the remote VRS system
   /// </summary>
   internal class VrsClientMsgHandler : MsgDevice
   {
      #region PrivateClassMembers
      private Dictionary<uint, Func<IntPtr, bool>> _messageHandlerMap;
      private ICommDevice _commInterface;
      #endregion

      #region PrivateClassFunctions

      /// <summary>
      /// Handle the add attribute message
      /// </summary>
      /// <param name="pData">the handle attribute msg data</param>
      /// <returns></returns>
      private bool HandleAddAttributesRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleAddAttributesRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrsAddAttributesRspMessage msg = new VrsAddAttributesRspMessage(false);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the VRS_CONNECT_RSP 
      /// </summary>
      /// <param name="pData">The data associated with VRS_CONNECT_RSP</param>
      private bool HandleConnectRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleConnectRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrsConnectRspMessage msg = new VrsConnectRspMessage(false);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the disconnect rsp msg
      /// </summary>
      /// <param name="pData"></param>
      /// <returns></returns>
      private bool HandleDisconnectRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleDisconnectRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrsDisconnectRspMessage msg = new VrsDisconnectRspMessage(false);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the VRS_GRAB_VIDEO_TO_DISK_RSP 
      /// </summary>
      /// <param name="pData">The data associated with VRS_GRAB_VIDEO_TO_DISK_RSP</param>
      private bool HandleGrabToDiskRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleGrabToDiskRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrsGrabToDiskRspMessage msg = new VrsGrabToDiskRspMessage(false);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the VrsGrabToDiskCompleteRspMessage
      /// </summary>
      /// <param name="pData"></param>
      /// <returns></returns>
      private bool HandleGrabToDiskCompleteRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleGrabToDiskCompleteRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrsGrabToDiskCompleteRspMessage msg = new VrsGrabToDiskCompleteRspMessage(false, "");
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the VrsMoveFileRspMessage
      /// </summary>
      /// <param name="pData"></param>
      /// <returns></returns>
      private bool HandleMoveFileRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleMoveFileRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrsMoveFileRspMessage msg = new VrsMoveFileRspMessage(false);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the VrsQueryHardDriveRspMessage
      /// </summary>
      /// <param name="pData"></param>
      /// <returns></returns>
      private bool HandleQueryDiskRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleQueryDiskRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrsQueryHardDriveRspMessage msg = new VrsQueryHardDriveRspMessage(false, 0, 0, 0, 0);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the VrStopLiveVideoRspMessage
      /// </summary>
      /// <param name="pData"></param>
      /// <returns></returns>
      private bool HandleStopLiveVideoRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleStopLiveVideoRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrStopLiveVideoRspMessage msg = new VrStopLiveVideoRspMessage(false);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }

      /// <summary>
      /// Handler for the VrShowLiveVideoRspMessage
      /// </summary>
      /// <param name="pData"></param>
      /// <returns></returns>
      private bool HandleShowLiveVideoRspMsg(IntPtr pData)
      {
         try
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::HandleShowLiveVideoRspMsg() - beginning", ErrorLogger.LogLevel.INFO);

            VrShowLiveVideoRspMessage msg = new VrShowLiveVideoRspMessage(false);
            msg.Parse(pData);
            msg.ExecuteMsg();
            AutomationRxMsgBuffer.Instance().AddMsg(msg);
         }
         catch (Exception err)
         {
            ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
         }

         return true;
      }
      #endregion

      #region PublicFuctions

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="bufferSize">The number of bytes for the buffer that holds onto incomming data</param>
      unsafe public VrsClientMsgHandler(uint bufferSize)
         : base(new VrsMsgParser(), bufferSize)
      {
         _commInterface = null;

         // set the callback
         base.SetCompleteMessageCallback(this.OnCompleteMessage);

         // set up function mapping for received messages
         _messageHandlerMap = new Dictionary<uint, Func<IntPtr, bool>>();
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_CONNECT_RSP] = HandleConnectRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_GRAB_VIDEO_TO_DISK_RSP] = HandleGrabToDiskRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_SHOW_LIVE_VIDEO_RSP] = HandleShowLiveVideoRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_STOP_LIVE_VIDEO_RSP] = HandleStopLiveVideoRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_MOVE_FILE_RSP] = HandleMoveFileRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_QUERY_HARD_DRIVE_RSP] = HandleQueryDiskRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_ADD_ATTRIBUTES_RSP] = HandleAddAttributesRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_DISCONNECT_RSP] = HandleDisconnectRspMsg;
         _messageHandlerMap[(uint)VrsAutomationMsgIds.VRS_GRAB_TO_DISK_COMPLETE_RSP] = HandleGrabToDiskCompleteRspMsg;
      }

      /// <summary>
      /// Add data to the buffer
      /// </summary>
      /// <param name="data">The data to add</param>
      /// <param name="numBytes">The number of bytes in the data buffer</param>
      public override void AddData(byte[] data, uint numBytes)
      {
         AddToBuffer(data, numBytes);
      }

      /// <summary>
      /// Clear all data from the buffer
      /// </summary>
      public void ClearData()
      {
         ClearBuffer();
      }

      /// <summary>
      /// The callback function for when a complete message is received
      /// </summary>
      /// <param name="msgId">The id of the message received</param>
      /// <param name="pData">The data for the message received</param>
      /// <param name="numBytes">The number of bytes in pData</param>
      /// <param name="errorCode">The error code</param>
      unsafe public void OnCompleteMessage(uint msgId, IntPtr pData, uint numBytes, uint errorCode)
      {
         if (_messageHandlerMap.ContainsKey(msgId) == false)
         {
            ErrorLogger.Instance().Write("VrsClientMsgHandler::OnCompleteMessage() - detected unknown msg id: " + msgId.ToString());
         }
         else
         {
            //call the message handler
            _messageHandlerMap[msgId](pData);
         }
      }

      /// <summary>
      /// Stops the message handler
      /// </summary>
      public void QuitHandler()
      {
         base.QuitThread();
      }

      /// <summary>
      /// If this handler needs to send out data, this is the setter for the ICommNodeInterface object
      /// </summary>
      /// <param name="commInterface">The communication interface</param>
      public void SetCommInterface(ICommDevice commInterface)
      {
         _commInterface = commInterface;
      }

      /// <summary>
      /// Sets the log file name. Not supported for this handler. Will throw an expection if called
      /// </summary>
      /// <param name="fileName">The log file name</param>
      public void SetLogFileName(string fileName)
      {
         throw new Exception("This handler does not support this");
      }
      #endregion
   }
}
