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

#ifndef UI_MATRIX_H
#define UI_MATRIX_H

// user interface for MatrixWindow class


#include "token.h"

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

class FundamentalMatrixUI {

    public:
        FundamentalMatrixUI() = default;
        FundamentalMatrixUI(QLabel * const label, QLineEdit * const lineEdit):
            description(label), value(lineEdit) {}
        ~FundamentalMatrixUI() {}

        inline static uint8_t rowHeight() { return minRowHeightInGrid; }
        inline static uint8_t columnWidth() { return minColumnWidthInGrid; }

        QLabel * description;
        QLineEdit * value;

    private:
        static const uint8_t minRowHeightInGrid = 25;
        static const uint8_t minColumnWidthInGrid = 50;
};

class Ui_MatrixWindow {

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

        QLabel * label_whatIsThisMatrixFor;
        QPushButton * pushButton_OK;
        QIcon * iconOK;

        void setupUi(QDialog * MatrixWindow, const QChar inDiffVariable,
                     const Matrix<double_t> & inMatrix) {

            // properties of dialog window
            const uint8_t size = inMatrix.size();
            const uint8_t height = size*FundamentalMatrixUI::rowHeight();
            const uint8_t width = size*FundamentalMatrixUI::columnWidth();
            MatrixWindow->setMinimumSize(300,120);
            MatrixWindow->resize(200+width,70+height);
            const QString dimensions =
                '(' + QString::number(size) + 'x' + QString::number(size) + ')';
            MatrixWindow->setWindowTitle(QStringLiteral("Fundamental matrix ")+dimensions);

            // rows (all rows from first to penultimate) - contents
            uint8_t row = 0;
            QVector<QVector<FundamentalMatrixUI>> matrix(size);
            for (auto & it : matrix) {

                QVector<FundamentalMatrixUI> individualRowsUI;
                for (uint8_t column = 0; column < size; ++column) {

                    const QString index = QStringLiteral("<sub>") +
                                          QString::number(column+1) +
                                          QStringLiteral("</sub>");

                    QLabel * description =
                        new QLabel(inDiffVariable + index +
                            format::expressDerivativesAsSymbols(row) + ":");
                    QLineEdit * value = new QLineEdit;
                    const QString numValue =
                        format::numberForOutput(inMatrix.getValue(row,column));
                    value->setText(numValue);
                    value->setReadOnly(true);

                    const FundamentalMatrixUI matrixElement(description, value);
                    individualRowsUI.push_back(matrixElement);
                }
                it = individualRowsUI;
                ++row;
            }

            // rows (all rows from first to penultimate) - layout
            gridLayout_table = new QGridLayout(); // size x size
            for (uint8_t row = 0; row < size; ++row) {

                gridLayout_table->setRowMinimumHeight(row, height);
                for (uint8_t column = 0; column < size; ++column) {

                    gridLayout_table->
                        addWidget(matrix[row][column].description, row, column*2);
                    gridLayout_table->
                        addWidget(matrix[row][column].value, row, column*2+1);
                }
            }

            // last row - contents
            label_whatIsThisMatrixFor = new QLabel(
                QStringLiteral("determinant of this matrix = wronskian"));
            pushButton_OK = new QPushButton(QStringLiteral("&OK"));
            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(label_whatIsThisMatrixFor);
            horizontalLayout_lastRow->addWidget(pushButton_OK);
            horizontalLayout_lastRow->insertStretch(1);

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

            QMetaObject::connectSlotsByName(MatrixWindow);
        }
};

#endif // UI_MATRIX_H
