//
// 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__FrStatDataData_HH
#define FrameCPP_UNIVERSAL__IMPL__FrStatDataData_HH

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

#include "framecpp/Common/SearchContainer.hh"

#include "framecpp/Universal/FrDetector.hh"
#include "framecpp/Universal/FrTable.hh"

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

namespace FrameCPP
{
    namespace Universal
    {
        namespace FrStatDataImpl
        {
            class Data : public Impl::FrObjectMetaData< Data >
            {
            public:
                struct v3
                {
                    using name_type = std::string;
                    using comment_type = std::string;
                    using timeStart_type = INT_4U;
                    using timeEnd_type = INT_4U;
                    using version_type = INT_4U;

                    using detector_type = boost::shared_ptr< FrDetector >;
                    using const_detector_type =
                        boost::shared_ptr< FrDetector > const;

                    using data_type =
                        Common::SearchContainer< FrVect, &FrVect::GetName >;
                    using iterator = data_type::iterator;
                    using const_iterator = data_type::const_iterator;
                };
                struct v4
                {
                    using name_type = v3::name_type;
                    using comment_type = v3::comment_type;
                    using regresentation_type = std::string;
                    using timeEnd_type = v3::timeEnd_type;
                    using timeStart_type = v3::timeStart_type;
                    using version_type = v3::version_type;

                    using const_detector_type = v3::const_detector_type;
                    using detector_type = v3::detector_type;

                    using const_iterator = v3::const_iterator;
                    using data_type = v3::data_type;
                    using iterator = v3::iterator;

                    using table_type =
                        Common::SearchContainer< FrTable,
                                                 &FrTable::GetNameSlow >;
                    using table_iterator = table_type::iterator;
                    using const_table_iterator = table_type::const_iterator;
                };
                using v5 = v4;
                using v6 = v5;
                using v7 = v6;
                using v8 = v7;

                struct v9
                {
                    //-------------------------------------------------------
                    // This structure has been removed in version 9 of the
                    //   frame specification.
                    //-------------------------------------------------------
                };

                //---------------------------------------------------------
                // NOTE:
                //   Even though this structure has been removed in
                //   version 9 of the frame specification, compatability
                //   with version 8 has been maintained to read and write
                //   legacy frames.
                //---------------------------------------------------------
                using current_version = v8;

                using comment_type = current_version::comment_type;
                using name_type = current_version::name_type;
                using representation_type =
                    current_version::regresentation_type;
                using timeEnd_type = current_version::timeEnd_type;
                using timeStart_type = current_version::timeStart_type;
                using version_type = current_version::version_type;

                using const_detector_type =
                    current_version::const_detector_type;
                using detector_type = current_version::detector_type;

                using const_iterator = current_version::const_iterator;
                using data_type = current_version::data_type;
                using iterator = current_version::iterator;

                using const_table_iterator =
                    current_version::const_table_iterator;
                using table_iterator = current_version::table_iterator;
                using table_type = current_version::table_type;

                //-----------------------------------------------------------------
                /// \brief Retrieve static data name
                ///
                /// \return
                ///     The static data name
                //-----------------------------------------------------------------
                inline const std::string&
                GetName( ) const
                {
                    return ( name );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the comment
                ///
                /// \return
                ///     The comment
                //-----------------------------------------------------------------
                inline const std::string&
                GetComment( ) const
                {
                    return ( comment );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the type of static data
                ///
                /// \return
                ///     The type of static data being represente
                //-----------------------------------------------------------------
                inline const std::string&
                GetRepresentation( ) const
                {
                    return ( representation );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the start time
                ///
                /// \return
                ///     The start time of the static data validity.
                //-----------------------------------------------------------------
                inline timeStart_type
                GetTimeStart( ) const
                {
                    return ( timeStart );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the end time
                ///
                /// \return
                ///     The end time of the static data validity.
                //-----------------------------------------------------------------
                inline timeEnd_type
                GetTimeEnd( ) const
                {
                    return ( timeEnd );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the version number
                ///
                /// \return
                ///    The version number for this static structure.
                //-----------------------------------------------------------------
                inline version_type
                GetVersion( ) const
                {
                    return ( version );
                }

                inline detector_type
                GetDetector( )
                {
                    return detector;
                }

                inline const_detector_type
                GetDetector( ) const
                {
                    return detector;
                }

                inline const data_type&
                RefData( ) const
                {
                    return data;
                }

                inline data_type&
                RefData( )
                {
                    return data;
                }

                inline const table_type&
                RefTable( ) const
                {
                    return table;
                }

                inline table_type&
                RefTable( )
                {
                    return table;
                }

                inline detector_type*
                AddressOfDetector( ) const
                {
                    return &( const_cast< Data* >( this )->detector );
                }

                inline void
                SetDetector( detector_type Detector )
                {
                    detector = Detector;
                }

                inline bool
                operator==( const Data& RHS ) const
                {
                    return (
                        ( this == &RHS ) ||
                        ( ( name == RHS.name ) && ( comment == RHS.comment ) &&
                          ( representation == RHS.representation ) &&
                          ( timeStart == RHS.timeStart ) &&
                          ( timeEnd == RHS.timeEnd ) &&
                          ( version == RHS.version ) &&
                          ( ( detector == RHS.detector ) ||
                            ( detector && RHS.detector &&
                              ( *detector == *( RHS.detector ) ) ) ) &&
                          ( data == RHS.data ) && ( table == RHS.table ) ) );
                }

            protected:
                //---------------------------------------------------------------
                /// Static data name
                //---------------------------------------------------------------
                name_type name;
                //---------------------------------------------------------------
                /// Comment for static data
                //---------------------------------------------------------------
                comment_type comment;
                //---------------------------------------------------------------
                /// Type of static data being represented. e,g., calibration,
                /// swept sine, pole-zero, FIR or IIR coefficients...
                //---------------------------------------------------------------
                representation_type representation;
                //---------------------------------------------------------------
                /// Start time of static data validity.
                /// GPS time in integer seconds since GPS standard epoch.
                //---------------------------------------------------------------
                timeStart_type timeStart;
                //---------------------------------------------------------------
                /// End time of static data validity.
                /// A value of zero represents the end time being unknown.
                /// GPS time in integer seconds since GPS standard epoch.
                //---------------------------------------------------------------
                timeEnd_type timeEnd;
                //---------------------------------------------------------------
                /// Version number for this static structure.
                /// i.e., the counter begins at 0 and is incremented by 1
                /// thereafter.
                /// Updated statics for the same time window
                /// (e.g., modified calibration data) will be identified by
                /// identified by unique version numbers.
                //---------------------------------------------------------------
                version_type version;

                detector_type detector;
                data_type     data;
                table_type    table;
                //=======================================================
                //-------------------------------------------------------
                static constexpr unsigned int MAX_REF{ 3 };
            }; // class Data
        } // namespace FrStatDataImpl
    } // namespace Universal
} // namespace FrameCPP

FR_OBJECT_META_DATA_DECLARE_UNIVERSAL( FrStatDataImpl )

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

#endif /* FrameCPP_UNIVERSAL__IMPL__FrStatDataData_HH */
