/*
 * Copyright (C) 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/>.
 *
 * Authors: wangyan <wangyan@kylinos.cn>
 *
 */

#include "devicemonitor.h"

#include "inputdevicemonitor.h"

#include <KF5/KScreen/kscreen/output.h>
#include <KF5/KScreen/kscreen/config.h>
#include <KF5/KScreen/kscreen/configmonitor.h>
#include <KF5/KScreen/kscreen/getconfigoperation.h>

#include <QTimer>

DeviceMonitor* DeviceMonitor::getInstance()
{
    static DeviceMonitor s_monitor;
    return &s_monitor;
}

DeviceMonitor::DeviceMonitor(QObject *parent)
    : QObject(parent)
{
    initInputMonitor();
    initOutputMonitor();
}

void DeviceMonitor::initInputMonitor()
{
    InputDeviceMonitor* monitor = InputDeviceMonitor::create(this);

    connect(monitor, &InputDeviceMonitor::deviceAdded, this, [this](int deviceId){
        emit deviceAdded(DeviceType::InputDevice, deviceId);
    });
    connect(monitor, &InputDeviceMonitor::deviceRemoved, this, [this](int deviceId){
        emit deviceRemoved(DeviceType::InputDevice, deviceId);
    });
    connect(monitor, &InputDeviceMonitor::deviceEnabled, this, [this](int deviceId){
        emit deviceEnabled(DeviceType::InputDevice, deviceId);
    });
    connect(monitor, &InputDeviceMonitor::deviceDisabled, this, [this](int deviceId){
        emit deviceDisabled(DeviceType::InputDevice, deviceId);
    });
}

void DeviceMonitor::initOutputMonitor()
{
    KScreen::GetConfigOperation *op = new KScreen::GetConfigOperation;
    if (!op->exec()) {
        qWarning() << op->errorString();
    }

    m_config = op->config();
    KScreen::ConfigMonitor::instance()->addConfig(m_config);

    connect(m_config.data(), &KScreen::Config::outputAdded,
            this, [this](const KScreen::OutputPtr &output){
        QTimer::singleShot(100, this, [=]() {
            emit deviceAdded(DeviceType::OutputDevice, output.data()->id());
        });
    });
    connect(m_config.data(), &KScreen::Config::outputRemoved,
            this, [this](int id){
        emit deviceRemoved(DeviceType::OutputDevice, id);
    });

    for (const KScreen::OutputPtr &output : m_config->outputs()) {
        connect(output.data(), &KScreen::Output::isConnectedChanged, this, [output, this](){
            onOutputDeviceChanged(output);
        });
    }
}

void DeviceMonitor::onOutputDeviceChanged(const KScreen::OutputPtr &output)
{
    if (output.data()->isConnected()) {
        emit deviceAdded(DeviceType::OutputDevice, output.data()->id());
    } else {
        emit deviceRemoved(DeviceType::OutputDevice, output.data()->id());
    }

}
