/* BEGIN software license
 *
 * MsXpertSuite - mass spectrometry software suite
 * -----------------------------------------------
 * Copyright 2009--2026 by 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


/////////////////////// Qt includes
#include <QString>

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


/////////////////////// Local includes
#include "MsXpS/export-import-config.h"

#include "MsXpS/libXpertMassCore/jsclassregistrar.h"
#include "MsXpS/libXpertMassCore/globals.hpp"
#include "MsXpS/libXpertMassCore/Formula.hpp"


namespace MsXpS
{
namespace libXpertMassCore
{

/*  BEGIN CLASS JS REFERENCE
 *  namespace: MsXpS::libXpertMassCore
 *  class name: Averagine
 */
class DECLSPEC Averagine: public QObject
{
  Q_OBJECT

  Q_PROPERTY(
    Formula formula WRITE setFormula READ getFormula NOTIFY formulaChanged)
  Q_PROPERTY(IsotopicDataCstSPtr isotopicData WRITE setIsotopicData READ
               getIsotopicData NOTIFY isotopicDataChanged)

  public:
  Q_INVOKABLE explicit Averagine(QObject *parent = nullptr);
  Q_INVOKABLE explicit Averagine(const Formula &formula,
                                 QObject *parent = nullptr);
  Q_INVOKABLE explicit Averagine(IsotopicDataCstSPtr isotopic_data_csp,
                                 QObject *parent = nullptr);
  Q_INVOKABLE explicit Averagine(const Formula &formula,
                                 IsotopicDataCstSPtr isotopic_data_csp,
                                 QObject *parent = nullptr);

  // Pseudo copy constructor
  Q_INVOKABLE explicit Averagine(const Averagine &other,
                                 QObject *parent = nullptr);
  Q_INVOKABLE Averagine &initialize(const Averagine &other);
  Q_INVOKABLE Averagine *clone(QObject *parent = nullptr);
  Q_INVOKABLE static Averagine *clone(const Averagine &other,
                                      QObject *parent = nullptr);

  virtual ~Averagine();

  Q_INVOKABLE void setFormula(const QString &formula);
  Q_INVOKABLE void setFormula(const Formula &formula);
  Q_INVOKABLE const Formula &getFormulaCstRef() const;
  Q_INVOKABLE const Formula &getFormula() const;

  Q_INVOKABLE double getMonoMass() const;
  Q_INVOKABLE double getAvgMass() const;

  Q_INVOKABLE void setIsotopicData(const IsotopicDataCstSPtr isotopic_data_csp);
  Q_INVOKABLE const IsotopicDataCstSPtr getIsotopicData() const;

  Q_INVOKABLE Averagine &operator=(const Averagine &other) = delete;
  Q_INVOKABLE bool operator==(const Averagine &other) const;
  Q_INVOKABLE bool operator!=(const Averagine &other) const;

  Q_INVOKABLE double
  averagineEquivalents(Enums::MassType mass_type, double mass, bool &ok) const;

  Q_INVOKABLE QString elementalComposition(Enums::MassType mass_type,
                                           double mass,
                                           bool as_integer_indices,
                                           bool &ok) const;

  Q_INVOKABLE bool validate(ErrorList *error_list_p);
  Q_INVOKABLE bool isValid() const;
  Q_INVOKABLE QString toString() const;

  static void registerJsConstructor(QJSEngine *engine);

  signals:

  void formulaChanged();
  void isotopicDataChanged();

  protected:
  Formula m_formula;
  IsotopicDataCstSPtr mcsp_isotopicData;

  double m_mono = 0;
  double m_avg  = 0;

  bool m_isValid = false;
};

/*  END CLASS JS REFERENCE
 *  namespace: MsXpS::libXpertMassCore
 *  class name: Averagine
 */

} // namespace libXpertMassCore
MSXPS_REGISTER_JS_CLASS(MsXpS::libXpertMassCore, Averagine)
} // namespace MsXpS

Q_DECLARE_METATYPE(MsXpS::libXpertMassCore::Averagine);
extern int averagineMetaTypeId;
