/**
 * This file is generated by dconfig2cpp.
 * Command line arguments: ./dconfig2cpp -p ./toolGenerate/dconfig2cpp/org_deepin_dde_control-center_datetime.hpp ./misc/configs/org.deepin.dde.control-center.datetime.json
 * Generation time: 2025-09-23T10:04:59
 * JSON file version: 1.0
 *
 * WARNING: DO NOT MODIFY THIS FILE MANUALLY.
 * If you need to change the content, please modify the dconfig2cpp tool.
 */

#ifndef DCONFIG_ORG_DEEPIN_DDE_CONTROL-CENTER_DATETIME_H
#define DCONFIG_ORG_DEEPIN_DDE_CONTROL-CENTER_DATETIME_H

#include <QThread>
#include <QVariant>
#include <QPointer>
#include <QDebug>
#include <QAtomicPointer>
#include <QAtomicInteger>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <QProperty>
#endif
#include <DSGApplication>
#include <DConfig>

class dconfig_org_deepin_dde_control-center_datetime : public QObject {
    Q_OBJECT

    Q_PROPERTY(QString customNtpServer READ customNtpServer WRITE setCustomNtpServer NOTIFY customNtpServerChanged RESET resetCustomNtpServer)
    Q_CLASSINFO("DConfigKeyList", "customNtpServer")
    Q_CLASSINFO("DConfigFileName", "org.deepin.dde.control-center.datetime")
    Q_CLASSINFO("DConfigFileVersion", "1.0")

public:
    explicit dconfig_org_deepin_dde_control-center_datetime(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend,
                        const QString &name, const QString &appId, const QString &subpath,
                        bool isGeneric, QObject *parent)
                : QObject(nullptr) {
        if (!thread->isRunning()) {
            qWarning() << QLatin1String("Warning: The provided thread is not running.");
        }
        Q_ASSERT(QThread::currentThread() != thread);
        auto worker = new QObject();
        worker->moveToThread(thread);
        QPointer<QObject> watcher(parent);
        QMetaObject::invokeMethod(worker, [=, this]() {
            DTK_CORE_NAMESPACE::DConfig *config = nullptr;
            if (isGeneric) {
                if (backend) {
                    config = DTK_CORE_NAMESPACE::DConfig::createGeneric(backend, name, subpath, nullptr);
                } else {
                    config = DTK_CORE_NAMESPACE::DConfig::createGeneric(name, subpath, nullptr);
                }
            } else {
                if (backend) {
                    if (appId.isNull()) {
                        config = DTK_CORE_NAMESPACE::DConfig::create(backend, DTK_CORE_NAMESPACE::DSGApplication::id(),
                                                                     name, subpath, nullptr);
                    } else {
                        config = DTK_CORE_NAMESPACE::DConfig::create(backend, appId, name, subpath, nullptr);
                    }
                } else {
                    if (appId.isNull()) {
                        config = DTK_CORE_NAMESPACE::DConfig::create(DTK_CORE_NAMESPACE::DSGApplication::id(),
                                                                     name, subpath, nullptr);
                    } else {
                        config = DTK_CORE_NAMESPACE::DConfig::create(appId, name, subpath, nullptr);
                    }
                }
            }
            if (!config) {
                qWarning() << QLatin1String("Failed to create DConfig instance.");
                worker->deleteLater();
                return;
            }
            config->moveToThread(QThread::currentThread());
            initializeInConfigThread(config);
            if (watcher != parent) {
                // delete this if watcher is changed to nullptr.
                deleteLater();
            } else if (!this->parent() && parent) {
                // !parent() means that parent is not changed.
                this->setParent(watcher);
            }
            worker->deleteLater();
        });
    }
    static dconfig_org_deepin_dde_control-center_datetime* create(const QString &appId = {}, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, nullptr, QStringLiteral(u"\u006f\u0072\u0067\u002e\u0064\u0065\u0065\u0070\u0069\u006e\u002e\u0064\u0064\u0065\u002e\u0063\u006f\u006e\u0074\u0072\u006f\u006c\u002d\u0063\u0065\u006e\u0074\u0065\u0072\u002e\u0064\u0061\u0074\u0065\u0074\u0069\u006d\u0065"), appId, subpath, false, parent); }
    static dconfig_org_deepin_dde_control-center_datetime* create(DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &appId = {}, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, backend, QStringLiteral(u"\u006f\u0072\u0067\u002e\u0064\u0065\u0065\u0070\u0069\u006e\u002e\u0064\u0064\u0065\u002e\u0063\u006f\u006e\u0074\u0072\u006f\u006c\u002d\u0063\u0065\u006e\u0074\u0065\u0072\u002e\u0064\u0061\u0074\u0065\u0074\u0069\u006d\u0065"), appId, subpath, false, parent); }
    static dconfig_org_deepin_dde_control-center_datetime* createByName(const QString &name, const QString &appId = {}, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, nullptr, name, appId, subpath, false, parent); }
    static dconfig_org_deepin_dde_control-center_datetime* createByName(DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &name, const QString &appId = {}, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, backend, name, appId, subpath, false, parent); }
    static dconfig_org_deepin_dde_control-center_datetime* createGeneric(const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, nullptr, QStringLiteral(u"\u006f\u0072\u0067\u002e\u0064\u0065\u0065\u0070\u0069\u006e\u002e\u0064\u0064\u0065\u002e\u0063\u006f\u006e\u0074\u0072\u006f\u006c\u002d\u0063\u0065\u006e\u0074\u0065\u0072\u002e\u0064\u0061\u0074\u0065\u0074\u0069\u006d\u0065"), {}, subpath, true, parent); }
    static dconfig_org_deepin_dde_control-center_datetime* create(DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, backend, QStringLiteral(u"\u006f\u0072\u0067\u002e\u0064\u0065\u0065\u0070\u0069\u006e\u002e\u0064\u0064\u0065\u002e\u0063\u006f\u006e\u0074\u0072\u006f\u006c\u002d\u0063\u0065\u006e\u0074\u0065\u0072\u002e\u0064\u0061\u0074\u0065\u0074\u0069\u006d\u0065"), {}, subpath, true, parent); }
    static dconfig_org_deepin_dde_control-center_datetime* createGenericByName(const QString &name, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, nullptr, name, {}, subpath, true, parent); }
    static dconfig_org_deepin_dde_control-center_datetime* createGenericByName(DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &name, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread())
    { return new dconfig_org_deepin_dde_control-center_datetime(thread, backend, name, {}, subpath, true, parent); }
    ~dconfig_org_deepin_dde_control-center_datetime() {
        if (m_config.loadRelaxed()) {
            m_config.loadRelaxed()->deleteLater();
        }
    }

    Q_INVOKABLE DTK_CORE_NAMESPACE::DConfig *config() const {
        return m_config.loadRelaxed();
    }

    Q_INVOKABLE bool isInitializeSucceed() const {
        return m_status.loadRelaxed() == static_cast<int>(Status::Succeed);
    }

    Q_INVOKABLE bool isInitializeFailed() const {
        return m_status.loadRelaxed() == static_cast<int>(Status::Failed);
    }

    Q_INVOKABLE bool isInitializing() const {
        return m_status.loadRelaxed() == static_cast<int>(Status::Invalid);
    }

    Q_INVOKABLE QStringList keyList() const {
        return { QStringLiteral("customNtpServer")};
    }

    Q_INVOKABLE bool isDefaultValue(const QString &key) const {
        if (key == QStringLiteral("customNtpServer"))
            return customNtpServerIsDefaultValue();
        return false;
    }

    QString customNtpServer() const {
        return p_customNtpServer;
    }
    void setCustomNtpServer(const QString &value) {
        auto oldValue = p_customNtpServer;
        p_customNtpServer = value;
        markPropertySet(0);
        if (auto config = m_config.loadRelaxed()) {
            QMetaObject::invokeMethod(config, [this, value]() {
                m_config.loadRelaxed()->setValue(QStringLiteral("customNtpServer"), value);
            });
        }
        if (p_customNtpServer != oldValue) {
            Q_EMIT customNtpServerChanged();
            Q_EMIT valueChanged(QStringLiteral("customNtpServer"), value);
        }
    }
    void resetCustomNtpServer() {
        if (auto config = m_config.loadRelaxed()) {
            QMetaObject::invokeMethod(config, [this]() {
                m_config.loadRelaxed()->reset(QStringLiteral("customNtpServer"));
            });
        }
    }
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    QBindable<QString> bindableCustomNtpServer() {
        return QBindable<QString>(this, "customNtpServer");
    }
#endif
    Q_INVOKABLE bool customNtpServerIsDefaultValue() const {
        return !testPropertySet(0);
    }
Q_SIGNALS:
    void configInitializeFailed(DTK_CORE_NAMESPACE::DConfig *config);
    void configInitializeSucceed(DTK_CORE_NAMESPACE::DConfig *config);
    void valueChanged(const QString &key, const QVariant &value);

    void customNtpServerChanged();
private:
    void initializeInConfigThread(DTK_CORE_NAMESPACE::DConfig *config) {
        Q_ASSERT(!m_config.loadRelaxed());
        m_config.storeRelaxed(config);
        if (!config->isValid()) {
           m_status.storeRelaxed(static_cast<int>(Status::Failed));
           Q_EMIT configInitializeFailed(config);
           return;
        }

        if (testPropertySet(0)) {
            config->setValue(QStringLiteral("customNtpServer"), QVariant::fromValue(p_customNtpServer));
        } else {
            updateValue(QStringLiteral("customNtpServer"), QVariant::fromValue(p_customNtpServer));
        }

        connect(config, &DTK_CORE_NAMESPACE::DConfig::valueChanged, this, [this](const QString &key) {
            updateValue(key);
        }, Qt::DirectConnection);

        m_status.storeRelaxed(static_cast<int>(Status::Succeed));
        Q_EMIT configInitializeSucceed(config);
    }
    void updateValue(const QString &key, const QVariant &fallback = QVariant()) {
        Q_ASSERT(QThread::currentThread() == m_config.loadRelaxed()->thread());
        const QVariant &value = m_config.loadRelaxed()->value(key, fallback);
        if (key == QStringLiteral("customNtpServer")) {
            markPropertySet(0, !m_config.loadRelaxed()->isDefaultValue(key));
            auto newValue = qvariant_cast<QString>(value);
            QMetaObject::invokeMethod(this, [this, newValue, key, value]() {
                Q_ASSERT(QThread::currentThread() == this->thread());
                if (p_customNtpServer != newValue) {
                    p_customNtpServer = newValue;
                    Q_EMIT customNtpServerChanged();
                    Q_EMIT valueChanged(key, value);
                }
            });
            return;
        }
    }
    inline void markPropertySet(const int index, bool on = true) {
        if (index < 32) {
            if (on)
                m_propertySetStatus0.fetchAndOrOrdered(1 << (index - 0));
            else
                m_propertySetStatus0.fetchAndAndOrdered(~(1 << (index - 0)));
            return;
        }
        Q_UNREACHABLE();
    }
    inline bool testPropertySet(const int index) const {
        if (index < 32) {
            return (m_propertySetStatus0.loadRelaxed() & (1 << (index - 0)));
        }
        Q_UNREACHABLE();
    }

    QAtomicPointer<DTK_CORE_NAMESPACE::DConfig> m_config = nullptr;

public:
    enum class Status {
        Invalid = 0,
        Succeed = 1,
        Failed = 2
    };
private:
    QAtomicInteger<int> m_status = static_cast<int>(Status::Invalid);

    // Default value: ""
    QString p_customNtpServer { QLatin1String("") };
    QAtomicInteger<quint32> m_propertySetStatus0 = 0;
};

#endif // DCONFIG_ORG_DEEPIN_DDE_CONTROL-CENTER_DATETIME_H
