//
// LDASTools frameCPP - A library implementing the LIGO/Virgo frame
// specification
//
// Copyright (C) 2018 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
//

#include <framecpp_config.h>

#include "framecpp/Universal/FrEndOfFrame.hh"

#include "framecpp/Universal/FrSE.hh"
#include "framecpp/Universal/FrSH.hh"
#include "framecpp/Universal/PTR_STRUCT.hh"


using FrameCPP::Common::Description;
using FrameCPP::Common::FrameSpec;

//=======================================================================
// Static
//=======================================================================
FR_OBJECT_META_DATA_DEFINE_UNIVERSAL( FrEndOfFrameImpl,
                                      FSI_FR_END_OF_FRAME,
                                      "FrEndOfFrame",
                                      "End of Frame Data Structure" )

namespace FrameCPP
{
    namespace Universal
    {
        FrEndOfFrame::FrEndOfFrame( )
        {
        }

        FrEndOfFrame::FrEndOfFrame( run_type Run, frame_type Frame )
        {
            run = Run;
            frame = Frame;
        }

        FrEndOfFrame::~FrEndOfFrame( )
        {
        }

        bool
        FrEndOfFrame::operator==( const Common::FrameSpec::Object& RHS ) const
        {
            return FrameCPP::compare( *this, RHS );
        }

    } // namespace Universal
} // namespace FrameCPP

//=======================================================================
//               C L A S S I C   I O
//=======================================================================
namespace FrameCPP
{
    namespace Universal
    {
        namespace FrEndOfFrameImpl
        {
            template <>
            const Description*
            ClassicIO< FrEndOfFrame >::GetDescription(
                unsigned int frame_stream_version ) const
            {

                Description* description{ nullptr };

                if ( frame_stream_version >= 8 )
                {
                    static Description v8_description;

                    if ( v8_description.size( ) == 0 )
                    {
                        v8_description( FrSH( Data::FrameSpecName( ),
                                              Data::FrameSpecId( ),
                                              Data::FrameSpecDescription( ) ) );
                        v8_description(
                            FrSE( "run",
                                  Description::Ascii< v8::run_type >( ),
                                  "Run number; same as in FrameHeader run "
                                  "number datum." ) );
                        v8_description( FrSE(
                            "frame",
                            Description::Ascii< v8::frame_type >( ),
                            "Frame number, monotonically increasing until end "
                            "of run;"
                            " same as in Frame Header run number datum" ) );
                        v8_description(
                            FrSE( "GTimeS",
                                  Description::Ascii< v8::GTimeS_type >( ),
                                  "Frame start time in GPS Seconds." ) );
                        v8_description(
                            FrSE( "GTimeN",
                                  Description::Ascii< v8::GTimeN_type >( ),
                                  "Frame start time residual, integer "
                                  "nanoseconds." ) );
                        v8_description( FrSE( "chkSum",
                                              CheckSumDataClass( ),
                                              CheckSumDataComment( ) ) );
                    }
                    description = &v8_description;
                }
                else if ( frame_stream_version >= 5 )
                {
                    static Description v5_description;

                    if ( v5_description.size( ) == 0 )
                    {
                        v5_description( FrSH( Data::FrameSpecName( ),
                                              Data::FrameSpecId( ),
                                              Data::FrameSpecDescription( ) ) );
                        v5_description(
                            FrSE( "run",
                                  Description::Ascii< v5::run_type >( ),
                                  "Run number; same as in FrameHeader run "
                                  "number datum." ) );
                        v5_description( FrSE(
                            "frame",
                            Description::Ascii< v5::frame_type >( ),
                            "Frame number, monotonically increasing until end "
                            "of run;"
                            " same as in Frame Header run number datum" ) );
                        v5_description(
                            FrSE( "chkType",
                                  Description::Ascii< v5::chkType_type >( ),
                                  "Frame start time in GPS Seconds." ) );
                        v5_description(
                            FrSE( "chkSum",
                                  Description::Ascii< v5::chkSum_type >( ),
                                  "Frame checksum." ) );
                    }
                    description = &v5_description;
                }
                else if ( frame_stream_version >= 4 )
                {
                    static Description v4_description;

                    if ( v4_description.size( ) == 0 )
                    {
                        v4_description( FrSH( Data::FrameSpecName( ),
                                              Data::FrameSpecId( ),
                                              Data::FrameSpecDescription( ) ) );
                        v4_description(
                            FrSE( "run",
                                  Description::Ascii< v4::run_type >( ),
                                  "Run number; same as in FrameHeader run "
                                  "number datum." ) );
                        v4_description( FrSE(
                            "frame",
                            Description::Ascii< v4::frame_type >( ),
                            "Frame number, monotonically increasing until end "
                            "of run;"
                            " same as in Frame Header run number datum" ) );
                    }
                    description = &v4_description;
                }
                else if ( frame_stream_version >= 3 )
                {
                    static Description v3_description;

                    if ( v3_description.size( ) == 0 )
                    {
                        v3_description( FrSH( Data::FrameSpecName( ),
                                              Data::FrameSpecId( ),
                                              Data::FrameSpecDescription( ) ) );
                        v3_description(
                            FrSE( "run",
                                  Description::Ascii< v3::run_type >( ),
                                  "Run number; same as in FrameHeader run "
                                  "number datum." ) );
                        v3_description( FrSE(
                            "frame",
                            Description::Ascii< v3::frame_type >( ),
                            "Frame number, monotonically increasing until end "
                            "of run;"
                            " same as in Frame Header run number datum" ) );
                    }
                    description = &v3_description;
                }

                return description;
            }

        } // namespace FrEndOfFrameImpl
    } // namespace Universal
} // namespace FrameCPP
