/****************************************************************************
 *   This file is part of the aGrUM/pyAgrum library.                        *
 *                                                                          *
 *   Copyright (c) 2005-2026 by                                             *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *                                                                          *
 *   The aGrUM/pyAgrum library is free software; you can redistribute it    *
 *   and/or modify it under the terms of either :                           *
 *                                                                          *
 *    - the GNU Lesser General Public License as published by               *
 *      the Free Software Foundation, either version 3 of the License,      *
 *      or (at your option) any later version,                              *
 *    - the MIT license (MIT),                                              *
 *    - or both in dual license, as here.                                   *
 *                                                                          *
 *   (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html)    *
 *                                                                          *
 *   This aGrUM/pyAgrum library is distributed in the hope that it will be  *
 *   useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,          *
 *   INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
 *   FOR A PARTICULAR PURPOSE  AND NONINFRINGEMENT. IN NO EVENT SHALL THE   *
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,        *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  *
 *   OTHER DEALINGS IN THE SOFTWARE.                                        *
 *                                                                          *
 *   See LICENCES for more details.                                         *
 *                                                                          *
 *   SPDX-FileCopyrightText: Copyright 2005-2026                            *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *   SPDX-License-Identifier: LGPL-3.0-or-later OR MIT                      *
 *                                                                          *
 *   Contact  : info_at_agrum_dot_org                                       *
 *   homepage : http://agrum.gitlab.io                                      *
 *   gitlab   : https://gitlab.com/agrumery/agrum                           *
 *                                                                          *
 ****************************************************************************/


/**
 * @file
 * @brief Headers of gum::PRMSlotChain.
 *
 * @author Lionel TORTI and Pierre-Henri WUILLEMIN(_at_LIP6)
 */

#ifndef GUM_SLOT_CHAIN_H
#define GUM_SLOT_CHAIN_H

#include <string>

#include <agrum/PRM/elements/PRMAggregate.h>
#include <agrum/PRM/elements/PRMReferenceSlot.h>

namespace gum {
  namespace prm {

    template < GUM_Numeric GUM_SCALAR >
    class PRMClass;
    template < GUM_Numeric GUM_SCALAR >
    class PRMAttribute;

    /**
     * @class PRMSlotChain
     * @headerfile slotChain.h <agrum/PRM/slotChain.h>
     *
     * @brief A PRMSlotChain represents a sequence of
     *gum::prm::PRMClassElement<GUM_SCALAR>
     *where
     *        the n-1 first gum::prm::PRMClassElement<GUM_SCALAR> are
     *gum::prm::PRMReferenceSlot and
     *        the last gum::prm::PRMClassElement<GUM_SCALAR> an
     *gum::prm::PRMAttribute
     *or
     *an
     *        gum::prm::PRMAggregate.
     *
     * A PRMSlotChain behaves as an gum::prm::PRMAttribute or an
     *gum::prm::PRMAggregate
     * (depending the gum::prm::PRMClassElement<GUM_SCALAR> type of it's last
     *element)
     *regarding
     * the following methods: gum::prm::PRMClassElement<GUM_SCALAR>::type() and
     * gum::prm::PRMClassElement<GUM_SCALAR>::cpf().
     *
     */
    // ==========================================================================
    template < GUM_Numeric GUM_SCALAR >
    class PRMSlotChain: public PRMClassElement< GUM_SCALAR > {
      public:
      // ========================================================================
      /// @name Constructors & destructor
      // ========================================================================
      /// @{

      /** @brief Default constructor.
       *
       * Chain's n-1 first elements must be PRMReferenceSlot and the last
       *element
       * must either be an PRMAttribute or an PRMAggregate.
       *
       * Warning: the last element in chain is copied !
       *
       * @param name The name of this PRMSlotChain.
       * @param chain The chain of gum::prm::PRMClassElement<GUM_SCALAR> in this
       *PRMSlotChain.
       *
       * @throw OperationNotAllowed Raised if the chain contains less than two
       *        PRMClassElement<GUM_SCALAR>.
       * @throw WrongClassElement<GUM_SCALAR> Raised contains invalid
       *PRMClassElement<GUM_SCALAR>.
       */
      PRMSlotChain(std::string_view name, const Sequence< PRMClassElement< GUM_SCALAR >* >& chain);

      /** @brief Tweak constructor.
       *
       * Chain's n-1 first elements must be PRMReferenceSlot and the last
       *element
       * must either be an PRMAttribute or an PRMAggregate.
       *
       * Warning: the last element in chain is copied !
       *
       * @param name The name of this PRMSlotChain.
       * @param chain The chain given to this PRMSlotChain, it is deleted
       *              when PRMSlotChain::~PRMSlotChain() is called.
       *
       * @throw OperationNotAllowed Raised if the chain contains less than two
       *        PRMClassElement<GUM_SCALAR>.
       * @throw WrongClassElement<GUM_SCALAR> Raised contains invalid
       *PRMClassElement<GUM_SCALAR>.
       */
      PRMSlotChain(Sequence< PRMClassElement< GUM_SCALAR >* >* chain, std::string_view name);

      /**
       * Copy constructor.
       *
       * This creates a copy of the slot chain.
       */
      PRMSlotChain(const PRMSlotChain< GUM_SCALAR >& source);

      /// Destructor.
      ~PRMSlotChain() override;

      /// @}
      // ========================================================================
      /// @name Getters & setters
      // ========================================================================
      /// @{

      /// See gum::PRMClassElement<GUM_SCALAR>::elt_type().
      typename PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const override;

      /// This is similar to the following call: this->lastElt().type()
      PRMType& type() override;

      /// This is similar to the following call: this->lastElt().type()
      const PRMType& type() const override;

      // /// This is similar to the following call: this->lastElt().cpf()
      // virtual Tensor<GUM_SCALAR>& cpf();

      /// This is similar to the following call: this->lastElt().cpf()
      const Tensor< GUM_SCALAR >& cpf() const override;

      /// Return true if this slot chain contains at least one multiple
      /// reference slot.
      bool isMultiple() const;

      /// Returns the PRMClassElement<GUM_SCALAR>Container over which this slot
      /// chain ends.
      PRMClassElementContainer< GUM_SCALAR >& end();

      /// Returns the PRMClassElement<GUM_SCALAR>Container over which this slot
      /// chain ends.
      const PRMClassElementContainer< GUM_SCALAR >& end() const;

      /// Returns the last element of the slot chain, typically this is an
      /// gum::PRMAttribute or a gum::PRMAggregate.
      PRMClassElement< GUM_SCALAR >& lastElt();

      /// Returns the last element of the slot chain, typically this is an
      /// gum::PRMAttribute or a gum::PRMAggregate.
      const PRMClassElement< GUM_SCALAR >& lastElt() const;

      /// Return the sequence representing the chain of elements in this
      /// PRMSlotChain.
      Sequence< PRMClassElement< GUM_SCALAR >* >& chain();

      /// Return the sequence representing the chain of elements in this
      /// PRMSlotChain.
      const Sequence< PRMClassElement< GUM_SCALAR >* >& chain() const;

      /// See gum::PRMClassElement<GUM_SCALAR>::addParent_().
      void addParent(const PRMClassElement< GUM_SCALAR >& elt) override;

      /// See gum::PRMClassElement<GUM_SCALAR>::addChild_().
      void addChild(const PRMClassElement< GUM_SCALAR >& elt) override;

      /// @brief Raise a gum::OperationNotAllowed exception
      /// See gum::PRMClassElement<GUM_SCALAR>::getCastDescendant()
      PRMAttribute< GUM_SCALAR >* getCastDescendant() const override;
      /// @}

      private:
      /// Copy operator. Don't use it.
      PRMSlotChain& operator=(const PRMSlotChain& source);

      // ========================================================================
      /// @name Private members of PRMSlotChain.
      // ========================================================================
      /// @{

      /// The sequence of PRMClassElement<GUM_SCALAR> composing the slot chain
      Sequence< PRMClassElement< GUM_SCALAR >* >* _chain_;

      /// Flag indicating if this slot chain is multiple or not.
      bool _isMultiple_;

      /// Copy the last element, this prevents unwanted DuplicateElement
      /// exceptions.
      void _copyLastElt_();

      /// @}
    };


#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
    extern template class PRMSlotChain< double >;
#endif


  } /* namespace prm */
}   // namespace gum

#include <agrum/PRM/elements/PRMSlotChain_tpl.h>

#endif /* GUM_SLOT_CHAIN_H */
