/*
 * Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * 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
 * (at your option) 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 <https://www.gnu.org/licenses/>.
 *
 * Authors: iaom <zhangpengfei@kylinos.cn>
 *
 */

#include "search-button.h"

#include <QDBusInterface>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDebug>
#include <QDBusReply>
#include <QProcess>
#include <startup-management.h>

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

void SearchButton::openUkuiSearch(int x, int y, int width, int height)
{
    uint searchPid =  QDBusConnection::sessionBus().interface()->servicePid(QStringLiteral("com.ukui.search.service"));
    UkuiQuick::StartupManagement::self()->setStartupGeometry(searchPid, x, y, width, height);

    QDBusInterface interface(QStringLiteral("com.ukui.search.service"), QStringLiteral("/"), QStringLiteral("org.ukui.search.service"), QDBusConnection::sessionBus());
    interface.asyncCall(QStringLiteral("mainWindowSwitch"));
}

void SearchButton::openSearchSetting()
{
    bool res(false);
    QDBusInterface appLaunchInterface(QStringLiteral("com.kylin.ProcessManager"),
                                      QStringLiteral("/com/kylin/ProcessManager/AppLauncher"),
                                      QStringLiteral("com.kylin.ProcessManager.AppLauncher"),
                                      QDBusConnection::sessionBus());
    if(!appLaunchInterface.isValid()) {
        qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
        res = false;
    } else {
        appLaunchInterface.setTimeout(10000);
        QDBusReply<void> reply = appLaunchInterface.call("LaunchAppWithArguments", "/usr/share/applications/ukui-control-center.desktop", QStringList{"-m", "search"});
        if(reply.isValid()) {
            res = true;
        } else {
            qWarning() << "ProcessManager dbus called failed!" << reply.error();
            res = false;
        }
    }
    if (!res) {
        //打开控制面板对应页面
        QProcess::startDetached("ukui-control-center", {"-m", "search"});
    }
}
