/*******************************************************************************
 Copyright 2017 Daniel Neuwirth
 This program is distributed under the terms of the GNU General Public License.
*******************************************************************************/

#ifndef UI_CONDITIONS_H
#define UI_CONDITIONS_H

// user interface for ConditionsWindow class


#include "compute.h"
#include "conditions.h"

#include <QDialog>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSpacerItem>
#include <QString>
#include <QVBoxLayout>
#include <QVector>

class InitialConditionsUI {

    public:
        InitialConditionsUI() = default;
        InitialConditionsUI(const QString & inDiffVariable,
                            const QString & atPoint = "0",
                            const QString & functionValue = ""):
            label_Variable(new QLabel(inDiffVariable)),
            lineEdit_ValueAtPoint(new QLineEdit(atPoint)),
            lineEdit_ValueOfDiffVar(new QLineEdit(functionValue)) {}
        ~InitialConditionsUI() {}

        inline static uint8_t rowHeight() { return minRowHeightInGrid; }

        QLabel * label_Variable;
        QLineEdit * lineEdit_ValueAtPoint;
        QLineEdit * lineEdit_ValueOfDiffVar;

    private:
        static const uint8_t minRowHeightInGrid = 25;
};

class Ui_ConditionsWindow {

    public:
        QVBoxLayout * verticalLayout_win;
        QGridLayout * gridLayout_table;
        QHBoxLayout * horizontalLayout_lastRow;

        QVector<InitialConditionsUI> individualRowsUI;

        QPushButton * pushButton_Cancel;
        QIcon * iconCancel;
        QPushButton * pushButton_OK;
        QIcon * iconOK;

        void setupUi(QDialog * ConditionsWindow,
                     QVector<InitConditions> & inConditions,
                     const QChar inDiffSign, const uint8_t inRows) {

            // properties of dialog window
            ConditionsWindow->setMinimumSize
                (180,65+inRows*InitialConditionsUI::rowHeight());
            ConditionsWindow->resize
                (220,65+inRows*InitialConditionsUI::rowHeight());
            ConditionsWindow->setWindowTitle(QStringLiteral("Initial conditions"));

            // rows (all rows from first to penultimate) - contents
            individualRowsUI.reserve(inRows);
            for (uint8_t i=0; i<inRows; ++i) {

                // determine contents for individual elements of this dialog
                const QString varSymbol =
                    inDiffSign + format::expressDerivativesAsSymbols(i)+" (";
                QString xValue = "0", functionValue = "";
                if (!inConditions.isEmpty()) {
                    // if any conditions have been already set, fetch their values
                    xValue =
                      format::numberForOutput(inConditions.at(i).getXValue());
                    functionValue =
                      format::numberForOutput(inConditions.at(i).getFunctionValue());
                }
                InitialConditionsUI currentRow(varSymbol, xValue, functionValue);
                individualRowsUI.push_back(currentRow);
            }

            // rows (all rows from first to penultimate) - layout
            gridLayout_table = new QGridLayout(); // inRows x 5
            uint8_t rowNumber = 0;
            for (auto it : individualRowsUI) {

                gridLayout_table->setRowMinimumHeight
                    (rowNumber,InitialConditionsUI::rowHeight());
                gridLayout_table->addWidget(it.label_Variable,rowNumber,0);
                gridLayout_table->addWidget(it.lineEdit_ValueAtPoint,rowNumber,1);
                gridLayout_table->addWidget(new QLabel(") ="),rowNumber,2);
                gridLayout_table->addItem(new QSpacerItem(50,1),rowNumber,3);
                gridLayout_table->addWidget(it.lineEdit_ValueOfDiffVar,rowNumber++,4);
            }

            // last row - contents
            pushButton_Cancel = new QPushButton(QStringLiteral("&Cancel"));
            iconCancel = new QIcon();
            iconCancel->addFile(QStringLiteral(":/icons/icons/cancel.ico"), QSize(),
                                QIcon::Normal, QIcon::Off);
            pushButton_Cancel->setIcon(*iconCancel);
            pushButton_OK = new QPushButton(QStringLiteral("&OK"));
            pushButton_OK->setDefault(true);
            iconOK = new QIcon();
            iconOK->addFile(QStringLiteral(":/icons/icons/ok.ico"), QSize(),
                            QIcon::Normal, QIcon::Off);
            pushButton_OK->setIcon(*iconOK);

            // last row - layout
            horizontalLayout_lastRow = new QHBoxLayout();
            horizontalLayout_lastRow->addWidget(pushButton_Cancel);
            horizontalLayout_lastRow->addWidget(pushButton_OK);
            horizontalLayout_lastRow->insertStretch(0,0);

            // vertical layout
            verticalLayout_win = new QVBoxLayout(ConditionsWindow);
            verticalLayout_win->addLayout(gridLayout_table);
            verticalLayout_win->addLayout(horizontalLayout_lastRow);
            verticalLayout_win->insertSpacing(1,10);

            QMetaObject::connectSlotsByName(ConditionsWindow);
        }
};

#endif // UI_CONDITIONS_H
