//
// LDASTools frameCPP - A library implementing the LIGO/Virgo frame
// specification
//
// Copyright (C) 2019 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__IMPL__FrEndOfFileSerialIO_HH
#define FrameCPP_UNIVERSAL__IMPL__FrEndOfFileSerialIO_HH

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/version.hpp>

#include "framecpp/Common/FrameSpec.hh"
#include "framecpp/Universal/impl/HelperSerialIO.hh"
#include "framecpp/Universal/impl/FrEndOfFileData.hh"

namespace FrameCPP
{
    namespace Universal
    {
        class FrEndOfFile;

        namespace FrEndOfFileImpl
        {
            template < typename FR_END_OF_FILE_T >
            class SerialIO : public virtual Data
            {
            public:
                //-------------------------------------------------------
                //
                //-------------------------------------------------------
                SerialIO( )
                {
                }
                
                SerialIO( const SerialIO& ) = default;
                SerialIO( SerialIO&& ) = default;
                SerialIO& operator=( const SerialIO& ) = delete;
                SerialIO& operator=( SerialIO&& ) = delete;

            private:
                friend class boost::serialization::access;

                template < class Archive >
                void
                serialize( Archive& archiver, const unsigned int version )
                {
                    using boost::serialization::make_nvp;

                    // All versions have nFrames
                    archiver & BOOST_SERIALIZATION_NVP( nFrames );

                    // nBytes field type varies by version
                    if ( version >= 5 ) {
                        archiver & BOOST_SERIALIZATION_NVP( nBytes );
                    } else if ( version >= 3 ) {
                        // Handle v3-v4 with smaller nBytes type
                        if ( typename Archive::is_saving() ) {
                            v3::nBytes_type nBytes_v3 = static_cast<v3::nBytes_type>( nBytes );
                            archiver & make_nvp( "nBytes", nBytes_v3 );
                        } else {
                            v3::nBytes_type nBytes_v3;
                            archiver & make_nvp( "nBytes", nBytes_v3 );
                            const_cast<SerialIO*>(this)->nBytes = nBytes_v3;
                        }
                    }

                    // Version 8+ uses different field ordering
                    if ( version >= 8 ) {
                        archiver & BOOST_SERIALIZATION_NVP( seekTOC );

                        if ( version >= 9 ) {
                            archiver & BOOST_SERIALIZATION_NVP( chkSumTOC );
                        }

                        archiver & BOOST_SERIALIZATION_NVP( chkSumFrHeader );
                        archiver & BOOST_SERIALIZATION_NVP( chkSum );
                        archiver & BOOST_SERIALIZATION_NVP( chkSumFile );
                    } else if ( version >= 3 ) {
                        // Legacy v3-v7 format handling
                        if ( version >= 5 ) {
                            v5::chkType_type chkType = 0; // Legacy field
                            archiver & make_nvp( "chkType", chkType );
                        } else {
                            v3::chkFlag_type chkFlag = 0; // Legacy field  
                            archiver & make_nvp( "chkFlag", chkFlag );
                        }

                        archiver & BOOST_SERIALIZATION_NVP( chkSumFile );

                        if ( version >= 5 ) {
                            archiver & BOOST_SERIALIZATION_NVP( seekTOC );
                        } else if ( version >= 4 ) {
                            // Handle v4 with smaller seekTOC type
                            if ( typename Archive::is_saving() ) {
                                v4::seekTOC_type seekTOC_v4 = static_cast<v4::seekTOC_type>( seekTOC );
                                archiver & make_nvp( "seekTOC", seekTOC_v4 );
                            } else {
                                v4::seekTOC_type seekTOC_v4;
                                archiver & make_nvp( "seekTOC", seekTOC_v4 );
                                const_cast<SerialIO*>(this)->seekTOC = seekTOC_v4;
                            }
                        }
                    }
                } // method - serialize
            }; // class - SerialIO

        } // namespace FrEndOfFileImpl
    } // namespace universal
} // namespace FrameCPP

BOOST_CLASS_VERSION( FrameCPP::Universal::FrEndOfFile, 9 )

#endif /* FrameCPP_UNIVERSAL__IMPL__FrEndOfFileSerialIO_HH */
