/* BEGIN software license
 *
 * msXpertSuite - mass spectrometry software suite
 * -----------------------------------------------
 * Copyright(C) 2009,...,2019 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
 */

#pragma once

/////////////////////// StdLib includes


/////////////////////// Qt includes
#include <QDebug>
#include <QObject>
#include <QSharedPointer>
#include <QFile>

/////////////////////// pappsomspp includes


/////////////////////// libXpertMassGui includes


/////////////////////// Local includes
#include "MsXpS/export-import-config.h"
#include "MsXpS/libXpertMassCore/globals.hpp"
#include "MsXpS/libXpertMassCore/IsotopicData.hpp"
#include "MsXpS/libXpertMassCore/IsotopicClusterGenerator.hpp"
#include "MsXpS/libXpertMassCore/MassPeakShaperConfig.hpp"
#include "MsXpS/libXpertMassCore/MonitoredTaskInterface.hpp"


namespace MsXpS
{
namespace libXpertMassCore
{

class DECLSPEC MassSpectrumSynthesizer: public MonitoredTaskInterface
{
  Q_OBJECT

  public:
  // These are the data that are needed to document the context in the thread
  // without messing around in the main GUI thread.
  struct Data
  {
    IsotopicDataSPtr isotopicDataSPtr = nullptr;
    MassPeakShaperConfig massPeakShaperConfig;
    double fixedMzRangeStart = 0;
    double fixedMzRangeStop  = 0;
    std::vector<FormulaChargePair> formula_charge_pairs;
    IsotopicDataType isotopicDataType;
    double cumulatedProbabilities = 0.9;
    double normalizationIntensity = 0;
    QString massSpectrumTitle     = "Untitled synthetic mass spectrum";
    QString massSpectrumColorName;
    ErrorList *errorList = nullptr;

    void
    initialize(const Data &other)
    {
      isotopicDataSPtr = other.isotopicDataSPtr;
      massPeakShaperConfig.initialize(other.massPeakShaperConfig);
      fixedMzRangeStart = other.fixedMzRangeStart;
      fixedMzRangeStop  = other.fixedMzRangeStop;
      formula_charge_pairs.assign(other.formula_charge_pairs.begin(),
                                  other.formula_charge_pairs.end());
      isotopicDataType       = other.isotopicDataType;
      cumulatedProbabilities = other.cumulatedProbabilities;
      normalizationIntensity = other.normalizationIntensity;
      massSpectrumTitle      = other.massSpectrumTitle;
      massSpectrumColorName  = other.massSpectrumColorName;
      errorList              = other.errorList;
    }
  } m_data;

  explicit MassSpectrumSynthesizer(Data data, QObject *parent = nullptr);

  virtual ~MassSpectrumSynthesizer();

  void setData(const Data &data);
  void setUserData(void *user_data_vp);

  void setIsotopicClusterChargePairs(
    std::vector<libXpertMassCore::IsotopicClusterChargePair> &pairs);
  void setIsotopicClusterChargePairs(
    std::vector<libXpertMassCore::IsotopicClusterChargePair> &&pairs);

  void generateIsotopicClusters();
  void shapeIsotopicClusters();

  public slots:

  void run();
  void cancelTask() override;

  signals:

  void finishedSignal(pappso::Trace synthetic_mass_spectrum,
                      QString mass_spectum_title,
                      QString mass_spectrum_color,
                      void *user_data_p = nullptr);

  private:
  bool m_isRunning = false;

  // We do not use these data here, these data are only provided
  // so that we can transfer them to the caller upon ending the
  // thread run withing the finishedSignal() call.
  // This pointer is intended to be reinterpreted_cas by the
  // user of this synthesizer, that know full well what is the
  // type behind the void pointer.
  void *mvp_userData = nullptr;

  // If non-nullptr, all the details about the computation are stored in the
  // file.

  QFile *mp_logFile             = nullptr;
  QTextStream *mp_logTextStream = nullptr;

  std::vector<libXpertMassCore::IsotopicClusterChargePair>
    m_isotopicClusterChargePairs;

  inline void
  logProcessingDetails(const QString &details_string) const
  {
    if(mp_logTextStream != nullptr)
      {
        *mp_logTextStream << details_string;
        mp_logTextStream->flush();
      }
  }
};


} // namespace libXpertMassCore

} // namespace MsXpS
