//                           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.Runtime.InteropServices;
using Raytheon.Common;

namespace Raytheon.Instruments
{
   /// <summary>
   /// The response message from the VRS upon receiving a VrsConnectCmdMessage
   /// </summary>
   internal class VrsConnectRspMessage : AutomationMessage
   {
      private const UInt32 COMMAND_WAS_SUCCESSFUL = 1;
      private const UInt32 COMMAND_WAS_NOT_SUCCESSFUL = 0;

      [StructLayout(LayoutKind.Sequential, Pack = 1)]
      private struct Data
      {
         public uint m_wasCommandSuccessful;
      };

      private Data _dataStruct;

      /// <summary>
      /// Copy constructor
      /// </summary>
      /// <param name="rhs">The object to copy</param>
      public VrsConnectRspMessage(VrsConnectRspMessage rhs)
         : base(rhs)
      {
         _dataStruct = rhs._dataStruct;
      }

      /// <summary>
      /// The constructor for sending
      /// </summary>
      /// <param name="wasCommandSuccessful">flag for if the command was successful</param>
      public VrsConnectRspMessage(bool wasCommandSuccessful)
         : base((uint)VrsAutomationMsgIds.VRS_CONNECT_RSP, "VRS Connect Response Message", (ushort)(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))))
      {
         if (wasCommandSuccessful == true)
         {
            _dataStruct.m_wasCommandSuccessful = COMMAND_WAS_SUCCESSFUL;
         }
         else
         {
            _dataStruct.m_wasCommandSuccessful = COMMAND_WAS_NOT_SUCCESSFUL;
         }
      }

      /// <summary>
      /// Perform the action associated with this message
      /// </summary>
      public override void ExecuteMsg()
      {
         try
         {
            // no action to take
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// getter
      /// </summary>
      /// <returns>The success flag from the execution of the command</returns>
      public bool GetSuccessfulFlag()
      {
         if (_dataStruct.m_wasCommandSuccessful == COMMAND_WAS_SUCCESSFUL)
         {
            return true;
         }
         else
         {
            return false;
         }
      }

      /// <summary>
      /// Convert this object into string form
      /// </summary>
      /// <returns>The string form of this object</returns>
      public override string ToString()
      {
         string msg = base.ToString();
         msg += "   wasCommandSuccessful: " + _dataStruct.m_wasCommandSuccessful.ToString() + "\r\n\r\n";

         return msg;
      }

      /// <summary>
      /// Create a copy of this object
      /// </summary>
      /// <returns>A copy of this object</returns>
      protected override object CloneSelf()
      {
         VrsConnectRspMessage msg = new VrsConnectRspMessage(this);

         return msg;
      }

      /// <summary>
      /// Encode the message into a byte array for sending.
      /// </summary>
      /// <param name="pData">The buffer to put the message items</param>
      protected override void FormatData(IntPtr pData)
      {
         // copy data from our structure, info the buffer
         byte[] successFlagBytes = BitConverter.GetBytes(_dataStruct.m_wasCommandSuccessful);
         Marshal.Copy(successFlagBytes, 0, pData, successFlagBytes.Length);
      }

      /// <summary>
      /// Takes an array of bytes and populates the message object
      /// </summary>
      /// <param name="pData">The message in byte form</param>
      protected override void ParseData(IntPtr pData)
      {
         _dataStruct = (Data)Marshal.PtrToStructure(pData, typeof(Data));
      }
   }
}