// Copyright (C) 2025-2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "settingmanager.h"
#include "common/treelandlogging.h"

const static qreal BASE_DPI = 96;
const static qreal XSETTINGS_BASE_DPI_FIXED = BASE_DPI * 1024;

SettingManager::SettingManager(xcb_connection_t *connection, QObject *parent)
    : QObject(parent)
    , m_resource(new XResource(connection, this))
    , m_settings(new XSettings(connection, this))
{
}

SettingManager::~SettingManager()
{
}

void SettingManager::setGTKTheme(const QString &themeName)
{
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Gtk_ThemeName), themeName);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Gtk_ThemeName), themeName);
}

QString SettingManager::GTKTheme() const
{
    return m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Gtk_ThemeName)).toString();
}

void SettingManager::setFont(const QString &name)
{
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Gtk_FontName), name);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Gtk_FontName), name);
}

QString SettingManager::font() const
{
    return m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Gtk_FontName)).toString();
}

void SettingManager::setIconTheme(const QString &theme)
{
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Gtk_IconThemeName), theme);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Gtk_IconThemeName), theme);
}

QString SettingManager::iconTheme() const
{
    return m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Gtk_IconThemeName)).toString();
}

void SettingManager::setSoundTheme(const QString &theme)
{
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Net_SoundThemeName), theme);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Net_SoundThemeName), theme);
}

QString SettingManager::soundTheme() const
{
    return m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Net_SoundThemeName)).toString();
}

void SettingManager::setCursorTheme(const QString &theme)
{
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Gtk_CursorThemeName), theme);
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Xcursor_Theme), theme);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Gtk_CursorThemeName), theme);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Xcursor_Theme), theme);
}

QString SettingManager::cursorTheme() const
{
    const auto theme =
        m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Gtk_CursorThemeName));
    if (theme.isValid()) {
        return theme.toString();
    }

    return m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Xcursor_Theme))
        .toString();
}

void SettingManager::setCursorSize(qreal value)
{
    const int size = qRound(value);

    m_resource->setPropertyValue(XResource::toByteArray(XResource::Xcursor_Size), size);
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Gtk_CursorThemeSize), size);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Xcursor_Size), size);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Gtk_CursorThemeSize), size);
}

qreal SettingManager::cursorSize() const
{
    const auto size = m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Xcursor_Size));
    if (size.isValid()) {
        return size.toReal();
    }

    return m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Gtk_CursorThemeSize))
        .toReal();
}

void SettingManager::setDoubleClickInterval(int interval)
{
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Net_DoubleClickTime), interval);
}

void SettingManager::setGlobalScale(qreal scale)
{
    m_resource->setPropertyValue(XResource::toByteArray(XResource::Xft_DPI), scale * BASE_DPI);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Gdk_WindowScalingFactor), qFloor(scale));
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Gdk_UnscaledDPI), XSETTINGS_BASE_DPI_FIXED);
    m_settings->setPropertyValue(XSettings::toByteArray(XSettings::Xft_DPI), qRound(scale * XSETTINGS_BASE_DPI_FIXED));
}

qreal SettingManager::globalScale() const
{
    return m_settings->getPropertyValue(XSettings::toByteArray(XSettings::Xft_DPI)).toReal() / XSETTINGS_BASE_DPI_FIXED;
}

void SettingManager::apply()
{
    m_resource->apply();
    m_settings->apply();
}
