/*
    KWin - the KDE window manager
    This file is part of the KDE project.

    SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "switcheritem.h"
// KWin
#include "composite.h"
#include "core/output.h"
#include "tabbox/tabbox.h"
#include "tabboxhandler.h"
#include "workspace.h"
// Qt
#include <QAbstractItemModel>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>

namespace KWin
{
namespace TabBox
{

SwitcherItem::SwitcherItem(QObject *parent)
    : QObject(parent)
    , m_model(nullptr)
    , m_item(nullptr)
    , m_visible(false)
    , m_allDesktops(false)
    , m_currentIndex(0)
    , m_themeSettings(nullptr)
{
    m_selectedIndexConnection = connect(tabBox, &TabBoxHandler::selectedIndexChanged, this, [this] {
        if (isVisible()) {
            setCurrentIndex(tabBox->currentIndex().row());
        }
    });

    if (QGSettings::isSchemaInstalled("org.ukui.style")) {
        m_themeSettings.reset(new QGSettings("org.ukui.style"));

        if (m_themeSettings) {
            const QString themeName = m_themeSettings->get("style-name").toString();
            m_theme = themeName.isEmpty() ? QStringLiteral("ukui-default") : themeName;

            connect(m_themeSettings.get(), &QGSettings::changed, this, [this](const QString &key) {
                if (key == "styleName") {
                    const QString newthemeName = m_themeSettings->get("style-name").toString();
                    if (!newthemeName.isEmpty() && newthemeName != m_theme) {
                        m_theme = newthemeName;
                        Q_EMIT themeChanged();
                    }
                }
            });
        }

        Q_EMIT themeChanged();
    }
    connect(workspace(), &Workspace::outputsChanged, this, &SwitcherItem::screenGeometryChanged);
    connect(Compositor::self(), &Compositor::compositingToggled, this, &SwitcherItem::compositingChanged);
}

SwitcherItem::~SwitcherItem()
{
    disconnect(m_selectedIndexConnection);
}

void SwitcherItem::setItem(QObject *item)
{
    if (m_item == item) {
        return;
    }
    m_item = item;
    Q_EMIT itemChanged();
}

void SwitcherItem::setModel(QAbstractItemModel *model)
{
    m_model = model;
    Q_EMIT modelChanged();
}

int SwitcherItem::getThemeRadius()
{
    // default to 8
    QString widgetName = "default";
    if (m_themeSettings && m_themeSettings->keys().contains("widgetThemeName")) {
        widgetName = m_themeSettings->get("widgetThemeName").toString();
    } else {
        qWarning() << QString("未查询到Gesetting的键值(%1)").arg("widgetThemeName");
        return 8;
    }

    QFile file(QString("/usr/share/qt5-ukui-platformtheme/themeconfig/%1.json").arg(widgetName));
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning() << "无法打开JSON文件";
        return 8;
    }

    QByteArray jsonData = file.readAll();
    file.close();

    // 将JSON数据转换为QJsonDocument对象
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
    if (error.error != QJsonParseError::NoError) {
        qWarning() << "JSON格式错误:" << error.errorString();
        return 8;
    }

    // 获取属性
    QJsonObject rootObj = jsonDoc.object();
    QJsonObject radiusObj = rootObj["Radius"].toObject();
    QJsonObject radiusValueObj = radiusObj["Max_Radius"].toObject();

    return radiusValueObj["value"].toString().toInt();
}

void SwitcherItem::setVisible(bool visible)
{
    if (m_visible == visible) {
        return;
    }
    if (visible) {
        Q_EMIT screenGeometryChanged();
    }
    m_visible = visible;
    Q_EMIT visibleChanged();
}

QRect SwitcherItem::screenGeometry() const
{
    return workspace()->activeOutput()->geometry();
}

void SwitcherItem::setCurrentIndex(int index)
{
    if (m_currentIndex == index) {
        return;
    }
    m_currentIndex = index;
    if (m_model) {
        tabBox->setCurrentIndex(m_model->index(index, 0));
    }
    Q_EMIT currentIndexChanged(m_currentIndex);
}

void SwitcherItem::setAllDesktops(bool all)
{
    if (m_allDesktops == all) {
        return;
    }
    m_allDesktops = all;
    Q_EMIT allDesktopsChanged();
}

void SwitcherItem::setNoModifierGrab(bool set)
{
    if (m_noModifierGrab == set) {
        return;
    }
    m_noModifierGrab = set;
    Q_EMIT noModifierGrabChanged();
}

void SwitcherItem::selectWindow()
{
    workspace()->tabbox()->accept(false);
    workspace()->tabbox()->close(true);
}

bool SwitcherItem::compositing()
{
    return Compositor::compositing();
}

}
}
