/*
 * 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 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: Wenjie Xiang <xiangwenjie@kylinos.cn>
 *
 */

#ifndef BACKGROUNDMANAGER_H
#define BACKGROUNDMANAGER_H

#include <QObject>
#include <QGSettings>
#include <QColor>
#include <QRect>
#include <QQmlEngine>

#define BACKGROUND_MATE_SETTINGS "org.mate.background"
#define BACKGROUND_MATE_PATH "/org/mate/desktop/background/"
#define BACKGROUND_SETTINGS "org.ukui.interface"
#define BACKGROUND_PATH "/org/ukui/interface/"
#define ACCOUNT_SETTINGS "org.freedesktop.Accounts"
#define ACCOUNT_PATH "/org/freedesktop/Accounts"
#define PICTUREFILENAME "pictureFilename"
#define PICTUREOPTIONS "pictureOptions"
#define PRIMARYCOLOR "primaryColor"
#define PICTURE_FILENAME "picture-filename"
#define DEFAULTBACKGROUND "/usr/share/backgrounds/ubuntukylin-default-settings.jpg"

class BackgroundManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString backgroundOption READ getBackgroundOption WRITE setBackgroundOption NOTIFY backgroundOptionChanged)
    Q_PROPERTY(QString accountBackground READ getAccountBackground WRITE setAccountBackground NOTIFY accountBackgroundChanged)
    Q_PROPERTY(bool usePureColor READ getUsePureColor WRITE setUsePureColor NOTIFY usePureColorChanged)
    Q_PROPERTY(QColor color READ getColor WRITE setColor NOTIFY colorChanged)

public:
    static BackgroundManager *globalInstance();

    Q_INVOKABLE QString getBackImagePath() const;
    Q_INVOKABLE QString getFrontImagePath() const;

    Q_INVOKABLE QString getBackgroundOption() const;
    Q_INVOKABLE void setBackgroundOption(const QString &backgroundOption);

    Q_INVOKABLE QString getAccountBackground() const;
    Q_INVOKABLE void setAccountBackground(const QString &backgroundPath);

    Q_INVOKABLE bool getUsePureColor() const;
    Q_INVOKABLE void setUsePureColor(const bool usePureColor);

    Q_INVOKABLE QColor getColor() const;
    Q_INVOKABLE void setColor(const QColor &color);

    Q_INVOKABLE QRect calculateSpannedSourceRect(const QRect &screenGeometry, const QString &imagePath) const;

private:
    explicit BackgroundManager(QObject *parent = nullptr);
    void initGSettings();
    void initBackground();
    void switchBackground();

Q_SIGNALS:
    void backgroundOptionChanged();
    void accountBackgroundChanged();
    void usePureColorChanged();
    void colorChanged();
    void backgroundImageChanged();

private:
    QString m_backImagePath;
    QString m_frontImagePath;
    bool m_usePureColor = false;
    QColor m_color = Qt::black;
    QString m_backgroundOption;
    QGSettings *m_backgroundSettings = nullptr;
    bool m_shouldSyncAccountBackground = true;
};

#endif // BACKGROUNDMANAGER_H
