/*
 * 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 "devicemanager.h"
#include "devicehelper.h"
#include "devicemonitor.h"
#include "configmanager.h"

#include <QDir>
#include <QDBusMessage>
#include <QDBusConnection>

static const QString TOUCHSCREEN_CFG_PATH = "/.config/touchcfg.ini";

DeviceManager* DeviceManager::getInstance()
{
    static DeviceManager s_manager;
    return &s_manager;
}

DeviceManager::DeviceManager(QObject* parent)
    : QObject(parent)
    , m_deviceMonitor(DeviceMonitor::getInstance())
    , m_configManager(new ConfigManager(QDir::homePath() + TOUCHSCREEN_CFG_PATH))
{
    initDeviceManager();
}

void DeviceManager::initDeviceManager()
{
    connect(m_deviceMonitor, &DeviceMonitor::deviceAdded, this, &DeviceManager::deviceAdded);
    connect(m_deviceMonitor, &DeviceMonitor::deviceRemoved, this, &DeviceManager::deviceRemoved);
    connect(m_deviceMonitor, &DeviceMonitor::deviceEnabled, this, &DeviceManager::deviceEnabled);
    connect(m_deviceMonitor, &DeviceMonitor::deviceDisabled, this, &DeviceManager::deviceDisabled);
}

QList<Device> DeviceManager::getTouchDevices()
{
    return DeviceHelper::getTouchDevices();
}

QList<Device> DeviceManager::getOutputDevices()
{
    return DeviceHelper::getOutputs();
}

QString DeviceManager::getInputName(int id)
{
    return DeviceHelper::getInputName(id);
}

QString DeviceManager::getInputProductId(int id)
{
    return DeviceHelper::getInputProductId(id);
}

QString DeviceManager::getInputSerial(int id)
{
    return DeviceHelper::getInputSerial(id);
}

bool DeviceManager::isPrimaryOutput(int id)
{
    return DeviceHelper::isPrimaryOutput(id);
}

void DeviceManager::mapToOutput(int inputDeviceId, const QString &outputName)
{
    DeviceHelper::mapToOutput(inputDeviceId, outputName);

    TouchDeviceConfigInfo configInfo;
    configInfo.deviceName = getInputName(inputDeviceId);
    configInfo.productId = getInputProductId(inputDeviceId);
    configInfo.serial = getInputSerial(inputDeviceId);

    m_configManager->saveConfigInfo(outputName, configInfo);
}

void DeviceManager::calibrateTouch(int touchDeviceId, QString outputName)
{
    QString id = QString::number(touchDeviceId);
    QDBusMessage msg = QDBusMessage::createSignal("/com/control/center/calibrator", "com.control.center.calibrator.interface", "calibratorEvent");
    msg.setArguments(QList<QVariant>{ id + "," + outputName});
    QDBusConnection::systemBus().send(msg);
}
