#include <QtWidgets>

#include "colorcommand.h"
#include "mainwindow.h"
#include "scribblearea.h"
#include "color_constants.h"
#include "colordatamodel.h"
#include <QApplication>
#include <QWidget>

class Ui_Dialog:QDialog
{
public:
    QVBoxLayout *verticalLayout_7;
    QVBoxLayout *verticalLayout;
    QVBoxLayout *verticalLayout_2;
    QLabel *label;
    QVBoxLayout *verticalLayout_3;
    QLabel *label_2;
    QVBoxLayout *verticalLayout_4;
    QLabel *label_3;
    QVBoxLayout *verticalLayout_5;
    QLabel *label_4;
    QVBoxLayout *verticalLayout_6;
    QLabel *label_5;
    QIcon icon;

    Ui_Dialog(){
        setupUi(this);
        retranslateUi(this);
        exec();
    }
    void setupUi(QDialog *Dialog)
    {
        if (Dialog->objectName().isEmpty())
            Dialog->setObjectName(QString::fromUtf8(_("Multitouch test")));
        Dialog->resize(400, 300);
        setWindowIcon(QIcon(":/com.ublinux.ubl-test-multitouch.svg"));
        verticalLayout_7 = new QVBoxLayout(Dialog);
        verticalLayout_7->setObjectName(QString::fromUtf8("verticalLayout_7"));
        verticalLayout = new QVBoxLayout();
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        QLabel *labl = new QLabel(Dialog);
        QPixmap *pixmap = new QPixmap();
        pixmap->load(":/com.ublinux.ubl-test-multitouch.png","png");
        labl->setPixmap(*pixmap);
        labl->setAlignment(Qt::AlignHCenter);
        verticalLayout->addWidget(labl);

        verticalLayout_7->addLayout(verticalLayout);

        verticalLayout_2 = new QVBoxLayout();
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        label = new QLabel(Dialog);
        label->setObjectName(QString::fromUtf8("label"));
        label->setAlignment(Qt::AlignCenter);

        verticalLayout_2->addWidget(label);


        verticalLayout_7->addLayout(verticalLayout_2);

        verticalLayout_3 = new QVBoxLayout();
        verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
        label_2 = new QLabel(Dialog);
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_2->setAlignment(Qt::AlignCenter);

        verticalLayout_3->addWidget(label_2);


        verticalLayout_7->addLayout(verticalLayout_3);

        verticalLayout_4 = new QVBoxLayout();
        verticalLayout_4->setObjectName(QString::fromUtf8("verticalLayout_4"));
        label_3 = new QLabel(Dialog);
        label_3->setObjectName(QString::fromUtf8("label_3"));
        label_3->setAlignment(Qt::AlignCenter);

        verticalLayout_4->addWidget(label_3);


        verticalLayout_7->addLayout(verticalLayout_4);

        verticalLayout_5 = new QVBoxLayout();
        verticalLayout_5->setObjectName(QString::fromUtf8("verticalLayout_5"));

        verticalLayout_7->addLayout(verticalLayout_5);

        verticalLayout_6 = new QVBoxLayout();
        verticalLayout_6->setObjectName(QString::fromUtf8("verticalLayout_6"));
        label_5 = new QLabel(Dialog);
        label_5->setObjectName(QString::fromUtf8("label_5"));
        label_5->setAlignment(Qt::AlignCenter);

        verticalLayout_6->addWidget(label_5);


        verticalLayout_7->addLayout(verticalLayout_6);


        retranslateUi(Dialog);

        QMetaObject::connectSlotsByName(Dialog);
    } // setupUi

    void retranslateUi(QDialog *Dialog)
    {
        Dialog->setWindowTitle(_("Multitouch test"));
        label->setText("ubl-test-multitouch");
        label->setStyleSheet("font-weight:500;");
        label_2->setText("1.2");
        label_3->setText(QString(_("Multitouch test")));
        // label_4->setText(str);
        label_4->setOpenExternalLinks(true);
        label_5->setText("Copyright \302\251 UBSoft LCC, 2022 - 2023");
    } // retranslateUi

};


static const int RESIZE_DIMENSION   = 500;
//! [0]
MainWindow::MainWindow()
{
    setlocale(LC_ALL,"");
    QTranslator qtTranslator;
    qtTranslator.load("qtbase_de");
    QApplication::installTranslator(&qtTranslator);
    scribbleArea = new ScribbleArea;
    setCentralWidget(scribbleArea);
    createActions();
    createMenus();

    setWindowTitle(tr(_("Multitouch test")));
    setWindowIcon(QIcon(":/com.ublinux.ubl-test-multitouch.svg"));
    resize(RESIZE_DIMENSION, RESIZE_DIMENSION);
}
//! [0]

//! [1]
void MainWindow::closeEvent(QCloseEvent *event)
//! [1] //! [2]
{
    if (maybeSave()) {
        event->accept();
    } else {
        event->ignore();
    }
}
//! [2]

//! [3]
void MainWindow::open()
//! [3] //! [4]
{
    if (maybeSave()) {
        QString fileName = QFileDialog::getOpenFileName(this,
                                   tr(_("Open File")), QDir::currentPath());
        if (!fileName.isEmpty())
            scribbleArea->openImage(fileName);
    }
}
//! [4]

//! [5]
void MainWindow::save()
//! [5] //! [6]
{
    auto *action = qobject_cast<QAction *>(sender());
    QByteArray fileFormat = action->data().toByteArray();
    saveFile(fileFormat);
}
//! [6]

//! [11]
void MainWindow::about()
//! [11] //! [12]
{
    Ui_Dialog dialog;
}

//! [12]

//! [13]
void MainWindow::createActions()
//! [13] //! [14]
{
    openAct = new QAction(tr(_("Open...")), this);
    openAct->setShortcut(tr("Ctrl+O"));
    connect(openAct, &QAction::triggered, this, &MainWindow::open);

    const QList<QByteArray> imageFormats = QImageWriter::supportedImageFormats();
    for (const QByteArray &format : imageFormats) {
        QString text = tr("%1...").arg(QString(format).toUpper());

        auto *action = new QAction(text, this);
        action->setData(format);
        connect(action, &QAction::triggered, this, &MainWindow::save);
        saveAsActs.append(action);
    }

    printAct = new QAction(tr(_("Print...")), this);
    connect(printAct, &QAction::triggered, scribbleArea, &ScribbleArea::print);

    exitAct = new QAction(tr(_("Exit")), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    connect(exitAct, &QAction::triggered, this, &QWidget::close);

    clearScreenAct = new QAction((_("Clean Screen")), this);
    clearScreenAct->setShortcut(tr("Ctrl+L"));
    connect(clearScreenAct, &QAction::triggered,
            scribbleArea, &ScribbleArea::clearImage);

    aboutAct = new QAction(tr(_("About")), this);
    connect(aboutAct, &QAction::triggered, this, &MainWindow::about);

    connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
}

//! [14]

//! [15]
void MainWindow::createMenus()
//! [15] //! [16]
{
    saveAsMenu = new QMenu(tr(_("Save as")), this);
    saveAsMenu->addActions(saveAsActs);

    fileMenu = new QMenu(tr(_("File")), this);
    fileMenu->addAction(openAct);
    fileMenu->addMenu(saveAsMenu);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    optionMenu = new QMenu(tr(_("Options")), this);
    optionMenu->addAction(clearScreenAct);

    addColorSubmenu();
    addPatternSubmenu();

    helpMenu = new QMenu(tr(_("Help")), this);
    helpMenu->addAction(aboutAct);
    helpMenu->addAction(aboutQtAct);

    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(optionMenu);
    menuBar()->addMenu(helpMenu);
}

void MainWindow::addColorSubmenu()
{
    QMenu *             colorSubMenu    = optionMenu->addMenu(_("Colors"));
    ColorDataModel &    model           = ColorDataModel::getInstance();
    QVector<QString>    colors          = model.getSupportedColors();

    QAction * colorAction = nullptr;

    foreach (QString color, colors)
    {
        colorAction = model.getColorAction(color);
        colorSubMenu->addAction(colorAction);
    }

    colorAction = model.getColorAction("Mult-color");
    colorSubMenu->addAction(colorAction);
}

void MainWindow::addPatternSubmenu()
{
    QMenu *             patternSubMenu  = optionMenu->addMenu(_("Pattern"));
    ColorDataModel &    model           = ColorDataModel::getInstance();
    QVector<QString>    patterns        = model.getSupportedPatterns();

    QAction *           patternAction   = nullptr;

    foreach (QString patterStr, patterns)
    {
        patternAction = model.getPatternAction(patterStr);
        patternSubMenu->addAction(patternAction);
    }

}
//! [16]

//! [17]
bool MainWindow::maybeSave()
//! [17] //! [18]
{
    bool success = true;

    if (scribbleArea->isModified()) {
       QMessageBox::StandardButton ret;
       ret = QMessageBox::warning(this, tr(_("Multitouch test")),
                          tr(_("Picture were changed.\n"
                             "Would you like to save changes?")),
                          QMessageBox::Save | QMessageBox::Discard
                          | QMessageBox::Cancel);
        if (ret == QMessageBox::Save)
        {
            success =  saveFile("png");
        }

        else if (ret == QMessageBox::Cancel)
        {
            success = false;
        }
    }

    return success;
}
//! [18]

//! [19]
bool MainWindow::saveFile(const QByteArray &fileFormat)
//! [19] //! [20]
{
    bool success = true;

    QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;

    QString fileName = QFileDialog::getSaveFileName(this, tr(_("Save As")),
                               initialPath,
                               tr("%1 Files (*.%2);;All Files (*)")
                               .arg(QString::fromLatin1(fileFormat.toUpper()))
                               .arg(QString::fromLatin1(fileFormat)));
    if (fileName.isEmpty()) {
        success = false;
    } else {
        success = scribbleArea->saveImage(fileName, fileFormat.constData());
    }

    return success;
}
//! [20]
