//
// LDASTools frameCPP - A library implementing the LIGO/Virgo frame
// specification
//
// Copyright (C) 2025 California Institute of Technology
//
// LDASTools frameCPP is free software; you may redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 (GPLv2) of the
// License or at your discretion, any later version.
//
// LDASTools frameCPP is distributed in the hope that it will be useful, but
// without any warranty or even the implied warranty of merchantability
// or fitness for a particular purpose. See the GNU General Public
// License (GPLv2) for more details.
//
// Neither the names of the California Institute of Technology (Caltech),
// The Massachusetts Institute of Technology (M.I.T), The Laser
// Interferometer Gravitational-Wave Observatory (LIGO), nor the names
// of its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// You should have received a copy of the licensing terms for this
// software included in the file LICENSE located in the top-level
// directory of this package. If you did not, you can view a copy at
// http://dcc.ligo.org/M1500244/LICENSE
//

#ifndef FrameCPP_UNIVERSAL_FrHeader_HH
#define FrameCPP_UNIVERSAL_FrHeader_HH

#if !defined( SWIGIMPORTED )
#include "framecpp/Universal/impl/FrHeaderClassicIO.hh"
#include "framecpp/Universal/impl/FrHeaderSerialIO.hh"
#endif /* !defined( SWIGIMPORTED ) */

#if defined( __cplusplus ) && !defined( SWIGIMPORTED )
namespace FrameCPP
{
    namespace Universal
    {
        //===================================================================
        /// \brief Frame header structure
        ///
        /// FrHeader represents the 40-byte frame header that appears at the
        /// beginning of each frame file. It contains basic frame metadata
        /// including GPS time, frame length, and library identification.
        ///
        /// The structure evolves across frame specification versions:
        /// - v3-v7: Bytes 38-39 contain legacy constant 'A''Z'
        /// - v8+: Bytes 38-39 contain frameLibrary and checksumScheme fields
        //===================================================================
        class FrHeader : public FrHeaderImpl::ClassicIO< FrHeader >,
                        public FrHeaderImpl::SerialIO< FrHeader >
        {
        public:
            //-----------------------------------------------------------------
            /// \brief Default constructor
            //-----------------------------------------------------------------
            FrHeader( );

            //-----------------------------------------------------------------
            /// \brief Explicit construction
            ///
            /// \param[in] frameLength
            ///     Length of the frame in bytes
            /// \param[in] gpsTime
            ///     GPS time of the frame start
            /// \param[in] frameLibrary
            ///     Library used to create the frame
            /// \param[in] checksumScheme
            ///     Checksum scheme used (v8+ only)
            //-----------------------------------------------------------------
            FrHeader( frameLibrary_type frameLibrary,
                     checksumScheme_type checksumScheme );

            //-----------------------------------------------------------------
            /// \brief Copy Constructor
            ///
            /// \param[in] header
            ///     Header to use as the base
            ///
            /// \return
            ///     A new instance of the object.
            //-----------------------------------------------------------------
            FrHeader( const FrHeader& header );

            //-----------------------------------------------------------------
            /// \brief Virtual constructor
            //-----------------------------------------------------------------
            virtual inline FrHeader*
            Clone( ) const override
            {
                return new FrHeader( *this );
            }

            //-----------------------------------------------------------------
            /// \brief equality operator
            ///
            /// \param[in] RHS
            ///     The FrHeader object to be compared.
            ///
            /// \return
            ///     The value true is returned if this object is equivelent
            ///     to the RHS instance; false otherwise.
            //-----------------------------------------------------------------
            virtual bool
            operator==( const Common::FrameSpec::Object& Obj ) const override;

            bool
            operator==( const FrHeader& RHS ) const
            {
                return Data::operator==( RHS );
            }

            //-----------------------------------------------------------------
            /// \brief Assignment operator for Common::FrHeader
            ///
            /// Copies the base Common::FrHeader data (bytes 0-6) into this
            /// Universal::FrHeader object while preserving the frame-specific
            /// fields (bytes 7-39).
            ///
            /// \param[in] base
            ///     The Common::FrHeader object to copy from
            ///
            /// \return
            ///     Reference to this object
            //-----------------------------------------------------------------
            FrHeader& operator=(const Common::FrHeader& base)
            {
                // Copy the base class portion (bytes 0-6)
                Common::FrHeader::operator=(base);
                return *this;
            }

            //-----------------------------------------------------------------
            /// \brief Get checksum scheme type
            /// 
            /// Returns the checksum scheme from byte 39 of the header.
            /// For compatibility with code expecting Checksum() method.
            ///
            /// \return
            ///     The checksum scheme type (0 for none, or specific scheme ID)
            //-----------------------------------------------------------------
            checksumScheme_type Checksum() const { return GetChecksumScheme(); }

            //-----------------------------------------------------------------
            /// \brief Override byte swapping detection from Common::FrHeader
            /// 
            /// Returns the detected byte swapping requirement based on the
            /// Endian_4_byte field analysis performed by detectAndSetByteOrder().
            ///
            /// \return
            ///     true if byte swapping is required, false otherwise
            //-----------------------------------------------------------------
            virtual bool ByteSwapping() const override { return m_reorder_bytes; }

        protected:
        };

    } // namespace Universal
} // namespace FrameCPP

#endif /* defined( __cplusplus ) && !defined( SWIGIMPORTED ) */

#endif /* FrameCPP_UNIVERSAL_FrHeader_HH */