/*---------------------------------------------------------------------------*\
  ==  == ====== ====   ====    |
                    \\     ||  | Multiphase Code Repository by HZDR
  ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.767
  ||  ||  //    ||  // || \\   | License: GPL-3.0-or-later
  ==  == ====== ====   ==  ==  |
-------------------------------------------------------------------------------
License
    This file is part of the Multiphase Code Repository by HZDR.

    Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
    Website: https://hzdr.de

    Multiphase Code Repository by HZDR is based on the free software for
    computational fluid dynamics (CFD) from the OpenFOAM Foundation.
    Copyright (C) 2025 by OpenFOAM Foundation, Website: https://openfoam.org

    If you are interested in which files are original OpenFOAM Foundation
    files, which OpenFOAM Foundation files were modified, and which files were
    newly created, see FILES.md.

    Multiphase Code Repository by HZDR 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.

    Multiphase Code Repository by HZDR 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 the Multiphase Code Repository by HZDR. If not, see
    <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "window.H.qt"
#include <QLayout>
#include <QValueAxis>
#include <QLogValueAxis>

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::window::window
(
    wordList& fieldNames,
    QWidget *parent
)
:
    QWidget(parent)
{
    // Create a 2 rows 3 columns grid layout
    QGridLayout *layout = new QGridLayout;

    // Progress bar spanning 2 columns
    QProgressBar *progressBar = new QProgressBar();
    progressBar->setRange(0, 100);
    layout->addWidget(progressBar, 0, 0, 1, 2);
    connect
    (
        this,
        SIGNAL(setProgress(int)),
        progressBar,
        SLOT(setValue(int))
    );

    // Label to show estimated time
    QLabel *estimatedTime = new QLabel("00h 00min 00s");
    layout->addWidget(estimatedTime, 0, 2, 1, 1, Qt::AlignHCenter);
    connect
    (
        this,
        SIGNAL(setEstimatedTime(QString)),
        estimatedTime,
        SLOT(setText(QString))
    );

    // Diagram with logarithmic y axis to plot residuals spanning 3 columns
    chart_ = new QtCharts::QChart;
    QtCharts::QValueAxis* axisX = new QtCharts::QValueAxis();
    QtCharts::QLogValueAxis* axisY = new QtCharts::QLogValueAxis();
    chart_->addAxis(axisX, Qt::AlignBottom);
    chart_->addAxis(axisY, Qt::AlignLeft);

    axisX->setTitleText("Runtime in s");
    axisX->setLabelFormat("%g");

    axisY->setTitleText("Residual");
    axisY->setLabelFormat("%g");
    axisY->setBase(10.0);
    axisY->setRange(0.001, 1);
    axisY->setMinorTickCount(-1);

    forAll(fieldNames, fieldi)
    {
        QtCharts::QLineSeries* series(new QtCharts::QLineSeries);
        series->setName(fieldNames[fieldi].c_str());
        chart_->addSeries(series);
        series->attachAxis(chart_->axes()[0]);
        series->attachAxis(chart_->axes()[1]);
    }

    QtCharts::QChartView *chartView = new QtCharts::QChartView(chart_);
    chartView->setRenderHint(QPainter::Antialiasing);
    layout->addWidget(chartView, 1, 0, 1, 3);

    resize(500, 500);
    setLayout(layout);
    show();
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
Foam::window::~window()
{}


// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //

void Foam::window::setResidual(word field, scalar time, scalar value)
{
    for(int i=0; i<chart_->series().size(); i++)
    {
        QtCharts::QLineSeries* series =
            dynamic_cast<QtCharts::QLineSeries*>(chart_->series()[i]);

        if (series->name() == QString(field.c_str()))
        {
            QtCharts::QLogValueAxis* axisY =
                dynamic_cast<QtCharts::QLogValueAxis*>(chart_->axes()[1]);

            if (value < axisY->min())
            {
                axisY->setMin(pow(10,floor(log10(value))));
            }

            series->append(time, value);
            break;
        }
    }
}


void Foam::window::setTimeRange(scalar min, scalar max)
{
    QtCharts::QValueAxis* axisX =
        dynamic_cast<QtCharts::QValueAxis*>(chart_->axes()[0]);

    axisX->setRange(min, max);
}


// ************************************************************************* //
