
/* 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 <chrono>


/////////////////////// Qt includes
#include <QWidget>
#include <QColor>

/////////////////////// QCustomPlot


/////////////////////// libXpertMassCore includes
#include "MsXpS/libXpertMassCore/globals.hpp"

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


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


/////////////////////// Local includes

namespace Ui
{
class TaskMonitorCompositeWidget;
}

namespace MsXpS
{
namespace libXpertMassGui
{

class DECLSPEC TaskMonitorCompositeWidget: public QWidget
{
  Q_OBJECT

  public:
  QColor m_color = Qt::black;

  explicit TaskMonitorCompositeWidget(QWidget *parent);
  explicit TaskMonitorCompositeWidget(QWidget *parent, const QColor &color);
  virtual ~TaskMonitorCompositeWidget();

  void setColor(const QColor &color);
  const QColor &getColor() const;

  void setTimeLag(double time_lag);
  double getTimeLag() const;

  bool shouldUpdate();

  void lock();
  void unlock();
  bool isLocked() const;

  // Make the connection for various classes, using template logic
  // Note that in order to be available at run time, the definition
  // of the function needs to be inlined in the header file.
  template <typename TaskType>
  void
  makeSignalSlotConnections(TaskType *task)
  {
    connect(task,
            &TaskType::taskSetMainIdentifierTextSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetMainIdentifierText);

    connect(task,
            &TaskType::taskSetDescriptionTextSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetDescriptionText);
    connect(task,
            &TaskType::taskSetStatusMessageSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetStatusMessage);

    connect(task,
            &TaskType::taskAppendStatusMessageSignal,
            this,
            &TaskMonitorCompositeWidget::taskAppendStatusMessage);

    connect(task,
            &TaskType::taskClearStatusMessageSignal,
            this,
            &TaskMonitorCompositeWidget::taskClearStatusMessage);

    connect(task,
            &TaskType::taskSetMinValueSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetMinValue);

    connect(task,
            &TaskType::taskSetMaxValueSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetMaxValue);

    connect(task,
            &TaskType::taskSetMinMaxValuesSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetMinMaxValues);

    connect(task,
            &TaskType::taskIncrementMaxValueSignal,
            this,
            &TaskMonitorCompositeWidget::taskIncrementMaxValue);

    connect(task,
            &TaskType::taskSetCurrentValueSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetCurrentValue);

    connect(task,
            &TaskType::taskIncrementCurrentValueSignal,
            this,
            &TaskMonitorCompositeWidget::taskIncrementCurrentValue);

    connect(task,
            &TaskType::taskSetStatusMessageAndCurrentValueSignal,
            this,
            &TaskMonitorCompositeWidget::taskSetStatusMessageAndCurrentValue);

    connect(task,
            &TaskType::taskSetStatusMessageAndIncrementCurrentValueSignal,
            this,
            &TaskMonitorCompositeWidget::
              taskSetStatusMessageAndIncrementCurrentValue);

    connect(task,
            &TaskType::taskCancelledSignal,
            this,
            &TaskMonitorCompositeWidget::taskCancelled,
            Qt::QueuedConnection);

    connect(this,
            &TaskMonitorCompositeWidget::cancelTaskSignal,
            task,
            &TaskType::cancelTask,
            Qt::QueuedConnection);

    connect(task,
            &TaskType::taskFinishedSignal,
            this,
            &TaskMonitorCompositeWidget::taskFinished,
            Qt::QueuedConnection);

    // If the user clicks the Cancel button in the monitor widget,
    // let the monitor task that it was cancelled.
    connect(this,
            &TaskMonitorCompositeWidget::cancelTaskSignal,
            task,
            &TaskType::cancelTask);
  }

  public slots:

  void taskSetMainIdentifierText(const QString &text);
  QString taskGetMainIdentifierText();

  void taskSetDescriptionText(const QString &text);
  void taskClearDescriptionText();
  QString taskGetDescriptionText();

  void taskSetStatusMessage(const QString &message);
  void taskAppendStatusMessage(const QString &message);
  QString taskGetStatusMessage();
  void taskClearStatusMessage();

  void taskSetMinValue(int value);
  void taskSetMaxValue(int value);
  void taskSetMinMaxValues(int min_value, int max_value);
  void taskIncrementMaxValue(int increment);
  void taskSetCurrentValue(int value);
  void taskIncrementCurrentValue(int increment);

  void taskSetStatusMessageAndCurrentValue(const QString &message, int value);
  void taskSetStatusMessageAndIncrementCurrentValue(const QString &message,
                                                    int increment);

  void taskCancelled();
  void taskFinished();


  signals:
  // When the user clicks on the cancel button in the composite widget.
  void cancelTaskSignal();

  protected:
  ::Ui::TaskMonitorCompositeWidget *m_ui;

  void setupWindow();

  private:
  libXpertMassCore::ErrorList *mrp_errorList = new libXpertMassCore::ErrorList;
  // Start with an unlocked widget.
  bool m_isLocked = false;

  double m_timeLag = 50; /* milliseconds */
  std::chrono::system_clock::time_point m_lastTimePoint;
  double m_currentValue = 0;
  QString craftStringIfFormattedText(const QString &text) const;
};


} // namespace libXpertMassGui

} // namespace MsXpS
