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

namespace FpgaMeasurementInstrumentsLib
{
   /// <summary>
   /// The response message for the FPGAReadRegisterCmdMessage
   /// </summary>
   internal class FPGAReadRegisterBlockIncrRspMessage : FPGARspMessage
   {
      #region PrivateClassMembers

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

      //array for all received data (address/data)
      private Data[] m_dataArray;

      //Array(ref) for received data (data only)
      private uint[] m_refArray;
      #endregion

      #region PrivateFunctions

      /// <summary>
      /// Create a copy of this object
      /// </summary>
      /// <returns>A copy of this object</returns>
      protected override object CloneSelf()
      {
         FPGAReadRegisterBlockIncrRspMessage msg = new FPGAReadRegisterBlockIncrRspMessage(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)
      {
         //From base class. Data does not need to be formatted (will not be sent out)
      }

      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      protected override int GetPayloadSize()
      {
         //Size of Data (address and data) * number of elements
         return Marshal.SizeOf(typeof(Data)) * m_dataArray.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
         {
            for (int i = 0; i < m_dataArray.Length; i++)
            {
               //Copy data into the structure
               m_dataArray[i] = (Data)(Marshal.PtrToStructure(pData, typeof(Data)));

               //Increment pointer 
               pData = IntPtr.Add(pData, Marshal.SizeOf(typeof(Data)));

               //Swap Address
               m_dataArray[i].m_address = Util.Swap(m_dataArray[i].m_address);

               //Swap Data
               m_dataArray[i].m_data = Util.Swap(m_dataArray[i].m_data);

               //Move data into referenced array
               m_refArray[i] = m_dataArray[i].m_data;
            }
         }
         catch (Exception)
         {
            throw;
         }
      }
      #endregion

      #region PublicFunctions

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

      /// <summary>
      /// The constructor
      /// </summary>
      /// <param name="address">The address that was read from from</param>
      /// <param name="data">The data that was read</param>
      public FPGAReadRegisterBlockIncrRspMessage(uint address, ref uint[] data)
         : base(FPGARspMsgIds.TYPE_TWO_READ_RSP_ID, "Read Register Response Message")
      {
         try
         {
            m_dataArray = new Data[data.Length];

            m_refArray = data;
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// getter for the address
      /// </summary>
      /// <returns>TThe address</returns>
      public override uint GetAddress()
      {
         //Array of data. This function would need to change
         return 0;
      }

      /// <summary>
      /// getter for the data
      /// </summary>
      /// <returns>The data</returns>
      public override uint GetData()
      {
         //Array of data. This function would need to change
         return 0;
      }

      /// <summary>
      /// Convert this object into string form
      /// </summary>
      /// <returns>The string form of this object</returns>
      public override string ToString()
      {
         string msg = base.ToString();
         for (int i = 0; i < m_dataArray.Length; i++)
         {
            msg += "   address: " + m_dataArray[i].m_address.ToString("X8") + "\r\n";
            msg += "   data: " + m_dataArray[i].m_data.ToString("X8") + "\r\n\r\n";
         }

         return msg;
      }
      #endregion

   }
}