//
// 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__IMPL__FrTableData_HH
#define FrameCPP__UNIVERSAL__IMPL__FrTableData_HH

#if !defined( SWIGIMPORTED )
#include <string>
#include <vector>
#endif /* ! defined(SWIGIMPORTED) */

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

#include "framecpp/Common/SearchContainer.hh"
#include "framecpp/Common/STRING.hh"

#include "framecpp/Universal/FrVect.hh"

namespace FrameCPP
{
    namespace Universal
    {
        namespace FrTableImpl
        {
            class Data : public Impl::FrObjectMetaData< Data >
            {
            public:
                struct v4
                {
                    static const INT_2U MAX_REF = 1;

                    typedef std::string name_type;
                    typedef std::string comment_type;
                    //---------------------------------------------------
                    /// \brief Collection of column names
                    ///
                    /// This storage class is used to abstract the
                    /// container type used to maintain the collection of
                    /// column names used in the table. The container
                    /// maintains the column order of the column names.
                    //---------------------------------------------------
                    typedef std::vector< std::string >
                        column_name_container_type;
                    //---------------------------------------------------
                    /// \brief Storage class for the number of columns.
                    ///
                    /// This storage class is used to abstract the data
                    /// type used to store the number of columns in the
                    /// table.
                    //---------------------------------------------------
                    typedef INT_2U nColumn_type;
                    //---------------------------------------------------
                    /// \brief Storage class for the number of rows.
                    ///
                    /// This storage class is used to abstract the data
                    /// type used to store the number of rows in the
                    /// table.
                    //---------------------------------------------------
                    typedef INT_4U nRow_type;

                    typedef Common::SearchContainer< FrVect, &FrVect::GetName >
                        column_type;

                    typedef column_type::iterator       iterator;
                    typedef column_type::const_iterator const_iterator;
                };

                using v5 = v4;
                using v6 = v5;
                using v7 = v6;
                using v8 = v7;
                using v9 = v8;

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

                using current_version = v9;

                using name_type = current_version::name_type;
                using comment_type = current_version::comment_type;
                using nColumn_type = current_version::nColumn_type;
                using nRow_type = current_version::nRow_type;

                using column_type = current_version::column_type;
                using iterator = current_version::iterator;
                using const_iterator = current_version::const_iterator;
                using column_name_container_type =
                    current_version::column_name_container_type;

                //---------------------------------------------------------------
                /// \brief Table name
                //---------------------------------------------------------------
                name_type name;
                //---------------------------------------------------------------
                /// \brief Table comment
                //---------------------------------------------------------------
                comment_type comment;

                //---------------------------------------------------------------
                /// Columns of the Table
                //---------------------------------------------------------------
                column_type column;

                //-----------------------------------------------------------------
                /// \brief Add additional information to the comment
                ///
                /// \param[in] comment
                ///     The text to append to the comment.
                //-----------------------------------------------------------------
                inline void
                AppendComment( const comment_type& Comment )
                {
                    FrameCPP::Common::AppendComment( comment, Comment );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the list of column names.
                ///
                /// \return
                ///     The set of column names in column order.
                //-----------------------------------------------------------------
                column_name_container_type
                GetColumnName( ) const
                {
                    column_name_container_type columnName;

                    for ( const_iterator column_element = column.begin( ),
                                         last_column_element = column.end( );
                          column_element != last_column_element;
                          ++column_element )
                    {
                        columnName.push_back( ( *column_element )->GetName( ) );
                    }
                    return columnName;
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the channel comment.
                ///
                /// \return
                ///     The channel comment
                //-----------------------------------------------------------------
                inline const comment_type&
                GetComment( ) const
                {
                    return comment;
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve the channel name.
                ///
                /// \return
                ///     The channel name
                //-----------------------------------------------------------------
                inline const name_type&
                GetName( ) const
                {
                    return name;
                }

                inline nColumn_type
                GetNColumn( ) const
                {
                    return ( ( RefColumn( ).size( ) >= 0 )
                                 ? RefColumn( ).size( )
                                 : 0 );
                }

                inline nRow_type
                GetNRow( ) const
                {
                    try
                    {
                        if ( RefColumn( ).size( ) )
                        {
                            return ( RefColumn( )[ 0 ]->GetNData( ) );
                        }
                    }
                    catch ( ... )
                    {
                    }
                    return ( 0 );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve reference to columns
                ///
                /// \return
                ///     A constant reference to the container of columns.
                //-----------------------------------------------------------------
                const column_type&
                RefColumn( ) const
                {
                    return ( column );
                }

                //-----------------------------------------------------------------
                /// \brief Retrieve reference to columns
                ///
                /// \return
                ///     A reference to the container of columns.
                //-----------------------------------------------------------------
                column_type&
                RefColumn( )
                {
                    return ( column );
                }

                //-----------------------------------------------------------------
                /// \brief equality operator
                ///
                /// \param[in] RHS
                ///     The FrTable 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 equality = true;

                    equality = equality //
                        && ( name == RHS.GetName( ) ) //
                        && ( comment == RHS.GetComment( ) ) //
                        ;

                    return ( equality );
                }

            protected:
                inline void
                setName( const name_type& Name )
                {
                    name = Name;
                }
            };
        } // namespace FrTableImpl
    } // namespace Universal
} // namespace FrameCPP

FR_OBJECT_META_DATA_DECLARE_UNIVERSAL( FrTableImpl )

#endif /* FrameCPP__UNIVERSAL__IMPL__FrTableData_HH */
