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

namespace Raytheon.Instruments
{
   /// <summary>
   /// The message that targets the VRS to save video to disk
   /// </summary>
   internal class VrsGrabToDiskCmdMessage : AutomationMessage
   {
      [StructLayout(LayoutKind.Sequential, Pack = 1)]
      private struct Data
      {
         public UInt32 m_numberOfFrames;
         public UInt32 m_fileFormat;
      };

      private Data m_dataStruct;

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

      /// <summary>
      /// The constructor for sending
      /// </summary>
      /// <param name="numberOfFrames">The number of frames to send</param>
      /// <param name="format">The resulting file format</param>
      public VrsGrabToDiskCmdMessage(uint numberOfFrames, VideoSaveFormat format)
         : base((uint)VrsAutomationMsgIds.VRS_GRAB_VIDEO_TO_DISK_CMD, "VRS_GRAB_VIDEO_TO_DISK_CMD", (UInt16)(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))))
      {
         try
         {
            m_dataStruct.m_numberOfFrames = numberOfFrames;

            if (format == VideoSaveFormat.BIN)
            {
               m_dataStruct.m_fileFormat = 0;
            }
            else if (format == VideoSaveFormat.NCDF)
            {
               m_dataStruct.m_fileFormat = 1;
            }
            else
            {
               throw new Exception("Unsupported format: " + format.ToString());
            }
         }
         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>
      /// return the file format
      /// </summary>
      /// <returns></returns>
      public UInt32 GetFileFormat()
      {
         return m_dataStruct.m_fileFormat;
      }

      /// <summary>
      /// getter for the number of frames
      /// </summary>
      /// <returns>The number of frames</returns>
      public UInt32 GetNumberOfFrames()
      {
         return m_dataStruct.m_numberOfFrames;
      }

      /// <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 += "   numberOfFrames: " + m_dataStruct.m_numberOfFrames.ToString() + "\r\n";
         msg += "   file format: " + m_dataStruct.m_fileFormat.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()
      {
         VrsGrabToDiskCmdMessage msg = new VrsGrabToDiskCmdMessage(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)
      {
         // put the struct to the ptr
         Marshal.StructureToPtr(m_dataStruct, pData, true);
      }

      /// <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));
      }
   }
}