//                           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>
   /// 
   /// </summary>
   internal class FPGAWriteRegisterCmdBlockIncrMessage : FPGACmdMessage
   {
      #region PrivateClassMembers

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

      private byte[] m_data;

      private Data m_dataStruct;

      private bool m_byteSwap;
      #endregion

      #region PrivateFunctions

      /// <summary>
      /// Create a copy of this object
      /// </summary>
      /// <returns>A copy of this object</returns>
      protected override object CloneSelf()
      {
         FPGAWriteRegisterCmdBlockIncrMessage msg = new FPGAWriteRegisterCmdBlockIncrMessage(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
         {
            //Temporary storage for message data
            Data dataTemp = m_dataStruct;

            //address swap
            dataTemp.m_address = Util.Swap(dataTemp.m_address);

            //Do we need to swap bytes
            if (m_byteSwap == true)
            {
               SwapWord(ref m_data);
            }

            //Pointer to message
            Marshal.StructureToPtr(dataTemp, pData, true);
            pData = IntPtr.Add(pData, Marshal.SizeOf(typeof(Data)));
            Marshal.Copy(m_data, 0, pData, m_data.Length);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      protected override int GetPayloadSize()
      {
         return Marshal.SizeOf(m_dataStruct) + m_data.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)
      {
         try
         {
            //copy data into our structure
            m_dataStruct = (Data)(Marshal.PtrToStructure(pData, typeof(Data)));
            pData = IntPtr.Add(pData, Marshal.SizeOf(typeof(Data)));
            Marshal.Copy(pData, m_data, 0, m_data.Length);

            m_dataStruct.m_address = Util.Swap(m_dataStruct.m_address);

            //Swap data if required
            if (m_byteSwap)
            {
               SwapWord(ref m_data);
            }
         }
         catch (Exception)
         {
            throw;
         }
      }
      #endregion

      #region PublicFunctions

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

      /// <summary>
      /// The constructor for sending
      /// </summary>
      public FPGAWriteRegisterCmdBlockIncrMessage(Page page, uint address, byte[] data, bool swapDataBytes)
         : base(FPGACmdMsgIds.TYPE_THREE_WRITE_CMD_ID, page, "Write Register Command Increment Block")
      {
         try
         {
            m_dataStruct.m_address = address;
            m_data = data;
            m_byteSwap = swapDataBytes;
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="page"></param>
      /// <param name="address"></param>
      /// <param name="data"></param>
      /// <param name="swapDataBytes"></param>
      public FPGAWriteRegisterCmdBlockIncrMessage(Page page, uint address, uint[] data, bool swapDataBytes)
         : base(FPGACmdMsgIds.TYPE_THREE_WRITE_CMD_ID, page, "Write Register Command Increment Block")
      {
         try
         {
            m_dataStruct.m_address = address;
            m_byteSwap = swapDataBytes;

            //Convert to Byte Array
            m_data = new byte[data.Length * sizeof(uint)];
            Buffer.BlockCopy(data, 0, m_data, 0, m_data.Length);
         }
         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\r\n";
         return msg;
      }
      #endregion

   }
}
