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

namespace Raytheon.Instruments
{
   /// <summary>
   /// The message that commands the VRS to move a file
   /// </summary>
   internal class VrsMoveFileCmdMessage : AutomationMessage
   {
      [StructLayout(LayoutKind.Sequential, Pack = 1)]
      private struct Data
      {
         public UInt32 m_fromStrLen;
         public UInt32 m_toStrLen;
         public UInt32 m_copyControl;
      };

      private Data m_dataStruct;

      private String m_fromFileName;
      private String m_toFileName;

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

      /// <summary>
      /// 
      /// </summary>
      /// <param name="fromFileName"></param>
      /// <param name="toFileName"></param>
      /// <param name="control"></param>
      public VrsMoveFileCmdMessage(string fromFileName, string toFileName, MoveControl control)
         : base((uint)VrsAutomationMsgIds.VRS_MOVE_FILE_CMD, "VRS_MOVE_FILE_CMD", (ushort)(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))) + (uint)fromFileName.Length + (uint)toFileName.Length)
      {
         m_dataStruct.m_fromStrLen = (uint)fromFileName.Length;
         m_fromFileName = fromFileName;

         m_dataStruct.m_toStrLen = (uint)toFileName.Length;
         m_toFileName = toFileName;

         m_dataStruct.m_copyControl = (uint)control;
      }

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

      /// <summary>
      /// Convert this object into string form
      /// </summary>
      /// <returns></returns>
      public override String ToString()
      {
         String msg = base.ToString();
         msg += "   fromLen: " + m_dataStruct.m_fromStrLen + "\r\n";
         msg += "   from: " + m_fromFileName + "\r\n";
         msg += "   toLen: " + m_dataStruct.m_toStrLen + "\r\n";
         msg += "   to: " + m_toFileName + "\r\n";
         msg += "   control: " + m_dataStruct.m_copyControl + "\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()
      {
         VrsMoveFileCmdMessage msg = new VrsMoveFileCmdMessage(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);
         pData = IntPtr.Add(pData, (System.Runtime.InteropServices.Marshal.SizeOf(typeof(Data))));

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

         byte[] toFileNameBytes = Encoding.ASCII.GetBytes(m_toFileName);
         Marshal.Copy(toFileNameBytes, 0, pData, toFileNameBytes.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[] fromFileNameArray = new byte[m_dataStruct.m_fromStrLen];
         Marshal.Copy(pData, fromFileNameArray, 0, (int)m_dataStruct.m_fromStrLen);
         m_fromFileName = Encoding.ASCII.GetString(fromFileNameArray);
         pData = IntPtr.Add(pData, (int)m_dataStruct.m_fromStrLen);

         byte[] toFileNameArray = new byte[m_dataStruct.m_toStrLen];
         Marshal.Copy(pData, toFileNameArray, 0, (int)m_dataStruct.m_toStrLen);
         m_toFileName = Encoding.ASCII.GetString(toFileNameArray);
      }
   }
}