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

namespace Raytheon.Instruments
{
   /// <summary>
   /// The response message from the VRS upon receiving a VrsDisconnectCmdMessage
   /// </summary>
   internal class VrsDisconnectRspMessage : 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 UInt32 m_wasCommandSuccessful;
      };

      private Data m_dataStruct;

      /// <summary>
      /// Copy constructor
      /// </summary>
      /// <param name="rhs">The object to copy</param>
      public VrsDisconnectRspMessage(VrsDisconnectRspMessage rhs)
         : base(rhs)
      {
         try
         {
            m_dataStruct = rhs.m_dataStruct;
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// The constructor for sending
      /// </summary>
      /// <param name="wasCommandSuccessful">flag for if the command was successful</param>
      public VrsDisconnectRspMessage(bool wasCommandSuccessful)
         : base((uint)VrsAutomationMsgIds.VRS_DISCONNECT_RSP, "VRS_DISCONNECT_RSP", (UInt16)(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))))
      {
         try
         {
            if (wasCommandSuccessful == true)
            {
               m_dataStruct.m_wasCommandSuccessful = COMMAND_WAS_SUCCESSFUL;
            }
            else
            {
               m_dataStruct.m_wasCommandSuccessful = COMMAND_WAS_NOT_SUCCESSFUL;
            }
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <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 (m_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: " + m_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()
      {
         VrsDisconnectRspMessage msg = new VrsDisconnectRspMessage(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(m_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)
      {
         m_dataStruct = (Data)Marshal.PtrToStructure(pData, typeof(Data));
      }
   }
}