#include "customdialog.h"

#include <QProcess>
#include <QPainter>
#include <QPainterPath>
#include <QDebug>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusConnection>
#include <QGSettings>

#define KWIN_SERVICE   "org.ukui.KWin"
#define KWIN_PATH      "/Compositor"
#define KWIN_INTERFACE "org.ukui.kwin.Compositing"

extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);

CustomDialog::CustomDialog(QWidget *parent)
    :QDialog(parent)
    ,m_isCompRun(true)
{
    m_isCompRun = checkCompositorRunning();
    if(m_isCompRun)
        setAttribute(Qt::WA_TranslucentBackground, true);
}

CustomDialog::~CustomDialog()
{

}

void CustomDialog::paintEvent(QPaintEvent *event)
{
    if(!m_isCompRun)
        return QDialog::paintEvent(event);

    Q_UNUSED(event);
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    QPainterPath rectPath;
    rectPath.addRoundedRect(this->rect().adjusted(10, 10, -10, -10), 6, 6);

    // 画一个黑底
    QPixmap pixmap(this->rect().size());
    pixmap.fill(Qt::transparent);
    QPainter pixmapPainter(&pixmap);
    pixmapPainter.setRenderHint(QPainter::Antialiasing);
    pixmapPainter.setPen(Qt::transparent);
    pixmapPainter.setBrush(Qt::black);
    pixmapPainter.setOpacity(0.65);
    pixmapPainter.drawPath(rectPath);
    pixmapPainter.end();

    // 模糊这个黑底
    QImage img = pixmap.toImage();
    qt_blurImage(img, 10, false, false);
    // 挖掉中心
    pixmap = QPixmap::fromImage(img);
    QPainter pixmapPainter2(&pixmap);
    pixmapPainter2.setRenderHint(QPainter::Antialiasing);
    pixmapPainter2.setCompositionMode(QPainter::CompositionMode_Clear);
    pixmapPainter2.setPen(Qt::transparent);
    pixmapPainter2.setBrush(Qt::transparent);
    pixmapPainter2.drawPath(rectPath);

    // 绘制阴影
    p.drawPixmap(this->rect(), pixmap, pixmap.rect());
    // 绘制一个背景
    p.save();
    p.fillPath(rectPath,palette().color(QPalette::Base));

    p.restore();
}

bool CustomDialog::checkCompositorRunning()
{
    QDBusInterface dbus_iface (KWIN_SERVICE, KWIN_PATH, KWIN_INTERFACE, QDBusConnection::sessionBus());
    QVariant reply = dbus_iface.property ("active");
    bool isfoundCompositingManger = false;
    if (reply.toBool()) {
        isfoundCompositingManger = true;
    }

    if(!isfoundCompositingManger)
    {
        if(QGSettings::isSchemaInstalled("org.gnome.metacity"))
        {
            QProcess process;
            process.start("sh -c \"ps -e |grep metacity\"");
            if(process.waitForStarted(100)&&process.waitForFinished(100) && process.readAllStandardOutput().contains("metacity"))
            {
                QGSettings metacityGSettings("org.gnome.metacity", "/org/gnome/metacity/");
                isfoundCompositingManger = metacityGSettings.get("compositing-manager").toBool();
            }
        }
    }

    if(!isfoundCompositingManger)
    {
        if(QGSettings::isSchemaInstalled("org.mate.Marco.general"))
        {
            QProcess process;
            process.start("sh -c \"ps -e |grep marco\"");
            if(process.waitForStarted(100)&&process.waitForFinished(100) && process.readAllStandardOutput().contains("marco"))
            {
                QGSettings marcoGSettings("org.mate.Marco.general", "/org/mate/marco/general/");
                isfoundCompositingManger = marcoGSettings.get("compositing-manager").toBool();
            }
        }
    }

    return isfoundCompositingManger;
}
