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

#if defined( __cplusplus )
#if !defined( SWIG )

#include "framecpp/Common/SearchContainer.hh"
#include "framecpp/Universal/FrTable.hh"
#include "framecpp/Universal/FrVect.hh"

#endif /* !defined( SWIG ) */
#endif /* defined( __cplusplus ) */

#if defined( __cplusplus )
#if !defined( SWIG )

#undef FR_SER_DATA_NAME_TYPE
#define FR_SER_DATA_NAME_TYPE std::string

#undef FR_SER_DATA_TIME_TYPE
#define FR_SER_DATA_TIME_TYPE FrameCPP::Universal::GPSTime

#undef FR_SER_DATA_SAMPLE_RATE_TYPE
#define FR_SER_DATA_SAMPLE_RATE_TYPE REAL_8

#undef FR_SER_DATA_DATA_TYPE
#define FR_SER_DATA_DATA_TYPE std::string

#include "framecpp/Universal/impl/FrObjectMetaData.hh"

#include "framecpp/Universal/impl/GPSTime.hh"

namespace FrameCPP
{
    namespace Universal
    {
        namespace FrSerDataImpl
        {
            class Data : public Impl::FrObjectMetaData< Data >
            {
            public:
                using FrTable = Universal::FrTable;
                using FrVect = Universal::FrVect;

                //-----------------------------------------------------
                // Legacy types
                //-----------------------------------------------------
                struct v3
                {
                    using name_type = std::string;
                    using time_type = GPSTime;
                    using timeSec_type = INT_4U;
                    using timeNsec_type = INT_4U;
                    using sampleRate_type = REAL_4;
                    using data_type = std::string;

                    using serial_type =
                        Common::SearchContainer< FrVect, &FrVect::GetName >;
                    using iterator = serial_type::iterator;
                    using const_iterator = serial_type::const_iterator;

                    using more_type =
                        Common::SearchContainer< FrVect, &FrVect::GetName >;
                };
                struct v4
                {
                    using name_type = v3::name_type;
                    using time_type = v3::time_type;
                    using timeSec_type = v3::timeSec_type;
                    using timeNsec_type = v3::timeNsec_type;
                    using sampleRate_type = v3::sampleRate_type;
                    using data_type = v3::data_type;

                    using serial_type = v3::serial_type;
                    using iterator = v3::iterator;
                    using const_iterator = v3::const_iterator;

                    using table_type =
                        Common::SearchContainer< FrTable,
                                                 &FrTable::GetNameSlow >;
                    using table_iterator = table_type::iterator;
                    using table_const_iterator = table_type::const_iterator;
                };
                using v5 = v4;
                using v6 = v5;
                struct v7
                {
                    using name_type = v6::name_type;
                    using time_type = v6::time_type;
                    using timeSec_type = v6::timeSec_type;
                    using timeNsec_type = v6::timeNsec_type;
                    using sampleRate_type = REAL_8;
                    using data_type = v6::data_type;

                    using serial_type = v6::serial_type;
                    using iterator = v6::iterator;
                    using const_iterator = v6::const_iterator;

                    using table_type = v6::table_type;
                    using table_iterator = v6::iterator;
                    using table_const_iterator = v6::table_const_iterator;
                };
                using v8 = v7;
                using v9 = v8;

                //-------------------------------------------------------

                using current_version = v9;

                using name_type = current_version::name_type;
                using time_type = current_version::time_type;
                using timeSec_type = current_version::timeSec_type;
                using timeNsec_type = current_version::timeNsec_type;
                using sampleRate_type = current_version::sampleRate_type;
                using data_type = current_version::data_type;

                using serial_type = current_version::serial_type;
                using iterator = current_version::iterator;
                using const_iterator = current_version::const_iterator;

                using table_type = current_version::table_type;
                using table_iterator = current_version::iterator;
                using table_const_iterator =
                    current_version::table_const_iterator;

                //-------------------------------------------------------
                /// \brief Return the name of station producing serial
                /// data stream.
                ///
                /// \return
                ///     The name associated of station producing serial
                ///     data stream.
                //-------------------------------------------------------
                inline const name_type&
                GetName( ) const
                {
                    return ( name );
                }

                //-------------------------------------------------------
                /// \brief Retrieve the time of data acquisition.
                ///
                /// \return
                ///     The time of data acquisition.
                //-------------------------------------------------------
                inline const time_type&
                GetTime( ) const
                {
                    return ( time );
                }

                //-------------------------------------------------------
                /// \brief Retrieve the sample rate.
                ///
                /// \return
                ///     The sample rate.
                //-------------------------------------------------------
                inline sampleRate_type
                GetSampleRate( ) const
                {
                    return ( sampleRate );
                }

                //-------------------------------------------------------
                /// \brief Retrieve string of ASCII-based data.
                ///
                /// \return
                ///     The string of ASCII-based data.
                //-------------------------------------------------------
                inline const data_type&
                GetData( ) const
                {
                    return ( data );
                }

                //-------------------------------------------------------
                /// \brief Establish the string of ASCII-based data.
                //-------------------------------------------------------
                inline void
                SetData( const data_type& Data )
                {
                    data = Data;
                }

                //-------------------------------------------------------
                /// \brief Identifier for serial data vector.
                ///
                /// \return
                ///     A constant reference the serial data vector.
                //-------------------------------------------------------
                inline const serial_type&
                RefSerial( ) const
                {
                    return ( serial );
                }

                //-------------------------------------------------------
                /// \brief Identifier for serial data vector.
                ///
                /// \return
                ///     A reference the serial data vector.
                //-------------------------------------------------------
                inline serial_type&
                RefSerial( )
                {
                    return ( serial );
                }

                //-------------------------------------------------------
                /// \brief Identifier for user-defined table structure.
                ///
                /// \return
                ///     A constant reference to the  user-defined table
                ///     structure.
                //-------------------------------------------------------
                inline const table_type&
                RefTable( ) const
                {
                    return ( table );
                }
                //-------------------------------------------------------
                /// \brief Identifier for user-defined table structure.
                ///
                /// \return
                ///     A reference to the  user-defined table structure.
                //-------------------------------------------------------
                inline table_type&
                RefTable( )
                {
                    return ( table );
                }

                //-----------------------------------------------------------------
                /// \brief equality operator
                ///
                /// \param[in] RHS
                ///     The FrSerData object to be compared.
                ///
                /// \return
                ///     The value true is returned if this object is equivelent
                ///     to the RHS instance; false otherwise.
                //-----------------------------------------------------------------
                inline bool
                operator==( const Data& RHS ) const
                {
                    bool const retval = ( ( this == &RHS ) ||
                                          ( ( name.compare( RHS.name ) == 0 ) &&
                                            ( time == RHS.time ) &&
                                            ( sampleRate == RHS.sampleRate ) &&
                                            ( data.compare( RHS.data ) == 0 ) &&
                                            ( serial == RHS.serial ) &&
                                            ( table == RHS.table ) ) );
#if 0
                    std::cerr
                        << "DEBUG: FrSerData::operator==(" << retval << ")"
                        << std::endl //
                        // --------------------------------------------------
                        << " name: " << name << " =?= RHS.name: " << RHS.name
                        << std::endl //
                        // --------------------------------------------------
                        << " time: " << time << " =?= RHS.time: " << RHS.time
                        << std::endl //
                        // --------------------------------------------------
                        << " sampleRate: " << sampleRate
                        << " =?= RHS.sampleRate: " << RHS.sampleRate
                        << std::endl
                        // --------------------------------------------------
                        << " data: " << ( data.compare( RHS.data ) == 0 )
                        << std::endl //
                        // --------------------------------------------------
                        << " serial: " << ( serial == RHS.serial )
                        << std::endl //
                        << " table: " << ( table == RHS.table ) //
                        << std::endl //
                        ;
#endif /* 0  */
                    return retval;
                }

            protected:
                //-------------------------------------------------------
                /// Name of station producing serial data stream
                //-------------------------------------------------------
                name_type name;
                //-------------------------------------------------------
                /// Time of data acquisition,
                /// GPS time in seconds since GPS standard epoch.
                //-------------------------------------------------------
                time_type time;
                //-------------------------------------------------------
                /// Sample rate, samles per second.
                //-------------------------------------------------------
                sampleRate_type sampleRate;
                //-------------------------------------------------------
                /// Pointer to strin for ASCII-based data.
                //-------------------------------------------------------
                data_type data;
                //-------------------------------------------------------
                /// Identifier for serial data vector.
                //-------------------------------------------------------
                serial_type serial;
                //-------------------------------------------------------
                /// Identifier for user-defined table structure.
                //-------------------------------------------------------
                table_type table;
                //=======================================================
                //-------------------------------------------------------
                static constexpr unsigned int MAX_REF{ 2 };

            }; // class Data
        } // namespace FrSerDataImpl
    } // namespace Universal
} // namespace FrameCPP

FR_OBJECT_META_DATA_DECLARE_UNIVERSAL( FrSerDataImpl )

#endif /* ! defined(SWIG) */
#endif /* defined(__cplusplus) */

#endif /* FrameCPP_UNIVERSAL__IMPL__FrSerDataData_HH */
