//                           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;

namespace FpgaMeasurementInstrumentsLib
{
   /// <summary>
   /// The header for all command messages
   /// </summary>
   internal class FPGACmdMessageHeader
   {
      #region PrivateClassMembers
      private const int m_HEADER_SIZE = 1;
      private byte m_headerCmd;
      #endregion

      #region PublicFunctions

      /// <summary>
      /// Copy constructor
      /// </summary>
      /// <param name="header">The header to copy</param>
      public FPGACmdMessageHeader(FPGACmdMessageHeader header)
      {
         try
         {
            m_headerCmd = header.m_headerCmd;
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Constructor for when receiving data.
      /// Use this constructor and then parse to populate it
      /// </summary>
      public FPGACmdMessageHeader()
      {
         try
         {
            m_headerCmd = 0;
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Constructor for sending
      /// </summary>
      /// <param name="commandType">the type of the message</param>
      /// <param name="page">The page to read or write to</param>
      public FPGACmdMessageHeader(FPGACmdMsgIds commandType, FPGACmdMessage.Page page)
      {
         try
         {
            m_headerCmd = (byte)((byte)commandType | (byte)page);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Encode the header into a byte array for sending
      /// </summary>
      /// <param name="pData">The buffer to put the message items</param>
      public void Format(IntPtr pData)
      {
         try
         {
            Marshal.StructureToPtr(m_headerCmd, pData, true);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// getter for the number of bytes in the header
      /// </summary>
      /// <returns>The header length in bytes</returns>
      public uint GetHeaderLength()
      {
         try
         {
            return m_HEADER_SIZE;
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// getter for the message id
      /// </summary>
      /// <returns>The message id</returns>
      public FPGACmdMsgIds GetMessageId()
      {
         byte temp = (byte)(m_headerCmd >> 4);
         return (FPGACmdMsgIds)temp;
      }

      /// <summary>
      /// Takes an array of bytes and populates the header object
      /// </summary>
      /// <param name="pData">The header in byte form</param>
      public void Parse(IntPtr pData)
      {
         try
         {
            m_headerCmd = System.Runtime.InteropServices.Marshal.ReadByte(pData, 0);
         }
         catch (Exception)
         {
            throw;
         }
      }

      /// <summary>
      /// Creates a string version of the header members
      /// </summary>
      /// <returns>A string containning the header data</returns>
      public override string ToString()
      {
         string msg = "Header Data:\r\n";
         msg += "Command: " + m_headerCmd.ToString("X") + "\r\n";
         return msg;
      }
      #endregion
   }
}
