#include "qgaboutdialog.h"

#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>

#include <QMessageBox>
#include <QSysInfo>
#include <QLabel>
#include <QDesktopServices>
#include <QUrl>

#include "qgrunsession.h"

class QgAboutDialog::Implementation
{
public:
    QVBoxLayout* ly;

    QLabel* lblCustom;

    QHBoxLayout* btnLy;

    QPushButton* btnQtAbout;
    QPushButton* btnSessionAbout;
    QPushButton* btnLicenseAbout;
    QPushButton* btnOpenSession;

    QString sessionMsg;
    QString licenseMsg;

    QString customMessage;
};


QgAboutDialog::QgAboutDialog(QWidget *parent):
    QgAboutDialog(QString(), parent)
{
}

QgAboutDialog::QgAboutDialog(const QString& aCustomMessage, QWidget* parent):
    QDialog(parent),
    p_(*new Implementation)
{
    p_.customMessage=aCustomMessage;

    p_.btnSessionAbout=new QPushButton("About Session...");
    p_.btnLicenseAbout=new QPushButton("About License...");
    p_.btnQtAbout=new QPushButton("About Qt...");
    p_.btnOpenSession=new QPushButton("Explore session...");

    p_.btnLy=new QHBoxLayout;
    p_.btnLy->addWidget(p_.btnSessionAbout);
    p_.btnLy->addWidget(p_.btnLicenseAbout);
    p_.btnLy->addWidget(p_.btnQtAbout);

    p_.lblCustom=new QLabel;
    p_.lblCustom->setText(p_.customMessage);

    p_.ly=new QVBoxLayout;    
    p_.ly->addWidget(p_.lblCustom);

    p_.ly->addLayout(p_.btnLy);

    this->setLayout(p_.ly);

    {
        QString hostInfo=
                QString("<p><b>Host:</b> %1 (%2) %3 %4</p>")
                    .arg(QSysInfo::machineHostName())
                    .arg(QSysInfo::currentCpuArchitecture())
                    .arg(QSysInfo::productType())
                    .arg(QSysInfo::productVersion());

        QgRunSession& s=QgRunSession::defaultSession();
        QString sessionInfo=
                QString("<p><b>Session:</b></p>"
                        "<p>Base path: <a href=\"file:///%1\">%1</a></p>"
                        "<p>Current Path: <a href=\"file:///%2\">%2</a></p>"
                        "<p>Output Path: %3</p>")
                    .arg(s.workDirPath())
                    .arg(s.currentDirPath())
                    .arg(s.outDirPath());

        p_.sessionMsg=QString("%1\n\%2").arg(sessionInfo).arg(hostInfo);

    }
    {        
        p_.licenseMsg=
                "<p>Copyright Leonardo Company S.p.A</p>"
                "<p>This application:</p>"
                "<ul>"
                    "<li>Is proprietaty software, copyright protected and not covered by any free linceces (as LGPL).</li>"
                    "<li><b>Is meant for internal use only and it is not deliverable or sell in any form</b></li>"
                    "<li>Use a dynamic linked Qt Open Source version. According with the LGPL, this is a <b>work that uses the library</b> and therefore it is not required compliance to the Qt LGPL lincence</li>"
                "</ul>"
                "<p>Third-part SW:</p>"
                "<ul>"
                    "<li>Qt Advanced Docking System v3.0.0, License LGPL-2.1-only (https://marketplace.qt.io/products/advanced-docking-system)</li>"
                "</ul>"
                "<p><b>At your risk</b>, without any warranty, this application can be execute with other version of Qt dynamic library, providing binary compatibiliy with the original Qt version (see 'About Qt...'</p>";

#if 0
                "<p>This applicaiton is proprietaty software, copyright protected and not covered by any free linceces (as LGPL).</p>"
                "<p>This applicaiton use a dynamic linked Qt Open Source version. According with the LGPL, this is a <b>work that uses the library</b> and therefore it is not required compliance to the Qt LGPL lincence</p>"
                "<p><b>At your risk</b>, without any warranty, this application can be execute with other version of Qt dynamic library, providing binary compatibiliy with the original Qt version (see 'About Qt...'</p>";
#endif
    }

    connect(p_.btnQtAbout, &QPushButton::clicked, this,
            [this]()
            {
                QString title="About Qt";
                QMessageBox::aboutQt(0, title);
            });

    connect(p_.btnSessionAbout, &QPushButton::clicked, this,
            [this]()
            {
                QString title="About Session";
                QMessageBox::about(0, title, p_.sessionMsg);
#if 0
                QMessageBox::StandardButton r=QMessageBox::question(0, "Open working directory?", p_.sessionMsg);
                if (r==QMessageBox::Yes)
                {
                    QDesktopServices::openUrl(QUrl(QString("file:///")+QgRunSession::defaultSession().currentDirPath()));
                }
#endif
            });

    connect(p_.btnLicenseAbout, &QPushButton::clicked, this,
            [this]()
            {
                QString title="About Licnese";
                QMessageBox::about(0, title, p_.licenseMsg);
            });

    connect(p_.btnOpenSession, &QPushButton::clicked, this,
            [this]()
            {
                QDesktopServices::openUrl(QUrl(QString("file:///")+QgRunSession::defaultSession().currentDirPath()));
            });

}


QgAboutDialog::~QgAboutDialog()
{
    delete &p_;
}
