#include "wallpaperinterface.h"

WallpaperInterface::WallpaperInterface()
{
    WorkerObject *workThread = new WorkerObject(this);
    connect(workThread, &WorkerObject::wallpaperReaded, this, [=](QStringList previewFiles, QStringList sourceFiles) {
        previewWallpaperList = previewFiles;
        sourceWallpaperList  = sourceFiles;
    });
    workThread->start();

    connect(UniversalInterface::self()->mateBackgroundGsettings(), &QGSettings::changed, this, [=](QString key){
        if (key == "pictureOptions") {
            Q_EMIT changed("displayType");
        } else if(key == "pictureFilename") {
            Q_EMIT changed("wallpaper");
        }
    });
}

WallpaperInterface::~WallpaperInterface()
{

}

QString WallpaperInterface::getWallpaperMode()
{
    QString mode = "";
    QString filename = "";
    filename = UniversalInterface::self()->mateBackgroundGsettings()->get("picture-filename").toString();
    if (filename.isEmpty()) {
        mode = "color";
    } else {
        mode = "picture";
    }
    return mode;
}

QString WallpaperInterface::getDisplayType()
{
    QString type = "";
    type = UniversalInterface::self()->mateBackgroundGsettings()->get("picture-options").toString();
    return type;
}

QString WallpaperInterface::getWallpaper()
{
    if (getWallpaperMode() == "color") {
        return UniversalInterface::self()->mateBackgroundGsettings()->get("primary-color").toString();
    } else {
        QString filename = UniversalInterface::self()->accountUserDbus()->property("BackgroundFile").toString();
        if (filename.isEmpty() || !QFile::exists(filename) ||
            filename == "/usr/share/backgrounds/ubuntukylin-default-settings.jpg") {
            filename = "/usr/share/backgrounds/1-warty-final-ubuntukylin.jpg";
        }
        return filename;
    }
}

QStringList WallpaperInterface::getPreviewWallpapers()
{
    return previewWallpaperList;
}

QStringList WallpaperInterface::getSourceWallpapers()
{
    return sourceWallpaperList;
}

void WallpaperInterface::setDisplayType(QString type)
{
    UniversalInterface::self()->mateBackgroundGsettings()->set("picture-options", type);
    Q_EMIT changed("displayType");
}

void WallpaperInterface::setWallpaper(QString mode, QString data)
{
    changeGlobalTheme();
    QString name = qgetenv("USER");
    if (name.isEmpty()) {
        name = qgetenv("USERNAME");
    }
    QString confFilename = "/var/lib/lightdm-data/" + name + "/ukui-greeter.conf";
    QSettings qsetting(confFilename, QSettings::IniFormat);
    qsetting.beginGroup("greeter");
    if (mode == "color") {
        qsetting.setValue("color", data);
        UniversalInterface::self()->mateBackgroundGsettings()->set("picture-filename", "");
        UniversalInterface::self()->mateBackgroundGsettings()->set("primary-color", QVariant(data));
        UniversalInterface::self()->mateBackgroundGsettings()->set("secondary-color", QVariant(data));
    } else {
        qsetting.setValue("color", "");
        UniversalInterface::self()->accountUserDbus()->call("SetBackgroundFile", data);
        UniversalInterface::self()->mateBackgroundGsettings()->set("picture-filename", data);
    }
    qsetting.endGroup();
    Q_EMIT changed("wallpaper");
}

void WallpaperInterface::resetDefault()
{
    UniversalInterface::self()->mateBackgroundGsettings()->reset("picture-filename");
    setWallpaper("picture", UniversalInterface::self()->mateBackgroundGsettings()->get("picture-filename").toString());
}

void WallpaperInterface::changeGlobalTheme()
{
    if (QGSettings::isSchemaInstalled(QByteArray("org.ukui.globaltheme.settings"))) {
        QGSettings globalTheme("org.ukui.globaltheme.settings");
        globalTheme.set("global-theme-name", "custom");
    }
}
