/**
 * \file core/spoms/location.cpp
 * \date 18/02/2026
 * \author Aurélien Berthier, BENOIST Emile, TESSIER Dominique
 * \brief store alignment interpretations
 */


/*
 * 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 "location.h"
#include <pappsomspp/core/exception/exceptionoutofrange.h>
#include <qcontainerfwd.h>
#include <qlogging.h>

namespace pappso
{
namespace spoms
{


/*
public Location(int[] protein_sequence, int beginning, int end)
{
  this.protein_sequence = protein_sequence;
  this.beginning = Math.max(0, beginning);
  this.length = Math.min(protein_sequence.length - 1 ,end) - beginning + 1;
}
*/

Location::Location(const std::vector<SpomsAa> &protein_sequence_in,
                   int beginning_in,
                   int end_in)
  : protein_sequence(protein_sequence_in)
{
  qDebug() << protein_sequence.size();
  beginning = std::max(0, beginning_in);
  length =
    std::min((int)protein_sequence_in.size() - 1, end_in) - beginning + 1;
  //
}

Location::Location(const Location &other)
  : protein_sequence(other.protein_sequence)
{
  beginning = other.beginning;
  length    = other.length;
}

Location::~Location()
{
}

int
Location::getStartPositionOnProtein(const QString &protein_sequence,
                                    const QString &peptide_sequence) const
{
  // qWarning() << "length:" << peptide_length;
  int near_pos =
    (int)protein_sequence.size() - beginning - peptide_sequence.size();
  int position      = 0;
  int position_best = 0;
  while(position >= 0)
    {
      position = protein_sequence.indexOf(peptide_sequence, position + 1);
      if(abs(position - near_pos) < abs(position_best - near_pos))
        {
          position_best = position;
        }
    }
  return (int)position_best;
}

int
Location::getLength() const
{
  return length;
}

const SpomsAa &
Location::get(int position) const
{
  if(position + beginning >= (int)protein_sequence.size())
    {
      throw pappso::ExceptionOutOfRange(
        QObject::tr("SpomsAa position %1 out of range (begining:%2 length:%3 "
                    "protein size:%4)")
          .arg(position)
          .arg(beginning)
          .arg(length)
          .arg(protein_sequence.size()));
    }
  return protein_sequence.at(position + beginning);
}
QString
Location::toString() const
{
  qDebug() << length;
  QString ans = "";
  for(int i = 0; i < length; ++i)
    {
      ans.append((char)get(i).aa);
    }
  return ans;
}

} // namespace spoms
} // namespace pappso
