/*
 * Copyright (C) 2023 KylinSoftCo., 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, 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 <http://www.gnu.org/licenses/>.
 *
 **/
#include "scconfiguration.h"
#include "utils.h"
#include <QDebug>
#include <QSettings>
#include <QGSettings>
#include <QString>
#include <QScreen>
#include <QApplication>
#include "lock-dialog/backenddbushelper.h"
#include "definetypes.h"

SCConfiguration *SCConfiguration::instance_ = nullptr;

SCConfiguration::SCConfiguration(QObject *parent) : QObject(parent)
{
    QString displayNum = QString(qgetenv("DISPLAY")).replace(":", "").replace(".", "_");
    QString sessionDbus = QString("%1%2").arg(QString(SS_DBUS_SERVICE)).arg(displayNum);
    m_helperBackendDbus = new BackendDbusHelper(sessionDbus, SS_DBUS_PATH, QDBusConnection::sessionBus(), this);
    if (!m_helperBackendDbus->isValid()) {
        delete m_helperBackendDbus;
        m_helperBackendDbus = new BackendDbusHelper(SS_DBUS_SERVICE, SS_DBUS_PATH, QDBusConnection::sessionBus(), this);
    }
    connect(
        m_helperBackendDbus,
        &BackendDbusHelper::screenSaverConfChanged,
        this,
        &SCConfiguration::onConfigurationChanged);
    connect(
        m_helperBackendDbus,
        &BackendDbusHelper::ukccPluginsConfChanged,
        this,
        &SCConfiguration::onConfigurationChanged);
    connect(
        m_helperBackendDbus, &BackendDbusHelper::themeStyleConfChanged, this, &SCConfiguration::onConfigurationChanged);
    connect(
        m_helperBackendDbus, &BackendDbusHelper::lockScreenConfChanged, this, &SCConfiguration::onConfigurationChanged);
    connect(
        m_helperBackendDbus,
        &BackendDbusHelper::networkConnectStateChanged,
        this,
        &SCConfiguration::onNetworkConnectStateChanged);
    connect(
        m_helperBackendDbus,
        &BackendDbusHelper::saverDatetimeEnableChanged,
        this,
        &SCConfiguration::onSaverDatetimeEnableChanged);
}

SCConfiguration *SCConfiguration::instance(QObject *parent)
{
    if (instance_ == nullptr)
        instance_ = new SCConfiguration(parent);
    return instance_;
}

void SCConfiguration::onConfigurationChanged(QString key, QVariant value)
{
    if (key == "mode") {
        bool ret = false;
        ret = (value.toString() == "default-ukui-custom");
        Q_EMIT isCustomModeChanged(ret);
    } else if (key == "cycleTime") {
        int cycleTime = value.toInt();
        Q_EMIT cycleTimeChanged(cycleTime);
    } else if (key == "automaticSwitchingEnabled") {
        bool changed = value.toBool();
        Q_EMIT autoSwitchChanged(changed);
    } else if (key == "backgroundPath") {
        QString path = value.toString();
        Q_EMIT backgroundPathChanged(path);
    } else if (key == "background") {
        QString path = value.toString();
        Q_EMIT backgroundChanged(path);
    } else if (key == "mytext") {
        QString text = value.toString();
        Q_EMIT myTextChanged(text);
    } else if (key == "showCustomRestTime") {
        bool ret = value.toInt();
        Q_EMIT showCRestTimeChanged(ret);
    } else if (key == "showUkuiRestTime") {
        bool ret = value.toInt();
        Q_EMIT showURestTimeChanged(ret);
    } else if (key == "textIsCenter") {
        bool ret = value.toBool();
        Q_EMIT textIsCenterChanged(ret);
    } else if (key == "showMessageEnabled") {
        bool ret = value.toBool();
        Q_EMIT messageShowEnableChanged(ret);
    } else if (key == "messageNumber") {
        int num = value.toInt();
        Q_EMIT messageNumberChanged(num);
    } else if (key == "hoursystem") {
        int timeType = value.toInt();
        Q_EMIT timeTypeChanged(timeType);
    } else if (key == "type") {
        QString dateType = value.toString();
        Q_EMIT dateTypeChanged(dateType);
    } else if (key == "menuTransparency") {
        int blur_Num = value.toInt();
        Q_EMIT blurChanged(blur_Num);
    } else if (key == "styleName") {
        QString m_curStyle = value.toString();
        Q_EMIT styleChanged(m_curStyle);
    } else if (key == "systemFontSize") {
        double m_curFontSize = value.toDouble();
        qDebug() << "curFontSize = " << m_curFontSize;
        Q_EMIT fontSizeChanged(m_curFontSize);
    } else if (key == "subMode") {
        int subMode = value.toInt();
        Q_EMIT subModeChanged(subMode);
    } else if (key == "weatherPosition") {
        int position = value.toInt();
        Q_EMIT weatherPositionChanged(position);
    } else if (key == "musicPath") {
        QString musicPath = value.toString();
        Q_EMIT musicPathChanged(musicPath);
    } else if (key == "photoCycleMode") {
        int photoCycleMode = value.toInt();
        Q_EMIT photoCycleModeChanged(photoCycleMode);
    } else if (key == "photoCycleInterval") {
        int photoCycleInterval = value.toInt();
        Q_EMIT photoCycleIntervalChanged(photoCycleInterval);
    } else if (key == "photoPath") {
        QString photoPath = value.toString();
        Q_EMIT photoPathChanged(photoPath);
    } else if (key == "musicFollowSystem") {
        bool b = value.toBool();
        Q_EMIT musicFollowSystemChanged(b);
    } else if (key == "photoSelectPath") {
        QString photoPath = value.toString();
        Q_EMIT photoSelectPathChanged(photoPath);
    }
}

void SCConfiguration::onNetworkConnectStateChanged(int state)
{
    Q_EMIT networkConnectStateChanged(state);
}

void SCConfiguration::onSaverDatetimeEnableChanged(bool isEnable, QString username)
{
    Q_EMIT saverDatetimeEnableChanged(isEnable, username);
}

QString SCConfiguration::getDefaultBackground()
{
    QString backgroundFile = m_helperBackendDbus->getLockScreenConf(KEY_BACKGROUND).toString();
    if (ispicture(backgroundFile)) {
        return backgroundFile;
    } else {
        QString default_background_path = "/usr/share/backgrounds/1-warty-final-ubuntukylin.jpg";
        if (ispicture(default_background_path)) {
            return default_background_path;
        }
        return DEFAULT_BACKGROUND_PATH;
    }
}

int SCConfiguration::getTimeType()
{
    int timeType = 24;
    timeType = m_helperBackendDbus->getUkccPluginsConf(KEY_HOUR_SYSTEM).toInt();
    return timeType;
}

QString SCConfiguration::getDateType()
{
    QString dateType = "cn";
    dateType = m_helperBackendDbus->getUkccPluginsConf(KEY_DATE).toString();
    return dateType;
}

int SCConfiguration::getFontSize()
{
    double fontSize = 0;
    fontSize = m_helperBackendDbus->getThemeStyleConf(KEY_SYSTEM_FONT_SIZE).toDouble();
    double defaultFontSize = getDefaultFontSize();
    qDebug() << "defaultFontSize = " << defaultFontSize;

    return fontSize - defaultFontSize;
}

double SCConfiguration::getPtToPx()
{
    double m_ptTopx = 1.0;
    if (QApplication::primaryScreen() && QApplication::primaryScreen()->logicalDotsPerInch() > 0) {
        m_ptTopx = 72 / (QApplication::primaryScreen()->logicalDotsPerInch());
    }
    return m_ptTopx;
}

bool SCConfiguration::getAutoSwitch()
{
    bool ret = false;
    ret = m_helperBackendDbus->getScreenSaverConf(KEY_AUTOMATIC_SWITCHING_ENABLE).toBool();
    return ret;
}

bool SCConfiguration::getIsCustom()
{
    bool ret = false;
    ret = (m_helperBackendDbus->getLockScreenConf("mode").toString() == "default-ukui-custom");

    return ret;
}

bool SCConfiguration::getMessageShowEnable()
{
    bool ret = false;
    ret = m_helperBackendDbus->getScreenSaverConf(KEY_SHOW_MESSAGE_ENABLED).toBool();

    return ret;
}

int SCConfiguration::getMessageNumber()
{
    bool ret = false;
    ret = (m_helperBackendDbus->getLockScreenConf("mode").toString() == "default-ukui-custom");

    return ret;
}

bool SCConfiguration::getCShowRestTime()
{
    bool ret = true;
    ret = m_helperBackendDbus->getScreenSaverConf(KEY_SHOW_CUSTOM_REST_TIME).toBool();
    return ret;
}

bool SCConfiguration::getUShowRestTime()
{
    bool ret = true;
    ret = m_helperBackendDbus->getScreenSaverConf(KEY_SHOW_UKUI_REST_TIME).toBool();
    return ret;
}

int SCConfiguration::getCycleTime()
{
    int cycleTime = 300;
    cycleTime = m_helperBackendDbus->getScreenSaverConf(KEY_CYCLE_TIME).toInt();
    return cycleTime;
}

QString SCConfiguration::getBackgroundPath()
{
    QString backgroundPath = "/usr/share/backgrounds";
    backgroundPath = m_helperBackendDbus->getScreenSaverConf(KEY_BACKGROUND_PATH).toString();
    return backgroundPath;
}

bool SCConfiguration::getTextIsCenter()
{
    bool ret = true;
    ret = m_helperBackendDbus->getScreenSaverConf(KEY_TEXT_IS_CENTER).toBool();
    return ret;
}

QString SCConfiguration::getMyText()
{
    QString myText = "";
    myText = m_helperBackendDbus->getScreenSaverConf("mytext").toString();
    return myText;
}

int SCConfiguration::getBlurNumber()
{
    int blurNum = 50;
    blurNum = m_helperBackendDbus->getThemeStyleConf("menuTransparency").toInt();
    return blurNum;
}

QString SCConfiguration::getcurStyle()
{
    QString curStyle;
    curStyle = m_helperBackendDbus->getThemeStyleConf("styleName").toString();
    return curStyle;
}

QString SCConfiguration::getVideoPath()
{
    QString videoPath;
    videoPath = m_helperBackendDbus->getScreenSaverConf(KEY_VIDEO_PATH).toString();
    return videoPath;
}

QString SCConfiguration::getVideoFormat()
{
    QString videoFormat;
    videoFormat = m_helperBackendDbus->getScreenSaverConf(KEY_VIDEO_FORMAT).toString();
    return videoFormat;
}

int SCConfiguration::getVideoSize()
{
    int videoSize;
    videoSize = m_helperBackendDbus->getScreenSaverConf(KEY_VIDEO_SIZE).toInt();
    return videoSize;
}

int SCConfiguration::getVideoWidth()
{
    int videoWidth;
    videoWidth = m_helperBackendDbus->getScreenSaverConf(KEY_VIDEO_WIDTH).toInt();
    return videoWidth;
}

int SCConfiguration::getVideoHeight()
{
    int videoHeight;
    videoHeight = m_helperBackendDbus->getScreenSaverConf(KEY_VIDEO_HEIGHT).toInt();
    return videoHeight;
}

int SCConfiguration::getSubMode()
{
    int subMode;
    subMode = m_helperBackendDbus->getScreenSaverConf(KEY_SUB_MODE).toInt();
    return subMode;
}

void SCConfiguration::setSubMode(int mode)
{
    QVariant subMode = mode;
    m_helperBackendDbus->setScreenSaverConf(KEY_SUB_MODE, subMode);
}

int SCConfiguration::getWeatherPosition()
{
    int weatherPosition;
    weatherPosition = m_helperBackendDbus->getScreenSaverConf(KEY_WEATHER_POSITION).toInt();
    return weatherPosition;
}

QString SCConfiguration::getMusicPath()
{
    QString musicPath;
    musicPath = m_helperBackendDbus->getScreenSaverConf(KEY_MUSIC_PATH).toString();
    return musicPath;
}

bool SCConfiguration::getMusicFollowSystem()
{
    bool value;
    value = m_helperBackendDbus->getScreenSaverConf(KEY_MUSIC_FOLLOW_SYSTEM).toBool();
    return value;
}

int SCConfiguration::getPhotoCycleMode()
{
    int photoCycleMode;
    photoCycleMode = m_helperBackendDbus->getScreenSaverConf(KEY_PHOTO_CYCLE_MODE).toInt();
    return photoCycleMode;
}

int SCConfiguration::getPhotoCycleInterval()
{
    int photoCycleInterval;
    photoCycleInterval = m_helperBackendDbus->getScreenSaverConf(KEY_PHOTO_CYCLE_INTERVAL).toInt();
    return photoCycleInterval;
}

QStringList SCConfiguration::getDefaultPhotoPath()
{
    QStringList defaultPhotoPath;
    defaultPhotoPath = m_helperBackendDbus->getDefaultPhotoPath();
    qDebug() << " defaultPhotoPath = " << defaultPhotoPath;
    return defaultPhotoPath;
}

QStringList SCConfiguration::getCustomPhotoPath()
{
    QStringList customPhotoPath;
    customPhotoPath = m_helperBackendDbus->getCustomPhotoPath();
    qDebug() << " customPhotoPath = " << customPhotoPath;
    return customPhotoPath;
}

QString SCConfiguration::getSelectPhotoPath()
{
    QString selectPhotoPath;
    selectPhotoPath = m_helperBackendDbus->getScreenSaverConf(KEY_PHOTO_SELECT_PATH).toString();
    qDebug() << " selectPhotoPath = " << selectPhotoPath;
    return selectPhotoPath;
}

int SCConfiguration::getNetworkConnectState()
{
    return m_helperBackendDbus->getNetworkConnectState();
}

QString SCConfiguration::getWeatherData()
{
    return m_helperBackendDbus->getWeatherData(KEY_WEATHER).toString();
}

bool SCConfiguration::getSaverDatetimeEnable()
{
    return m_helperBackendDbus->getSaverDatetimeEnable();
}
