/*
 * Copyright (C) 2018 Tianjin KYLIN Information Technology 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, 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 "commonfunc.h"
#include <QDebug>
#include <QSettings>
#include <QGSettings>
#include <QString>
#include <kysdk/kysdk-system/libkysysinfo.h>
#include <QScreen>
#include <QApplication>
#include "glibinterface.h"

#define GSETTINGS_SCHEMA_SCREENSAVER 		"org.ukui.screensaver"
#define GSETTINGS_SCHEMA_MATE_BACKGROUND 	"org.mate.background"
#define GSETTINGS_SCHEMA_SCREENSAVER_DEFAULT 	"org.ukui.screensaver-default"
#define TIME_TYPE_SCHEMA 			"org.ukui.control-center.panel.plugins"
#define GSETTINGS_SCHEMA_STYLE      "org.ukui.style"

SCConfiguration* SCConfiguration::instance_ = nullptr;

SCConfiguration::SCConfiguration(QObject *parent) :
    QObject(parent),
    mgsettings(nullptr),
    ukgsettings(nullptr),
    udgsettings(nullptr),
    timegsettings(nullptr),
    stygsettings(nullptr)
{
    initGsettings();
}

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

void SCConfiguration::initGsettings()
{
    if(QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_MATE_BACKGROUND))
    	mgsettings = new QGSettings(GSETTINGS_SCHEMA_MATE_BACKGROUND, "", this);

    if(QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_SCREENSAVER))
    	ukgsettings = new QGSettings(GSETTINGS_SCHEMA_SCREENSAVER, "", this);
    
    if(QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_SCREENSAVER_DEFAULT))
    	udgsettings   = new QGSettings(GSETTINGS_SCHEMA_SCREENSAVER_DEFAULT,"",this);
    
    if(QGSettings::isSchemaInstalled(TIME_TYPE_SCHEMA))
    	timegsettings = new QGSettings(TIME_TYPE_SCHEMA,"",this);

    if(QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_STYLE))
        stygsettings = new QGSettings(GSETTINGS_SCHEMA_STYLE, "", this);

    connect(udgsettings, &QGSettings::changed,
            this, &SCConfiguration::onConfigurationChanged);
    connect(ukgsettings, &QGSettings::changed,
            this, &SCConfiguration::onConfigurationChanged);
    connect(timegsettings, &QGSettings::changed,
            this, &SCConfiguration::onConfigurationChanged);
    connect(stygsettings, &QGSettings::changed,
            this, &SCConfiguration::onConfigurationChanged);
}

void SCConfiguration::initDefaultSettings()
{
     

}

void SCConfiguration::onConfigurationChanged(QString key)
{
    if(key == "cycleTime"){
        int cycleTime = getCycleTime();
        Q_EMIT cycleTimeChanged(cycleTime);
    }else if(key == "automaticSwitchingEnabled"){
        bool changed = getAutoSwitch();
        Q_EMIT autoSwitchChanged(changed);
    }else if(key == "backgroundPath"){
        QString path = getBackgroundPath();
        Q_EMIT backgroundPathChanged(path);
    }else if(key == "mytext"){
        QString text = getMyText();
        Q_EMIT myTextChanged(text);
    }else if(key == "showCustomRestTime"){
        bool ret = getCShowRestTime();
        Q_EMIT showCRestTimeChanged(ret);
    }else if(key == "showUkuiRestTime"){
        bool ret = getUShowRestTime();
        Q_EMIT showURestTimeChanged(ret);
    }else if(key == "textIsCenter"){
        bool ret = getTextIsCenter();
        Q_EMIT textIsCenterChanged(ret);
    }else if(key == "showMessageEnabled"){
        bool ret = getMessageShowEnable();
        Q_EMIT messageShowEnableChanged(ret);
    }else if(key == "messageNumber"){
        int num = getMessageNumber();
        Q_EMIT messageNumberChanged(num);
    }else if(key == "hoursystem"){
        int timeType = timegsettings->get("hoursystem").toInt();
        Q_EMIT timeTypeChanged(timeType);
    }else if(key == "type"){
        QString dateType = timegsettings->get("date").toString();
        Q_EMIT dateTypeChanged(dateType);
    }else if(key == "menuTransparency"){
        int blur_Num = stygsettings->get("menuTransparency").toInt();
        Q_EMIT blurChanged(blur_Num);
    }else if(key == "styleName"){
        QString m_curStyle = stygsettings->get("styleName").toString();
        Q_EMIT styleChanged(m_curStyle);
    } else if (key == "systemFontSize") {
        double m_curFontSize = stygsettings->get("systemFontSize").toDouble();
        qDebug() << "curFontSize = " << m_curFontSize ;
        Q_EMIT fontSizeChanged(m_curFontSize);
    }
}

QString SCConfiguration::getDefaultBackground()
{
    QString backgroundFile = "";
    if(ukgsettings){
        backgroundFile = ukgsettings->get("background").toString();
    }

    if(ispicture(backgroundFile)) {
        return backgroundFile;
    } else {
        char *systemName = kdk_system_get_systemName();
        if (systemName) {
            if (QString(systemName) == "openKylin") {
                free(systemName);
                return "/usr/share/backgrounds/1-openkylin.jpg";
            }
            free(systemName);
        }
        return "/usr/share/backgrounds/1-warty-final-ubuntukylin.jpg";
    }
}

int SCConfiguration::getTimeType()
{
    int timeType = 24;
    if(timegsettings){
    	QStringList keys = timegsettings->keys();
	if (keys.contains("hoursystem")) {
            timeType = timegsettings->get("hoursystem").toInt();
        }
    }
    return timeType;
}

QString SCConfiguration::getDateType()
{
    QString dateType = "cn";
    if(timegsettings){
        QStringList keys = timegsettings->keys();
        if (keys.contains("date")) {
            dateType = timegsettings->get("date").toString();
        }
    }
    return dateType;
}

int SCConfiguration::getFontSize()
{
    double fontSize = 0;
    if(stygsettings){
        QStringList keys = stygsettings->keys();
    if (keys.contains("systemFontSize")) {
            fontSize = stygsettings->get("systemFontSize").toDouble();
        }
    }

    double defaultFontSize = getDefaultFontSize();
    qDebug()<<"defaultFontSize = "<<defaultFontSize;

    return fontSize - defaultFontSize;
}

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

bool SCConfiguration::getAutoSwitch()
{
    bool ret = false;
    if(udgsettings){
        ret = udgsettings->get("automatic-switching-enabled").toBool();
    }

    return ret;
}

bool SCConfiguration::getIsCustom()
{
    bool ret = false;
    if(ukgsettings){
        ret = (ukgsettings->get("mode").toString() == "default-ukui-custom");
    }

    return ret;
}

bool SCConfiguration::getMessageShowEnable()
{
    bool ret = false;
    if(ukgsettings){
        ret = ukgsettings->get("show-message-enabled").toBool();
    }

    return ret;
}

int SCConfiguration::getMessageNumber()
{
    bool ret = false;
    if(ukgsettings){
        ret = (ukgsettings->get("mode").toString() == "default-ukui-custom");
    }

    return ret;
}

bool SCConfiguration::getCShowRestTime()
{
    bool ret = true;
    if(udgsettings){
         QStringList keys = udgsettings->keys();
         ret = udgsettings->get("show-custom-rest-time").toBool();
    }
    return ret;
}

bool SCConfiguration::getUShowRestTime()
{
    bool ret = true;
    if(udgsettings){
         QStringList keys = udgsettings->keys();
         ret = udgsettings->get("show-ukui-rest-time").toBool();
    }
    return ret;
}

int SCConfiguration::getCycleTime()
{
    int cycleTime = 300;
    if(udgsettings){
         cycleTime = udgsettings->get("cycle-time").toInt();
    }
    return cycleTime;
}

QString SCConfiguration::getBackgroundPath()
{
    QString backgroundPath = "/usr/share/backgrounds";
    if(udgsettings){
         backgroundPath = udgsettings->get("background-path").toString();
    }
    return backgroundPath;
}

bool SCConfiguration::getTextIsCenter()
{
    bool ret = true;
    if(udgsettings){
         ret = udgsettings->get("text-is-center").toBool();
    }
    return ret;
}

QString SCConfiguration::getMyText()
{
    QString myText = "";
    if(udgsettings){
         myText = udgsettings->get("mytext").toString();
    }
    return myText;
}

int SCConfiguration::getBlurNumber()
{
    int blurNum = 50;
    if(stygsettings){
         blurNum = stygsettings->get("menu-transparency").toInt();
         qDebug()<<"????nm"<<blurNum;
    }
    return blurNum;
}

QString SCConfiguration::getcurStyle()
{
    QString curStyle;
    if(stygsettings){
         curStyle = stygsettings->get("style-name").toString();
    }
    return curStyle;
}
