//                           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 FpgaMeasurementInstrumentsLib
{
   /// <summary>
   /// The message that targets the VRSUI to make a connection
   /// </summary>
   internal class FPGAWriteRegisterCmdMessage : FPGACmdMessage
   {
      #region PrivateClassMembers

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

      private Data m_dataStruct;
      #endregion

      #region PrivateFunctions

      /// <summary>
      /// Create a copy of this object
      /// </summary>
      /// <returns>A copy of this object</returns>
      protected override object CloneSelf()
      {
         FPGAWriteRegisterCmdMessage msg = new FPGAWriteRegisterCmdMessage(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)
      {
         try
         {
            // perform byte swapping and copy data into the buffer
            Data dataTemp = m_dataStruct;
            dataTemp.m_address = Util.Swap(dataTemp.m_address);
            dataTemp.m_data = Util.Swap(dataTemp.m_data);
            Marshal.StructureToPtr(dataTemp, pData, true);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      protected override int GetPayloadSize()
      {
         return Marshal.SizeOf(m_dataStruct);
      }

      /// <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)
      {
         try
         {
            // copy data into our structure and byte swap
            m_dataStruct = (Data)(Marshal.PtrToStructure(pData, typeof(Data)));
            m_dataStruct.m_address = Util.Swap(m_dataStruct.m_address);
            m_dataStruct.m_data = Util.Swap(m_dataStruct.m_data);
         }
         catch (Exception)
         {
            throw;
         }
      }
      #endregion

      #region PublicFunctions

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

      /// <summary>
      /// The constructor for sending
      /// </summary>
      public FPGAWriteRegisterCmdMessage(Page page, uint address, uint data)
         : base(FPGACmdMsgIds.TYPE_TWO_WRITE_CMD_ID, page, "Write Register Command")
      {
         try
         {
            m_dataStruct.m_address = address;
            m_dataStruct.m_data = data;
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Convert this object into string form
      /// </summary>
      /// <returns></returns>
      public override string ToString()
      {
         string msg = base.ToString();
         msg += "   address: " + m_dataStruct.m_address.ToString("X8") + "\r\n";
         msg += "   data: " + m_dataStruct.m_data.ToString("X8") + "\r\n\r\n";
         return msg;
      }
      #endregion

   }
}