// **********************************************************************************************************
//    OeMessage.cs
//    5/18/2022
//    NGI - Next Generation Interceptor
//    
//    Contract No. HQ0856-21-C-0003/1022000209
//    
//    THIS DOCUMENT DOES NOT CONTAIN TECHNOLOGY OR TECHNICAL DATA CONTROLLED UNDER EITHER THE U.S. 
//    INTERNATIONAL TRAFFIC IN ARMS REGULATIONS OR THE U.S. EXPORT ADMINISTRATION REGULATIONS.
//    
//    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.
//    
//    UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
//    
//    DESTRUCTION NOTICE: FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN DOD 5220.22-M, 
//    NATIONAL INDUSTRIAL SECURITY PROGRAM OPERATING MANUAL, FEBRUARY 2006, 
//    INCORPORATING CHANGE 1, MARCH 28, 2013, CHAPTER 5, SECTION 7, OR DODM 5200.01-VOLUME 3, 
//    DOD INFORMATION SECURITY PROGRAM: PROTECTION OF CLASSIFIED INFORMATION, ENCLOSURE 3, 
//    SECTION 17. FOR CONTROLLED UNCLASSIFIED INFORMATION FOLLOW THE PROCEDURES IN DODM 5200.01-VOLUME 4, 
//    INFORMATION SECURITY PROGRAM: CONTROLLED UNCLASSIFIED INFORMATION.
//    
//    CONTROLLED BY: MISSILE DEFENSE AGENCY
//    CONTROLLED BY: GROUND-BASED MIDCOURSE DEFENSE PROGRAM OFFICE
//    CUI CATEGORY: CTI 
//    DISTRIBUTION/DISSEMINATION CONTROL: F 
//    POC: Alex Kravchenko (1118268)
// **********************************************************************************************************

using Raytheon.Instruments.coeCSharp;
using System;

namespace Raytheon.Instruments.MessagingUtilities
{
    public class OeMessage : coeMessage
    {
        public Message XmlMessage
        {
            get { return m_Msg; }
            set
            {
                m_Msg = value;
                Label = value.Label;
                Size = value.GetMessageSize();
            }
        }

        public new string Label
        {
            get { return base.Label.ToString(); }
            set
            {
                if (Parse.Try(value, out uint label) == true)
                {
                    base.Label = label;
                }
                else
                {
                    throw new Exception("OeMessage: Label does not parse to an Unsigned Integer");
                }
            }
        }

        public new string Domain
        {
            get { return base.Domain.ToString(); }
            set
            {
                if (Parse.Try(value, out uint domain) == true)
                {
                    base.Domain = domain;
                }
                else
                {
                    throw new Exception("OeMessage: Domain does not parse to an Unsigned Integer");
                }
            }
        }

        public OeMessage() : base(0) { m_Msg = null; }
        public OeMessage(int size) : base(size) { m_Msg = null; }
        public OeMessage(Message msg)
           : base(0)
        {
            XmlMessage = msg;
        }
        public OeMessage(Message msg, int msgSize) : base(msgSize)
        {
            XmlMessage = msg;
        }

        override public void Serialize()
        {
            if (m_Msg != null)
            {
                byte[] serializedBuffer = m_Msg.serialize();
                if (serializedBuffer.Length > BufferSize)
                {
                    BufferSize = serializedBuffer.Length;
                }
                Size = (uint)serializedBuffer.Length;
                copyToMessageBuffer(serializedBuffer);
                Domain = m_Msg.Domain;
            }
        }

        override public void Deserialize()
        {
            if (m_Msg != null)
            {
                // Get sending time and pass it to the XmlMessage object;
                GetSendTime(out ulong sendSec, out uint sendSecFrac);
                m_Msg.SendSecond = sendSec;
                m_Msg.SendSecondFraction = sendSecFrac;
                m_Msg.Domain = Domain;
                byte[] receiveBuffer = copyFromMessageBuffer();
                if (receiveBuffer != null)
                {
                    m_Msg.deserialize(receiveBuffer);
                }
            }
        }

        Message m_Msg = null;
    }
}
