/*
 * Peony-Qt's Library
 *
 * Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: Wenjie Xiang <xiangwenjie@kylinos.cn>
 */

#include <gtest/gtest.h>
#include <qtest.h>
#include <QPluginLoader>
#include <QDebug>
#include "stubext.h"
#include "menu-plugin-iface.h"
#include "file-info-job.h"
#include "file-info.h"
#include "directory-view-plugin-iface2.h"
#include "properties-window-tab-page-plugin-iface.h"

using namespace testing;

class extensionsTest : public testing::Test
{
public:
    QPluginLoader loader;
    QObject *plugin = nullptr;
    Peony::MenuPluginInterface *iface = nullptr;
    Peony::DirectoryViewPluginIface2 *viewIface = nullptr;
    Peony::PropertiesWindowTabPagePluginIface *tabPageIface = nullptr;

private:
    //打桩声明
    stub_ext::StubExt stub;

};

TEST_F(extensionsTest, bluetoothPlugin)
{
    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-bluetooth-plugin/libpeony-bluetooth-plugin.so");
    loader.setFileName(path);
    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
    plugin = loader.instance();
    iface = dynamic_cast<Peony::MenuPluginInterface *>(plugin);
    ASSERT_NE(iface, nullptr);
    QString directory = "file:///data/usershare";
    QStringList selectUris = (QStringList() << (directory + "/test.txt"));
    auto job = new Peony::FileInfoJob(Peony::FileInfo::fromUri(selectUris.first()));
    job->querySync();
    QList<QAction *> actions = iface->menuActions(Peony::MenuPluginInterface::DirectoryView, directory, selectUris);
    ASSERT_GT(actions.size(), 0);
    Peony::PluginInterface::PluginType type = iface->pluginType();
    ASSERT_EQ(type, Peony::PluginInterface::MenuPlugin);
    QString name = iface->name();
    ASSERT_GT(name.size(), 0);
    QString description = iface->description();
    ASSERT_GT(description.size(), 0);
    QIcon icon = iface->icon();
    ASSERT_GT(icon.name().size(), 0);
    iface->setEnable(true);
    EXPECT_TRUE(iface->isEnable());
    ASSERT_GT(iface->testPlugin().size(), 0);
    actions.first()->trigger();
}

TEST_F(extensionsTest, terminalPlugin)
{
    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-menu-plugin-mate-terminal/libpeony-menu-plugin-mate-terminal.so");
    loader.setFileName(path);
    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
    plugin = loader.instance();
    iface = dynamic_cast<Peony::MenuPluginInterface *>(plugin);
    ASSERT_NE(iface, nullptr);
    QString directory = "file:///data/usershare";
    QStringList selectUris = (QStringList() << (directory));
    auto job = new Peony::FileInfoJob(Peony::FileInfo::fromUri(selectUris.first()));
    job->querySync();
    QList<QAction *> actions = iface->menuActions(Peony::MenuPluginInterface::DirectoryView, directory, selectUris);
    ASSERT_GT(actions.size(), 0);
    Peony::PluginInterface::PluginType type = iface->pluginType();
    ASSERT_EQ(type, Peony::PluginInterface::MenuPlugin);
    QString name = iface->name();
    ASSERT_GT(name.size(), 0);
    QString description = iface->description();
    ASSERT_GT(description.size(), 0);
    QIcon icon = iface->icon();
    ASSERT_GT(icon.name().size(), 0);
    iface->setEnable(true);
    EXPECT_TRUE(iface->isEnable());
    ASSERT_GT(iface->testPlugin().size(), 0);
    actions.first()->trigger();
}

TEST_F(extensionsTest, driveRenamePlugin)
{
    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-drive-rename/libpeony-drive-rename.so");
    loader.setFileName(path);
    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
    plugin = loader.instance();
    iface = dynamic_cast<Peony::MenuPluginInterface *>(plugin);
    ASSERT_NE(iface, nullptr);
    QString directory = "computer:///SKHynix_HFS512GD9TNI-L2B0B-3.drive";
    QStringList selectUris = (QStringList() << (directory));
    QList<QAction *> actions = iface->menuActions(Peony::MenuPluginInterface::SideBar, directory, selectUris);
    ASSERT_GT(actions.size(), 0);
    Peony::PluginInterface::PluginType type = iface->pluginType();
    ASSERT_EQ(type, Peony::PluginInterface::MenuPlugin);
    QString name = iface->name();
    ASSERT_GT(name.size(), 0);
    QString description = iface->description();
    ASSERT_GT(description.size(), 0);
    QIcon icon = iface->icon();
    ASSERT_GT(icon.name().size(), 0);
    iface->setEnable(true);
    EXPECT_TRUE(iface->isEnable());
    ASSERT_EQ(iface->testPlugin().size(), 0);
    actions.first()->trigger();
}

//TEST_F(extensionsTest, computerViewPlugin)
//{
//    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-extension-computer-view/libpeony-computer-view-plugin.so");
//    loader.setFileName(path);
//    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
//    plugin = loader.instance();
//    viewIface = dynamic_cast<Peony::DirectoryViewPluginIface2 *>(plugin);
//    ASSERT_NE(viewIface, nullptr);
//    Peony::PluginInterface::PluginType type = viewIface->pluginType();
//    ASSERT_EQ(type, Peony::PluginInterface::DirectoryViewPlugin2);
//    QString name = viewIface->name();
//    ASSERT_GT(name.size(), 0);
//    QString description = viewIface->description();
//    ASSERT_GT(description.size(), 0);
//    QIcon icon = viewIface->icon();
//    ASSERT_GT(icon.name().size(), 0);
//    viewIface->setEnable(true);
//    EXPECT_TRUE(viewIface->isEnable());
//    ASSERT_GT(viewIface->viewIdentity().size(), 0);
//    ASSERT_NE(viewIface->viewName(), nullptr);
//    EXPECT_FALSE(viewIface->viewIcon().isNull());
//    EXPECT_TRUE(viewIface->supportUri("computer:///"));
//    ASSERT_EQ(viewIface->zoom_level_hint(), -1);
//    ASSERT_EQ(viewIface->minimumSupportedZoomLevel(), -1);
//    ASSERT_EQ(viewIface->maximumSupportedZoomLevel(), -1);
//    EXPECT_TRUE(viewIface->priority("computer:///"));
//    EXPECT_FALSE(viewIface->supportZoom());
//    ASSERT_NE(viewIface->create(), nullptr);
//}

TEST_F(extensionsTest, engrampaPlugin)
{
    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-engrampa-menu-plugin/libpeony-engrampa-menu-plugin.so");
    loader.setFileName(path);
    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
    plugin = loader.instance();
    iface = dynamic_cast<Peony::MenuPluginInterface *>(plugin);
    ASSERT_NE(iface, nullptr);
    QString directory = "file:///data/usershare";
    QStringList selectUris = (QStringList() << (directory + "/test.txt"));
    auto job = new Peony::FileInfoJob(Peony::FileInfo::fromUri(selectUris.first()));
    job->querySync();
    QList<QAction *> actions = iface->menuActions(Peony::MenuPluginInterface::DirectoryView, directory, selectUris);
    ASSERT_GT(actions.size(), 0);
    Peony::PluginInterface::PluginType type = iface->pluginType();
    ASSERT_EQ(type, Peony::PluginInterface::MenuPlugin);
    QString name = iface->name();
    ASSERT_GT(name.size(), 0);
    QString description = iface->description();
    ASSERT_GT(description.size(), 0);
    QIcon icon = iface->icon();
    ASSERT_GT(icon.name().size(), 0);
    iface->setEnable(true);
    EXPECT_TRUE(iface->isEnable());
    ASSERT_GT(iface->testPlugin().size(), 0);
    actions.first()->trigger();
}

TEST_F(extensionsTest, sendToDevicePlugin)
{
    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-send-to-device/libpeony-send-to-device.so");
    loader.setFileName(path);
    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
    plugin = loader.instance();
    iface = dynamic_cast<Peony::MenuPluginInterface *>(plugin);
    ASSERT_NE(iface, nullptr);
    QString directory = "file:///data/usershare";
    QStringList selectUris = (QStringList() << (directory + "/test.txt"));
    auto job = new Peony::FileInfoJob(Peony::FileInfo::fromUri(selectUris.first()));
    job->querySync();
    QList<QAction *> actions = iface->menuActions(Peony::MenuPluginInterface::DirectoryView, directory, selectUris);
    ASSERT_GT(actions.size(), 0);
    Peony::PluginInterface::PluginType type = iface->pluginType();
    ASSERT_EQ(type, Peony::PluginInterface::MenuPlugin);
    QString name = iface->name();
    ASSERT_GT(name.size(), 0);
    QString description = iface->description();
    ASSERT_GT(description.size(), 0);
    QIcon icon = iface->icon();
    ASSERT_GT(icon.name().size(), 0);
    iface->setEnable(true);
    EXPECT_TRUE(iface->isEnable());
    ASSERT_EQ(iface->testPlugin().size(), 0);
    actions.first()->trigger();
}

TEST_F(extensionsTest, setWallpaperPlugin)
{
    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-set-wallpaper/libpeony-set-wallpaper.so");
    loader.setFileName(path);
    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
    plugin = loader.instance();
    iface = dynamic_cast<Peony::MenuPluginInterface *>(plugin);
    ASSERT_NE(iface, nullptr);
    QString directory = "file:///data/usershare";
    QStringList selectUris = (QStringList() << (directory + "/test.png"));
    auto job = new Peony::FileInfoJob(Peony::FileInfo::fromUri(selectUris.first()));
    job->querySync();
    QList<QAction *> actions = iface->menuActions(Peony::MenuPluginInterface::DirectoryView, directory, selectUris);
    ASSERT_GT(actions.size(), 0);
    Peony::PluginInterface::PluginType type = iface->pluginType();
    ASSERT_EQ(type, Peony::PluginInterface::MenuPlugin);
    QString name = iface->name();
    ASSERT_GT(name.size(), 0);
    QString description = iface->description();
    ASSERT_GT(description.size(), 0);
    QIcon icon = iface->icon();
    ASSERT_GT(icon.name().size(), 0);
    iface->setEnable(true);
    EXPECT_TRUE(iface->isEnable());
    ASSERT_GT(iface->testPlugin().size(), 0);
    actions.first()->trigger();
}

//TEST_F(extensionsTest, sharePlugin)
//{
//    QString path = PLUGIN_INSTALL_DIRS + QString("/peony-share/app/libpeony-share.so");
//    loader.setFileName(path);
//    ASSERT_EQ(loader.metaData().value("MetaData").toObject().value("version").toString(), VERSION);
//    plugin = loader.instance();
//    tabPageIface = dynamic_cast<Peony::PropertiesWindowTabPagePluginIface *>(plugin);
//    ASSERT_NE(tabPageIface, nullptr);
//    QString directory = "file:///data/usershare";
//    QStringList selectUris = (QStringList() << (directory + "/test"));
//    auto job = new Peony::FileInfoJob(Peony::FileInfo::fromUri(selectUris.first()));
//    job->querySync();
//    Peony::PluginInterface::PluginType type = tabPageIface->pluginType();
//    ASSERT_EQ(type, Peony::PluginInterface::PropertiesWindowPlugin);
//    QString name = tabPageIface->name();
//    ASSERT_GT(name.size(), 0);
//    QString description = tabPageIface->description();
//    ASSERT_GT(description.size(), 0);
//    QIcon icon = tabPageIface->icon();
//    ASSERT_GT(icon.name().size(), 0);
//    tabPageIface->setEnable(true);
//    EXPECT_TRUE(tabPageIface->isEnable());
//    ASSERT_EQ(tabPageIface->tabOrder(), 99);
//    EXPECT_TRUE(tabPageIface->supportUris(selectUris));
//    ASSERT_NE(tabPageIface->createTabPage(selectUris), nullptr);
//}
