/**
 * \file core/spoms/alignmentbucket.cpp
 * \date 20/02/2026
 * \author Aurélien Berthier, BENOIST Emile, TESSIER Dominique
 * \brief store alignments candidates for post processing
 */


/*
 * PeptidOMS, Spectra to protein alignment tool
 * Copyright (C) 2026  Olivier Langella
 * <olivier.langella@cnrs.fr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "alignmentbucket.h"
#include <memory>
#include <qlogging.h>

namespace pappso
{
namespace spoms
{
AlignmentBucket::AlignmentBucket(int nb_interpretation_saved)
{
  m_nbInterpretationsSaved = nb_interpretation_saved;
}
AlignmentBucket::~AlignmentBucket()
{
}


void
AlignmentBucket::tryToAdd(std::shared_ptr<Alignment> new_alignment)
{
  if(new_alignment.get() != nullptr)
    {
      qDebug()
        << new_alignment.get()->getPeptideModel().get()->toInterpretation()
        << " score:" << new_alignment.get()->getScore();
      if(m_alignmentList.size() < m_nbInterpretationsSaved)
        {
          m_alignmentList.push_back(new_alignment);
          std::sort(m_alignmentList.begin(),
                    m_alignmentList.end(),
                    [](const std::shared_ptr<Alignment> &a,
                       const std::shared_ptr<Alignment> &b) {
                      return b.get()->compareTo(*a.get());
                    });
        }
      else if(m_alignmentList.back().get()->compareTo(*new_alignment.get()))
        {
          m_alignmentList.back() = new_alignment;
          std::sort(m_alignmentList.begin(),
                    m_alignmentList.end(),
                    [](const std::shared_ptr<Alignment> &a,
                       const std::shared_ptr<Alignment> &b) {
                      return b.get()->compareTo(*a.get());
                    });
        }
    }
}

std::shared_ptr<Alignment>
pappso::spoms::AlignmentBucket::getBestAlignment() const
{
  if(m_alignmentList.size() == 0)
    {
      return nullptr;
    }
  return m_alignmentList.front();
}

int
pappso::spoms::AlignmentBucket::getBestAlignmentScore() const
{
  if(m_alignmentList.size() > 0)
    {
      if(m_alignmentList.front() != nullptr)
        {
          return m_alignmentList.front()->getScore();
        }
    }
  return 0;
}


} // namespace spoms
} // namespace pappso
