/* massXpert - the true massist's program.
   --------------------------------------
   Copyright(C) 2006,2007 Filippo Rusconi

   http://www.massxpert.org/massXpert

   This file is part of the massXpert project.

   The massxpert project is the successor to the "GNU polyxmass"
   project that is an official GNU project package(see
   www.gnu.org). The massXpert project is not endorsed by the GNU
   project, although it is released ---in its entirety--- under the
   GNU General Public License. A huge part of the code in massXpert
   is actually a C++ rewrite of code in GNU polyxmass. As such
   massXpert was started at the Centre National de la Recherche
   Scientifique(FRANCE), that granted me the formal authorization to
   publish it under this Free Software License.

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU  General Public
   License version 3, as published by the Free Software Foundation.
   

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this software; if not, write to the

   Free Software Foundation, Inc.,

   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/


/////////////////////// Local includes
#include "crossLinkList.hpp"
#include "polymer.hpp"


namespace massXpert
{

  CrossLinkList::CrossLinkList(const QString &name, 
				Polymer *polymer,
				const QString &comment)
    : m_name(name), mp_polymer(polymer), m_comment(comment)
  {
  }


  CrossLinkList::CrossLinkList(const CrossLinkList *other)
    : QList<CrossLink *>(*other),
      m_name(other->m_name),
      mp_polymer(other->mp_polymer),
      m_comment(other->m_comment)
  {
    // And now copy all the items from other in here:
    for (int iter = 0; iter < other->size(); ++iter)
      {
	CrossLink *crossLink = other->at(iter);
      
	CrossLink *crossLinkNew = 
	  new CrossLink(crossLink->polChemDef(),
			 crossLink->polymer(),
			 crossLink->name(),
			 crossLink->formula(),
			 crossLink->comment());
      
	append(crossLinkNew);
      }
  }


  CrossLinkList::~CrossLinkList()
  {
    qDeleteAll(begin(), end());
    clear();
  }


  CrossLinkList *
  CrossLinkList::clone() const
  {
    CrossLinkList *other = new CrossLinkList(this);

    return other;
  }


  void 
  CrossLinkList::clone(CrossLinkList *other) const
  {
    Q_ASSERT(other);
  
    if (other == this)
      return;
  
    other->m_name = m_name;
    other->m_comment = m_comment;
    other->mp_polymer = mp_polymer;
  
    // Now empty and clear the other list.
    qDeleteAll(other->begin(), other->end());
    other->clear();
  
    // And now copy all the items to other from here:
    for (int iter = 0; iter < size(); ++iter)
      {
	CrossLink *crossLink = at(iter);
      
	CrossLink *crossLinkNew = 
	  new CrossLink(crossLink->polChemDef(),
			 crossLink->polymer(),
			 crossLink->name(),
			 crossLink->formula(),
			 crossLink->comment());
      
	other->append(crossLinkNew);
      }
  }


  void 
  CrossLinkList::mold(const CrossLinkList &other)
  {
    if (&other == this)
      return;
  
    m_name = other.m_name;
    m_comment = other.m_comment;
    mp_polymer = other.mp_polymer;
  
    // Now empty and clear this list.
    qDeleteAll(begin(), end());
    clear();
  
    // And now copy all the items from other in here:
    for (int iter = 0; iter < other.size(); ++iter)
      {
	CrossLink *crossLink = other.at(iter);
      
	CrossLink *crossLinkNew = 
	  new CrossLink(crossLink->polChemDef(),
			 crossLink->polymer(),
			 crossLink->name(),
			 crossLink->formula(),
			 crossLink->comment());
      
	append(crossLinkNew);
      }
  }


  CrossLinkList &
  CrossLinkList::operator =(const CrossLinkList &other)
  {
    if (&other != this)
      mold(other);
  
    return *this;
  }


  //! Sets the name.
  /*!  \param name new name.
   */
  void
  CrossLinkList::setName(QString name)
  {
    if (!name.isEmpty())
      m_name = name;
  }


  QString
  CrossLinkList::name()
  {
    return m_name;
  }


  //! Sets the comment.
  /*!  \param comment new comment.
   */
  void
  CrossLinkList::setComment(QString comment)
  {
    if (!comment.isEmpty())
      m_comment = comment;
  }


  const QString &
  CrossLinkList::comment() const
  {
    return m_comment;
  }


  void
  CrossLinkList::setPolymer(Polymer *polymer)
  {
    mp_polymer = polymer;
  }


  const Polymer *
  CrossLinkList::polymer() const
  {
    return mp_polymer;
  }


  int 
  CrossLinkList::crossLinksInvolvingMonomer(const Monomer *monomer,
					     QList<int> *list) const
  {
    int count = 0;
    
    for (int iter = 0; iter < size(); ++iter)
      {
	CrossLink *crossLink = at(iter);
      
	for(int jter = 0; jter < crossLink->monomerList()->size(); ++jter)
	  {
	    const Monomer *iterMonomer = crossLink->monomerList()->at(jter);
	  
	    if (monomer == iterMonomer)
	      {
		count ++;
	      
		if(list)
		  list->append(iter);
	      }
	  }
      }
  
    return count;
  }

} // namespace massXpert
