#include "kybaboutdialog.h"
#include "ui_kybaboutdialog.h"
#include "src/frame/xatom-helper.h"
#include "src/po/translation.h"
#include "src/model/logicmain.h"
#include "src/model/menufactory.h"

#include <QScreen>
#include <QStyle>
#include <QDesktopServices>
#include <QScrollBar>
#include <ukuistylehelper/ukuistylehelper.h>

KYBAboutDialog *KYBAboutDialog::m_oInstance = nullptr;
QMutex          KYBAboutDialog::m_oMutex;

#define VERSION "3.2.0"
#define SUPPORT "<u>support@kylinos.cn</u>"

#include <QDebug>

KYBAboutDialog::KYBAboutDialog(QWidget *parent) :
    kdk::KAboutDialog(parent)
  //  ui(new Ui::KYBAboutDialog)
{
  //  ui->setupUi(this);
    kdk::UkuiStyleHelper::self()->removeHeader(this);
      setAppIcon(QIcon::fromTheme("burner"));
      setAppName(_("KylinBurner"));
      setAppVersion(_("Version:") + VERSION);
       setAppSupport(_("Service & Support: %1").arg(SUPPORT));
       closeButton()->setToolTip(_("Close"));
       this->setModal(true);
       //this->show();
//#if 1
//    MotifWmHints hints;
//    hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
//    hints.functions = MWM_FUNC_ALL;
//    hints.decorations = MWM_DECOR_BORDER;
//    XAtomHelper::getInstance()->setWindowMotifHint(winId(), hints);
//#endif
//    //setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
//    QFont f = ui->labelTitle->font();
//    setAttribute(Qt::WA_ShowModal, true);
//    f.setPixelSize(14);
//    ui->labelTitle->setText(_("KylinBurner"));
//    f.setWeight(28);
//    f.setPixelSize(18);
//    ui->labelName->setFont(f);
//    ui->labelName->setText(_("KylinBurner"));
//    f.setWeight(24);
//    f.setPixelSize(14);
//    ui->labelVersion->setFont(f);
//    ui->labelVersion->setText(_("Version: %1").arg(VERSION));
//    f.setPixelSize(12);
//    f.setPixelSize(14);
//    setWindowModality(Qt::WindowModal);
//    setWindowTitle(_("About"));
//    //图标库主题的刻录图片名换为burner
//    //ui->labelLogo->setPixmap(QIcon::fromTheme("disk-burner").pixmap(ui->labelLogo->size()));
//    //ui->labelIcon->setPixmap(QIcon::fromTheme("disk-burner").pixmap(96, 96));
//    ui->labelLogo->setPixmap(QIcon::fromTheme("burner").pixmap(ui->labelLogo->size()));
//    ui->labelIcon->setPixmap(QIcon::fromTheme("burner").pixmap(96, 96));

//    ui->labelTitle->setText(_("KylinBurner")/* + " - " + _("About")*/);
//    ui->btnClose->setIcon(QIcon::fromTheme("window-close-symbolic"));
//    ui->btnClose->setProperty("isWindowButton", 0x2);
//    ui->btnClose->setProperty("useIconHighlightEffect", 0x8);
//    ui->btnClose->setIconSize(QSize(16, 16));
//    ui->btnClose->installEventFilter(this);
//    //ui->btnClose->setFlat(true);
//    ui->btnClose->setFocusPolicy(Qt::NoFocus);

//    QScreen *screen = QGuiApplication::primaryScreen ();
//    QRect screenRect =  screen->availableGeometry();
//    m_oPosition.setX(screenRect.width() / 2);
//    m_oPosition.setY(screenRect.height() / 2);
//    connect(ui->btnClose, SIGNAL(clicked()), this, SLOT(hide()));
//    ui->labelSupport->setContextMenuPolicy(Qt::NoContextMenu);
//    connect(ui->labelSupport, &QLabel::linkActivated, [=](QString u){
//        QUrl url(u);
//        QDesktopServices::openUrl(url);
//    });
//#if 1
//    connect(MENU, &MenuFactory::about, [=](){
//        ABOUT->move(m_oPosition);
//        //qDebug() << m_oPosition;
//        ABOUT->show();
//    });
//#endif
}

KYBAboutDialog *KYBAboutDialog::instance(QWidget *parent)
{
    if (nullptr == m_oInstance)
    {
        QMutexLocker locker(&m_oMutex);
        if (nullptr == m_oInstance)
            m_oInstance = new KYBAboutDialog(parent);
    }
    return m_oInstance;
}

//void KYBAboutDialog::updatePosition(QPoint p)
//{
//    m_oPosition.setX(p.x() - width() / 2);
//    m_oPosition.setY(p.y() - height() / 2);
//    //move(m_oPosition);
//}

void KYBAboutDialog::paintEvent(QPaintEvent *event)
{
//    QPalette pal = QApplication::style()->standardPalette();

//    ui->labelLogo->setPixmap(QIcon::fromTheme("burner").pixmap(ui->labelLogo->size()));
//    ui->labelIcon->setPixmap(QIcon::fromTheme("burner").pixmap(96, 96));

//    if (MAIN->theme() != "ukui-black")
//    {
//        pal.setColor(QPalette::Background, QColor("#FFFFFF"));
//        ui->labelSupport->setText(_("Service & Support : ") +
//                                  "<a href=\"mailto://support@kylinos.cn\"" +
//                                  QString("style=\"color:%1\">").arg(pal.windowText().color().name(QColor::HexRgb))
//                                  + "support@kylinos.cn</a>");

//        ui->textEdit->setText("<body style=\"background:#FFFFFF;\">"+
//                              tr("<p style=\"color: %1\">").arg(pal.windowText().color().name(QColor::HexRgb))+
//                              _("Burner is an application developed by Kylinsoft, simple interface beautiful, "
//                                "interactive friendly daily burn software. It includes the creation of mirror "
//                                "image, data burning, data additional burning, mirror burning, image extraction "
//                                "and CD copy and other users daily required scenes, and additional support for "
//                                "the selection of physical optical drive, pop-up and other operations, "
//                                "to avoid the trouble of the user operation of physical entities.")
//                              + "</p></body>");
//        setPalette(pal);
//    }
//    else
//    {
//        setPalette(pal);
//        ui->labelSupport->setText(_("Service & Support : ") +
//                                  "<a href=\"mailto://support@kylinos.cn\"" +
//                                  QString("style=\"color:%1\">").arg(pal.windowText().color().name(QColor::HexRgb))
//                                  + "support@kylinos.cn</a>");
//        ui->textEdit->setText(QString("<body style=\"background:%1;\">")
//                              .arg(pal.background().color().name(QColor::HexRgb)) +
//                              tr("<p style=\"color: %1\">").arg(pal.windowText().color().name(QColor::HexRgb)) +
//                              _("Burner is an application developed by Kylinsoft, simple interface beautiful, "
//                                "interactive friendly daily burn software. It includes the creation of mirror "
//                                "image, data burning, data additional burning, mirror burning, image extraction "
//                                "and CD copy and other users daily required scenes, and additional support for "
//                                "the selection of physical optical drive, pop-up and other operations, "
//                                "to avoid the trouble of the user operation of physical entities.")
//                              + "</p></body>");

//    }


//    ui->textEdit->setDisabled(false);
//    QString si18n = ui->textEdit->toPlainText();
//    int w = ui->textEdit->fontMetrics().width(si18n, si18n.length());
//    int h = ui->textEdit->fontMetrics().height();
//    int row = w / ui->textEdit->width();
//    if (w % ui->textEdit->width()) row += 2; // for += 2 因为计算行数方法可能不是很精准，所以额外多加一行。
//    int he = (row * h);
//    if (he < 200)
//    {
//        ui->textEdit->setFixedHeight(he);
//        ui->textEdit->verticalScrollBar()->hide();
//        ui->textEdit->setDisabled(true);
//        m_iHeight = 336 + he;
//    }
//    else
//    {
//        ui->textEdit->setFixedHeight(200);
//        ui->textEdit->verticalScrollBar()->show();
//        ui->textEdit->setDisabled(false);
//        m_iHeight = 336 + 200;
//    }
//    setFixedSize(420, m_iHeight);

//    QFont f = ui->labelTitle->font();
//    f.setPixelSize(14);
//    ui->labelTitle->setFont(f);
//    f.setPixelSize(18);
//    ui->labelName->setFont(f);
//    f.setPixelSize(14);
//    ui->labelVersion->setFont(f);
//    f.setFamily(font().family());
//    ui->textEdit->setFont(f);
//    ui->labelSupport->setFont(f);
//    move(m_oPosition);
//    QDialog::paintEvent(event);
}

KYBAboutDialog::~KYBAboutDialog()
{
  //  delete ui;
}
