/*
 * Copyright 2023 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/>.
 */

#include "multimediacontroller.h"
#include "loghelper.h"
#include <QApplication>
#include <QDBusInterface>
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDBusReply>
#include <QDebug>

namespace
{

const char *mpris_dbus_path = "/org/mpris/MediaPlayer2";
const char *mpris_dbus_interface = "org.mpris.MediaPlayer2.Player";

}

void MultimediaController::pause(const std::string &serviceName)
{
    if (isPause(serviceName)) {
        return;
    }

    QDBusMessage message = QDBusMessage::createMethodCall(
        QString::fromStdString(serviceName), mpris_dbus_path, mpris_dbus_interface,
        QStringLiteral("PlayPause"));

    QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(message);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall);
    QObject::connect(watcher, &QDBusPendingCallWatcher::finished, qApp,
                     [watcher, serviceName](QDBusPendingCallWatcher *call) {
        QDBusPendingReply<> reply = *call;
        if (reply.isError()) {
            PRINT_NOTICE("Call mpris dbus %s error\n", serviceName.c_str());
        }
        call->deleteLater();
        watcher->deleteLater();
    });
}

bool MultimediaController::isPause(const std::string &serviceName)
{
    if (!QDBusConnection::sessionBus().isConnected()) {
        PRINT_NOTICE("Cannot connect to the D-Bus session bus.\n");
        return true;
    }

    QDBusInterface mprisDbus(
        QString::fromStdString(serviceName),
        mpris_dbus_path, mpris_dbus_interface,
        QDBusConnection::sessionBus());

    if (!mprisDbus.isValid()) {
        PRINT_NOTICE("mpris dbus service %s is not valid.\n", serviceName.c_str());
        return true;
    }

    QDBusReply<int> reply = mprisDbus.call("PlayState");
    return reply <= 0;
}
