//                           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 message that commands the VRS to move a file
   /// </summary>
   internal class VrsAddAttributesCmdMessage : AutomationMessage
   {
      #region PrivateClassMembers

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

      private Data _dataStruct;

      private string _videoFileName;
      private string _attributeFileName;
      #endregion

      #region PrivateClassFunctions

      /// <summary>
      /// Create a copy of this object
      /// </summary>
      /// <returns>A copy of this object</returns>
      protected override object CloneSelf()
      {
         VrsAddAttributesCmdMessage msg = new VrsAddAttributesCmdMessage(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(_dataStruct, pData, true);
         pData = IntPtr.Add(pData, (System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))));

         byte[] videoFileNameBytes = Encoding.ASCII.GetBytes(_videoFileName);
         Marshal.Copy(videoFileNameBytes, 0, pData, _videoFileName.Length);
         pData = IntPtr.Add(pData, _videoFileName.Length);

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

         byte[] videoFileNameArray = new byte[_dataStruct._videoFileNameStrLen];
         Marshal.Copy(pData, videoFileNameArray, 0, (int)_dataStruct._videoFileNameStrLen);
         _videoFileName = Encoding.ASCII.GetString(videoFileNameArray);
         pData = IntPtr.Add(pData, (int)_dataStruct._videoFileNameStrLen);

         byte[] attributeFileNameArray = new byte[_dataStruct.m_attributeFileStrLen];
         Marshal.Copy(pData, attributeFileNameArray, 0, (int)_dataStruct.m_attributeFileStrLen);
         _attributeFileName = Encoding.ASCII.GetString(attributeFileNameArray);
      }
      #endregion

      #region PublicClassFunctions

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

      /// <summary>
      /// The constructor for sending
      /// </summary>
      public VrsAddAttributesCmdMessage(string videoFile, string attributeFile)
         : base((uint)VrsAutomationMsgIds.VRS_ADD_ATTRIBUTES_CMD, "VRS_ADD_ATTRIBUTES_CMD", (ushort)(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))) + (uint)videoFile.Length + (uint)attributeFile.Length)
      {
         _dataStruct._videoFileNameStrLen = (uint)videoFile.Length;
         _videoFileName = videoFile;

         _dataStruct.m_attributeFileStrLen = (uint)attributeFile.Length;
         _attributeFileName = attributeFile;
      }

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

      /// <summary>
      /// Convert this object into string form
      /// </summary>
      /// <returns></returns>
      public override String ToString()
      {
         string msg = base.ToString();
         msg += "   videoFileNameStrLen: " + _dataStruct._videoFileNameStrLen + "\r\n";
         msg += "   videoFileName: " + _videoFileName + "\r\n";
         msg += "   attributeFileStrLen: " + _dataStruct.m_attributeFileStrLen + "\r\n";
         msg += "   attributeFileName: " + _attributeFileName + "\r\n\r\n";
         return msg;
      }
      #endregion
   }
}