/***************************************************************************
**
**  This file is part of QGpCoreStat.
**
**  QGpCoreStat 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.
**
**  QGpCoreStat 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 Foobar.  If not, see <http://www.gnu.org/licenses/>
**
**  See http://www.geopsy.org for more information.
**
**  Created: 2017-06-11
**  Copyright: 2017-2019
**    Marc Wathelet (ISTerre, Grenoble, France)
**
***************************************************************************/

#ifndef HISTOGRAMDENSITIES_H
#define HISTOGRAMDENSITIES_H

#include <QGpCoreMath.h>
#include <DinverCore.h>

#include "Densities.h"
#include "GaussianMixtureParameters.h"
#include "QGpCoreStatDLLExport.h"

namespace QGpCoreStat {

  class QGPCORESTAT_EXPORT HistogramDensities : public QObject
  {
    Q_OBJECT
  public:
    HistogramDensities(QObject * parent=nullptr);
    ~HistogramDensities();

    void setParameters(const GaussianMixtureParameters& p) {_parameters=p;}
    const GaussianMixtureParameters& parameters() const {return _parameters;}

    void setHistogram(Histogram2D * h);
    void start();
    void start(int index);
    void wait();

    const Histogram2D * histogram() const {return _histogram;}
    const Densities& densities(int index) const {return _densities[index];}
    int bestMisfitIndex() const {return _na->bestModelIndex();}
    double bestMisfit() const {return _na->misfit(bestMisfitIndex());}
    int modelCount() const {return _na->allModels()->count();}
  signals:
    void histogramFinished(int index);
    void finished();
  private slots:
    void monteCarloFinished();
    void neighborhoodFinished();
  private:
    void internalStart();

    GaussianMixtureParameters _parameters;
    Histogram2D * _histogram;

    int _currentIndex;
    int _stopIndex;

    ModelRepository *_na;
    Densities * _densities;
  };

} // namespace QGpCoreStat

#endif // HISTOGRAMDENSITIES_H

