/* BEGIN software license
 *
 * msXpertSuite - mass spectrometry software suite
 * -----------------------------------------------
 * Copyright (C) 2009--2020 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
#include <vector>


/////////////////////// Qt includes


/////////////////////// pappsomspp includes
#include <pappsomspp/core/massspectrum/qualifiedmassspectrum.h>
#include <pappsomspp/core/processing/combiners/massspectrumpluscombiner.h>


/////////////////////// libXpertMassCore includes
#include <MsXpS/libXpertMassCore/MonitoredTaskInterface.hpp>


/////////////////////// libXpertMassGui includes
// #include <MsXpS/libXpertMassGui/TaskMonitorCompositeWidget.hpp>


/////////////////////// Local includes
#include "globals.hpp"
#include "MsRunDataSet.hpp"
#include "ProcessingFlow.hpp"

namespace MsXpS
{
namespace MineXpert
{

typedef std::vector<pappso::QualifiedMassSpectrumCstSPtr>
  QualifiedMassSpectraVector;

typedef std::shared_ptr<QualifiedMassSpectraVector>
  QualifiedMassSpectraVectorSPtr;

typedef std::shared_ptr<const QualifiedMassSpectraVector>
  QualifiedMassSpectraVectorCstSPtr;

class QualifiedMassSpectrumVectorMassDataIntegrator
  : public libXpertMassCore::MonitoredTaskInterface
{
  Q_OBJECT

  public:
  QualifiedMassSpectrumVectorMassDataIntegrator(QObject *parent = nullptr);

  QualifiedMassSpectrumVectorMassDataIntegrator(
    MsRunDataSetCstSPtr ms_run_data_set_csp,
    const ProcessingFlow &processing_flow,
    const QualifiedMassSpectraVectorSPtr
      &qualified_mass_spectra_to_integrate_sp,
    QObject *parent = nullptr);


  virtual ~QualifiedMassSpectrumVectorMassDataIntegrator();

  virtual QualifiedMassSpectrumVectorMassDataIntegrator *
  clone(QObject *parent = nullptr) const = 0;

  MsRunDataSetCstSPtr getMsRunDataSet() const;

  void setProcessingFlow(const ProcessingFlow &processing_flow);
  const ProcessingFlow &getProcessingFlowCstRef() const;
  ProcessingFlow &getProcessingFlowRef();
  ProcessingFlow *getProcessingFlow();

  virtual pappso::QualifiedMassSpectrumCstSPtr checkQualifiedMassSpectrum(
    pappso::QualifiedMassSpectrumCstSPtr &qualified_mass_spectrum_csp);

  const pappso::MapTrace &getMapTrace() const;

  virtual void setMaxThreadUseCount(std::size_t count);
  virtual std::size_t getMaxThreadUseCount();

  public slots:

  virtual void integrate() = 0;
  void cancelOperation();


  signals:
  void cancelOperationSignal();

  void setTaskDescriptionTextSignal(QString text);

  void setupProgressBarSignal(std::size_t min_value, std::size_t max_value);
  void setProgressBarMinValueSignal(std::size_t value);
  void setProgressBarMaxValueSignal(std::size_t value);
  void incrementProgressBarMaxValueSignal(std::size_t increment);
  void setProgressBarCurrentValueSignal(std::size_t number_of_processed_nodes);
  void incrementProgressBarCurrentValueSignal(std::size_t increment);

  void setStatusTextSignal(QString text);
  void appendStatusTextSignal(QString text);
  void setStatusTextAndCurrentValueSignal(QString text, int value);

  void
  incrementProgressBarCurrentValueAndSetStatusTextSignal(std::size_t increment,
                                                         const QString &text);

  void logTextToConsoleSignal(QString text);
  void logColoredTextToConsoleSignal(QString text, QColor color);

  void lockTaskMonitorCompositeWidgetSignal();
  void unlockTaskMonitorCompositeWidgetSignal();

  protected:
  MsRunDataSetCstSPtr mcsp_msRunDataSet = nullptr;

  ProcessingFlow *mp_processingFlow;

  std::size_t m_maxThreadUseCount = 0;

  std::vector<ProcessingStepCstSPtr> m_innermostSteps2D;

  std::pair<double, double> m_dtRange =
    std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(),
                              std::numeric_limits<double>::quiet_NaN());
  std::pair<double, double> m_mzRange =
    std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(),
                              std::numeric_limits<double>::quiet_NaN());
  std::pair<double, double> m_rtRange =
    std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(),
                              std::numeric_limits<double>::quiet_NaN());

  const QualifiedMassSpectraVectorSPtr
    mcsp_qualifiedMassSpectraToIntegrateVector;

  pappso::MapTrace m_mapTrace;

  bool m_isOperationCancelled = false;

  std::size_t bestThreadCount();

  // In the pair below, first is the ideal number of threads and second is the
  // number of nodes per thread.
  std::pair<std::size_t, std::size_t>
  bestParallelIntegrationParams(std::size_t node_count);

  virtual void setInnermostRanges();

  virtual std::size_t fillInIntegrationScopeSpecs(
    std::vector<pappso::IntegrationScopeSpec> &integration_scope_specs);

  virtual bool checkMsLevel(
    pappso::QualifiedMassSpectrumCstSPtr &qualified_mass_spectrum_csp);

  virtual bool checkMsFragmentation(
    ProcessingStepCstSPtr processing_step_csp,
    pappso::QualifiedMassSpectrumCstSPtr &qualified_mass_spectrum_csp);

  virtual bool checkMsFragmentation(
    pappso::QualifiedMassSpectrumCstSPtr &qualified_mass_spectrum_csp);

  virtual bool checkRtRange(
    pappso::QualifiedMassSpectrumCstSPtr &qualified_mass_spectrum_csp);

  virtual bool checkDtRange(
    pappso::QualifiedMassSpectrumCstSPtr &qualified_mass_spectrum_csp);

  // We almost systematically need these m/z range values when making visits:
  // one criterium might be that the m/z value of data points (the x member of
  // DataPoint) be contained within the limit m/z range.
  //
  // Note that we set them to min() and max() because we'll use std::min() and
  // std::max() against these values.

  using Iterator =
    std::vector<pappso::QualifiedMassSpectrumCstSPtr>::const_iterator;

  std::vector<std::pair<Iterator, Iterator>>
  calculateIteratorPairs(std::size_t thread_count, int nodes_per_thread);

  std::vector<std::pair<Iterator, Iterator>>
  calculateIteratorPairs(Iterator begin_iterator,
                         Iterator end_iterator,
                         std::size_t thread_count,
                         int nodes_per_thread);

  bool checkInnermostDtRt2DSteps(
    pappso::QualifiedMassSpectrumCstSPtr &qualified_mass_spectrum_csp);
};


} // namespace MineXpert

} // namespace MsXpS
