/* BEGIN software license
 *
 * MsXpertSuite - mass spectrometry software suite
 * -----------------------------------------------
 * Copyright (C) 2009--2026 Filippo Rusconi
 *
 * http://www.msxpertsuite.org
 *
 * This file is part of the MsXpertSuite project.
 *
 * The MsXpertSuite project is the successor of the massXpert project. This
 * project now includes various independent modules:
 *
 * - massXpert, model polymer chemistries and simulate mass spectrometric data;
 * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner;
 *
 * 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/>.
 *
 * END software license
 */


/////////////////////// stdlib includes


/////////////////////// Qt includes
#include <QList>
#include <QMap>
#include <QFileInfo>


/////////////////////// pappsomspp includes
#include "pappsomspp/core/trace/trace.h"


/////////////////////// Local includes
#include "MsXpS/libXpertMassCore/LowMassDeconvolverWorker.hpp"
#include "MsXpS/libXpertMassCore/LowMassDeconvolverNoLabelling.hpp"
#include "MsXpS/libXpertMassCore/LowMassDeconvolverFullLabelling.hpp"

namespace MsXpS
{
namespace libXpertMassCore
{
LowMassDeconvolverWorker::LowMassDeconvolverWorker(QObject *parent)
  : QObject(parent)
{
}

void
LowMassDeconvolverWorker::setData(const LowMassDeconvolverWorker::Data &data)
{
  m_data = data;
}

void
LowMassDeconvolverWorker::LowMassDeconvolverWorker::process()
{
  emit statusMessageSignal("Starting deconvolution...");
  emit progressSignal(0);

  QList<libXpertMassCore::DeconvolutedFeature> deconvoluted_features;

  try
    {
      if(m_data.fullHeavyIsotopeLabelling)
        {
          emit statusMessageSignal(
            "Using full heavy isotope labelling deconvolution...");

          libXpertMassCore::LowMassDeconvolverFullLabelling deconvolver(
            m_data.deconvolverParams);

          emit progressSignal(30);

          deconvoluted_features = deconvolver.deconvolute(m_data.centroids);
        }
      else
        {
          emit statusMessageSignal("Using no labelling deconvolution...");

          libXpertMassCore::LowMassDeconvolverNoLabelling deconvolver(
            m_data.deconvolverParams);

          emit progressSignal(30);

          deconvoluted_features = deconvolver.deconvolute(m_data.centroids);
        }

      emit progressSignal(100);
      emit statusMessageSignal(
        QString("Deconvolution complete: %1 features found")
          .arg(deconvoluted_features.size()));

      // Pass all the necessary data back to the GUI thread
      emit finishedSignal(deconvoluted_features,
                          m_data.centroidsSourcePlottable,
                          m_data.sampleName,
                          m_data.sampleColorString,
                          m_data.shouldSynthesizeMassSpectrum,
                          m_data.shouldIdentifyAnalytes);
    }
  catch(const std::exception &e)
    {
      emit errorSignal(QString("Deconvolution error: %1").arg(e.what()));
    }
  catch(...)
    {
      emit errorSignal("Unknown error during deconvolution");
    }
}

} // namespace libXpertMassCore
} // namespace MsXpS
