/*
 * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
 *
 * Author:     sbw <sbw@sbw.so>
 *
 * Maintainer: sbw <sbw@sbw.so>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "dockpluginscontroller.h"
#include "proxyplugincontroller.h"
#include "pluginsiteminterface.h"
#include "traypluginitem.h"

#include <QDebug>
#include <QDir>
#include <QDrag>

DockPluginsController::DockPluginsController(QObject *parent)
    : AbstractPluginsController(parent)
{
    setObjectName("DockPlugin");

    ProxyPluginController::instance(PluginType::FixedSystemPlugin)->addProxyInterface(this);
}

DockPluginsController::~DockPluginsController()
{
    ProxyPluginController::instance(PluginType::FixedSystemPlugin)->removeProxyInterface(this);
}

void DockPluginsController::itemAdded(PluginsItemInterface *const itemInter, const QString &itemKey)
{
    QMap<PluginsItemInterface *, QMap<QString, QObject *>> &mPluginsMap = pluginsMap();

    // check if same item added
    if (mPluginsMap.contains(itemInter))
        if (mPluginsMap[itemInter].contains(itemKey))
            return;

    // 取 plugin api
    ProxyPluginController *proxyController = ProxyPluginController::instance(itemInter);
    if (!proxyController)
        return;

    QPluginLoader *pluginLoader = proxyController->pluginLoader(itemInter);
    if (!pluginLoader)
        return;

    const QJsonObject &meta = pluginLoader->metaData().value("MetaData").toObject();

    PluginsItem *item = nullptr;
    if (itemInter->pluginName() == "tray") {
        item = new TrayPluginItem(itemInter, itemKey, meta);
        if (item->graphicsEffect()) {
            item->graphicsEffect()->setEnabled(false);
        }
        connect(static_cast<TrayPluginItem *>(item), &TrayPluginItem::trayVisableCountChanged,
                this, &DockPluginsController::trayVisableCountChanged, Qt::UniqueConnection);
    } else {
        item = new PluginsItem(itemInter, itemKey, meta);
    }

    mPluginsMap[itemInter][itemKey] = item;

    emit pluginItemInserted(item);
}

void DockPluginsController::itemUpdate(PluginsItemInterface *const itemInter, const QString &itemKey)
{
    PluginsItem *item = getPluginItem(itemInter, itemKey);
    if (!item)
        return;

    item->update();

    emit pluginItemUpdated(item);
}

void DockPluginsController::itemRemoved(PluginsItemInterface *const itemInter, const QString &itemKey)
{
    PluginsItem *item = getPluginItem(itemInter, itemKey);
    if (!item)
        return;

    item->detachPluginWidget();

    emit pluginItemRemoved(item);

    QMap<PluginsItemInterface *, QMap<QString, QObject *>> &mPluginsMap = pluginsMap();
    mPluginsMap[itemInter].remove(itemKey);

    // do not delete the itemWidget object(specified in the plugin interface)
    item->centralWidget()->setParent(nullptr);

    if (item->isDragging()) {
        QDrag::cancel();
    }

    // just delete our wrapper object(PluginsItem)
    item->deleteLater();
}

void DockPluginsController::requestWindowAutoHide(PluginsItemInterface *const itemInter, const QString &itemKey, const bool autoHide)
{
    PluginsItem *item = getPluginItem(itemInter, itemKey);
    if (!item)
        return;

    Q_EMIT item->requestWindowAutoHide(autoHide);
}

void DockPluginsController::requestRefreshWindowVisible(PluginsItemInterface *const itemInter, const QString &itemKey)
{
    PluginsItem *item = getPluginItem(itemInter, itemKey);
    if (!item)
        return;

    Q_EMIT item->requestRefreshWindowVisible();
}

void DockPluginsController::requestSetAppletVisible(PluginsItemInterface *const itemInter, const QString &itemKey, const bool visible)
{
    PluginsItem *item = getPluginItem(itemInter, itemKey);
    if (!item)
        return;

    if (visible) {
        item->showPopupApplet(itemInter->itemPopupApplet(itemKey));
    } else {
        item->hidePopup();
    }
}

QMap<PluginsItemInterface *, QMap<QString, QObject *>> &DockPluginsController::pluginsMap()
{
    return ProxyPluginController::instance(PluginType::FixedSystemPlugin)->pluginsMap();
}

PluginsItem *DockPluginsController::getPluginItem(PluginsItemInterface * const itemInter, const QString &itemKey) const
{
    ProxyPluginController *proxyController = ProxyPluginController::instance(itemInter);
    if (!proxyController)
        return nullptr;

    const QMap<PluginsItemInterface *, QMap<QString, QObject *>> &plugins = proxyController->pluginsMap();
    if (plugins.contains(itemInter) && plugins[itemInter].contains(itemKey))
        return static_cast<PluginsItem *>(plugins[itemInter][itemKey]);

    return nullptr;
}
