//                           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;
using System.Text;

namespace Raytheon.Instruments
{
   /// <summary>
   /// The response message from the VRS upon receiving a completing saving video to disk
   /// </summary>
   internal class VrsGrabToDiskCompleteRspMessage : 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;
         public UInt32 m_fileNameLen;
      };

      private Data m_dataStruct;

      private String m_fileName;

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

      /// <summary>
      /// The constructor for sending
      /// </summary>
      /// <param name="wasCommandSuccessful">flag for if the command was successful</param>
      public VrsGrabToDiskCompleteRspMessage(bool wasCommandSuccessful, String fileName)
         : base((uint)VrsAutomationMsgIds.VRS_GRAB_TO_DISK_COMPLETE_RSP, "VRS Grab To Disk Complete Response Message", (UInt16)(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))) + (uint)fileName.Length)
      {
         try
         {
            if (wasCommandSuccessful == true)
            {
               m_dataStruct.m_wasCommandSuccessful = COMMAND_WAS_SUCCESSFUL;
            }
            else
            {
               m_dataStruct.m_wasCommandSuccessful = COMMAND_WAS_NOT_SUCCESSFUL;
            }

            m_dataStruct.m_fileNameLen = (uint)fileName.Length;
         }
         catch (Exception)
         {
            throw;
         }
      }

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

      public String GetFileName()
      {
         return m_fileName;
      }

      /// <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";
         msg += "   fileNameLen: " + m_dataStruct.m_fileNameLen.ToString() + "\r\n";
         msg += "   filename: " + m_fileName + "\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()
      {
         VrsGrabToDiskCompleteRspMessage msg = new VrsGrabToDiskCompleteRspMessage(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);
         pData = IntPtr.Add(pData, successFlagBytes.Length);

         byte[] fileNameLenBytes = BitConverter.GetBytes(m_dataStruct.m_fileNameLen);
         Marshal.Copy(successFlagBytes, 0, pData, fileNameLenBytes.Length);
         pData = IntPtr.Add(pData, fileNameLenBytes.Length);

         byte[] fileNameBytes = Encoding.ASCII.GetBytes(m_fileName);
         Marshal.Copy(fileNameBytes, 0, pData, m_fileName.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));
         pData = IntPtr.Add(pData, (System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))));

         byte[] fileNameArray = new byte[m_dataStruct.m_fileNameLen];
         Marshal.Copy(pData, fileNameArray, 0, (int)m_dataStruct.m_fileNameLen);
         m_fileName = Encoding.ASCII.GetString(fileNameArray);
      }
   }
}