/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2025 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "accessibilityinterface.h"
#include <QDebug>
#include <QProcess>
//#include <KSharedConfig>
//#include <KConfigGroup>
#include <QDBusReply>

#define STYLE_NAME_KEY "style-name"

AccessibilityInterface::AccessibilityInterface():UKUI::ServiceObject(nullptr)
{
    styleGsettings = UniversalInterface::self()->ukuiStyleGsettings();
    m_wlcomRenderDBus = UniversalInterface::self()->wlcomDbus();
    m_wlcomInterface = UniversalInterface::self()->wlcomEffectDbusInterface();
    m_kwinInterface = UniversalInterface::self()->kwinMagnifierInterface();
    m_kwinEffectInterface = UniversalInterface::self()->kwinEffectInterface();
    m_wlcomInterface->asyncCall("EnableEffect", "zoom", true);  // 打开后才有Magnifier路径
    initConnect();
}

AccessibilityInterface::~AccessibilityInterface()
{

}

QString AccessibilityInterface::getStyleName()
{
    if (styleGsettings->keys().contains("styleName")) {
        return styleGsettings->get(STYLE_NAME_KEY).toString();
    } else {
        qCritical() << "mStyleGsettings not contains the key: " << STYLE_NAME_KEY;
    }
    return QString();
}

void AccessibilityInterface::initConnect()
{
    connect(styleGsettings, &QGSettings::changed, this, [=](QString key) {
        if (key == STYLE_NAME_KEY) {
            Q_EMIT changed("styleName");
        }
    });
    QDBusConnection::sessionBus().connect("org.ukui.KWin",
                                          "/Magnifier",
                                          "org.ukui.Magnifier",
                                          "zoomStop",
                                          this,
                                          SLOT(zoomStopSlot()));
    QDBusConnection::sessionBus().connect("org.ukui.KWin",
                                          "/Magnifier",
                                          "org.ukui.Magnifier",
                                          "zoomStart",
                                          this,
                                          SLOT(zoomStartSlot()));
    QDBusConnection::sessionBus().connect("org.ukui.KWin",
                                          "/Magnifier",
                                          "org.ukui.KWin",
                                          "modeChanged",
                                          this,
                                          SLOT(modeChangedSlot(int)));
    QDBusConnection::sessionBus().connect("com.kylin.Wlcom",
                                          "/ColorFilter",
                                          "org.ukui.colorfilter",
                                          "colorFilterTypeChanged",
                                          this,
                                          SLOT(colorFilterTypeChangedSlot(QString)));
    QDBusConnection::sessionBus().connect("com.kylin.Wlcom",
                                          "/ColorFilter",
                                          "org.ukui.colorfilter",
                                          "statusChanged",
                                          this,
                                          SLOT(filterStatusChangedSlot(bool)));
    QDBusConnection::sessionBus().connect("com.kylin.Wlcom",
                                          "/ColorFilter",
                                          "org.ukui.colorfilter",
                                          "colorFilterShortCutEnabledChanged",
                                          this,
                                          SLOT(colorFilterShortCutEnabledChangedSlot(bool)));
}

void AccessibilityInterface::zoomStartSlot() {
    qDebug() << Q_FUNC_INFO << "signal zoomStart";
    emit statusChanged(true);
}

void AccessibilityInterface::zoomStopSlot() {
    qDebug() << Q_FUNC_INFO << "signal zoomStop";
    emit statusChanged(false);
}

void AccessibilityInterface::modeChangedSlot(int mode) {
    emit modeChanged(mode);
}

void AccessibilityInterface::colorFilterTypeChangedSlot(QString type) {
    emit colorFilterTypeChanged(type);
}

void AccessibilityInterface::filterStatusChangedSlot(bool enabled) {
    qDebug() << Q_FUNC_INFO << "filterStatusChangedSlot" << enabled;
    emit filterStatusChanged(enabled);
}

void AccessibilityInterface::colorFilterShortCutEnabledChangedSlot(bool enabled) {
    emit colorFilterShortCutEnabledChanged(enabled);
}

int AccessibilityInterface::GetType()
{
    QDBusReply<int> zoomModeReply = m_kwinInterface->call("GetType");
    qDebug() << Q_FUNC_INFO << "GetType" << zoomModeReply.value();
    return zoomModeReply.value();
}

bool AccessibilityInterface::isEffectSupported() {
    QDBusReply<bool> isEffectSupportedReply = m_wlcomRenderDBus->call("IsHardwareRendering");
    qDebug() << Q_FUNC_INFO << "isEffectSupported" << isEffectSupportedReply.value();
    return isEffectSupportedReply.value();
}

bool AccessibilityInterface::isColorFilterShortCutEnabled() {
    QDBusReply<bool> isColorFilterShortCutEnabledReply = m_kwinEffectInterface->call("isColorFilterShortCutEnabled");
    qDebug() << Q_FUNC_INFO << "isColorFilterShortCutEnabled" << isColorFilterShortCutEnabledReply.value();
    return isColorFilterShortCutEnabledReply.value();
}

bool AccessibilityInterface::isColorFilterEnabled() {
    QDBusReply<bool> isColorFilterEnabledReply = m_kwinEffectInterface->call("isColorFilterEnabled");
    qDebug() << Q_FUNC_INFO << "isColorFilterEnabled" << isColorFilterEnabledReply.value();
    return isColorFilterEnabledReply.value();
}

QString AccessibilityInterface::getCurrentColorFilter() {
    QString currentColorFilter;
    QDBusReply<QString> curColorFilterReply = m_kwinEffectInterface->call("getCurrentColorFilter");
    if (curColorFilterReply.isValid()) {
        currentColorFilter = curColorFilterReply.value();
    }
    qDebug() << Q_FUNC_INFO << "getCurrentColorFilter" << currentColorFilter;
    return currentColorFilter;
}

void AccessibilityInterface::setZoomMode(int index) {
    uint32_t idx = index;
    m_kwinInterface->call("SetMagnifierMode", idx);
    qDebug() << Q_FUNC_INFO << "setZoomMode" << idx;
}

void AccessibilityInterface::setColorFilter(const QString& mode) {
    qDebug() << Q_FUNC_INFO << "" << mode;
    m_kwinEffectInterface->call("setColorFilter", mode);
}

void AccessibilityInterface::setZoomSwitch(bool status) {
    qDebug() << Q_FUNC_INFO << "setZoomSwitch" << status;
    if (status) {
        QDBusReply<int32_t> scaleReply = m_kwinInterface->call("GetScale");
        uint32_t scale = scaleReply.value();
        QDBusReply<int32_t> modeReply = m_kwinInterface->call("GetType");
        uint32_t mode = modeReply.value();
        qDebug() << Q_FUNC_INFO << "StartZoom" << "mode" << mode << "scale" << scale << scaleReply.isValid() << modeReply.isValid();
        m_kwinInterface->call("StartZoom", mode, scale);
    } else {
        m_kwinInterface->call("StopZoom");
    }
}

void AccessibilityInterface::setColorFilterSwitch(bool toggled) {
    if (toggled) {
        qDebug() << Q_FUNC_INFO << "openColorFilter" << toggled;
        m_kwinEffectInterface->call("openColorFilter");
    } else {
        qDebug() << Q_FUNC_INFO << "closeColorFilter" << toggled;
        m_kwinEffectInterface->call("closeColorFilter");
    }
}

void AccessibilityInterface::setColorFilterShortCutEnabled(bool enabled) {
    qDebug() << Q_FUNC_INFO << "setColorFilterShortCutEnabled" << enabled;
    m_kwinEffectInterface->call("setColorFilterShortCutEnabled", enabled);
}

bool AccessibilityInterface::isZooming()
{
    QDBusReply<bool> isZoomingReply = m_kwinInterface->call("IsZooming");
    qDebug() << Q_FUNC_INFO << "IsZooming" << isZoomingReply.value();
    return isZoomingReply.value();
}
