#include "qwidgetpluginmanager.h"
#include <QQuickWindow>
#include <QPluginLoader>
#include <QPushButton>
#include <QWidget>
#include <QPainter>
#include <QGuiApplication>
#include <QScreen>
#include <QDebug>
#include <QGSettings>

QWidgetPluginContainer::QWidgetPluginContainer(QWidget *parent)
    : QWidget(parent)
{
    if (QGSettings::isSchemaInstalled("org.mate.background")) {
        m_backgroundGSettings = new QGSettings("org.mate.background");
        setBackground(m_backgroundGSettings->get("pictureFilename").toString());
        connect(m_backgroundGSettings, &QGSettings::changed, [this](const QString &key) {
            if (key == "pictureFilename") {
                setBackground(m_backgroundGSettings->get("pictureFilename").toString());
            }
        });
    }
    setWindowFlag(Qt::FramelessWindowHint);
}

void QWidgetPluginContainer::setPlugin(QWidget *plugin)
{
    m_plugin = plugin;
    if (!m_plugin) {
        return;
    }
    m_plugin->setWindowFlag(Qt::FramelessWindowHint);
    m_plugin->setAttribute(Qt::WA_TranslucentBackground);
    m_plugin->setParent(this);
}

QWidget *QWidgetPluginContainer::plugin() const
{
    return m_plugin;
}

void QWidgetPluginContainer::updateBackgroundPosition()
{
    m_containerBackgroundImg = m_desktopBackgroundImg.copy(this->geometry());
    update();
}

void QWidgetPluginContainer::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    m_containerBackgroundImg = m_desktopBackgroundImg.copy(this->geometry());
    p.drawImage(this->rect(), m_containerBackgroundImg);
    return QWidget::paintEvent(event);
}

void QWidgetPluginContainer::setBackground(const QString &background)
{
    auto rect = QGuiApplication::primaryScreen()->geometry();
    m_desktopBackgroundImg = QImage(background).scaled(rect.width(), rect.height());
    m_containerBackgroundImg = m_desktopBackgroundImg.copy(this->geometry());
    update();
}

QWidgetPluginManager::QWidgetPluginManager(QObject *parent)
    : QObject{parent}
{

}

void QWidgetPluginManager::createPlugin(const QString &pluginName, int width, int height)
{
    if (m_plugins.contains(pluginName)) {
        return;
    }

    QPluginLoader loader("/opt/small-plugin/bin/libsmall-plugin-manage.so");
    QObject * plugin=loader.instance();
    if (plugin) {
        KySmallPluginInterface * app = qobject_cast<KySmallPluginInterface *>(plugin);
        QWidget *widget = app->createWidget(nullptr);
        widget->setFixedSize(width, height);

        QWidgetPluginContainer *pluginContainer = new QWidgetPluginContainer();
        pluginContainer->setFixedSize(width, height);
        pluginContainer->setPlugin(widget);
        m_plugins.insert(pluginName, pluginContainer);
    }
}

void QWidgetPluginManager::setPluginRootWindow(const QString &pluginName, QQuickWindow *rootWindow)
{
    m_rootWindow = rootWindow;
    m_plugins.value(pluginName)->winId();
    m_plugins.value(pluginName)->windowHandle()->setParent(rootWindow);
}

void QWidgetPluginManager::movePlugin(const QString &pluginName, int x, int y)
{
    if (!m_plugins.contains(pluginName)) {
        return;
    }
    m_plugins.value(pluginName)->move(x, y);
}

void QWidgetPluginManager::showPlugin(const QString &pluginName)
{
    if (!m_plugins.contains(pluginName)) {
        return;
    }
    m_plugins.value(pluginName)->show();
}

void QWidgetPluginManager::hidePlugin(const QString &pluginName)
{
    if (!m_plugins.contains(pluginName)) {
        return;
    }
    m_plugins.value(pluginName)->hide();
}

bool QWidgetPluginManager::containsPlugin(const QString &pluginName)
{
    return m_plugins.contains(pluginName);
}

QWidgetPluginContainer *QWidgetPluginManager::plugin(const QString &pluginName)
{
    if (m_plugins.contains(pluginName)) {
        return m_plugins.value(pluginName);
    } else {
        return nullptr;
    }
}


