/*******************************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** 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 <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include "settings.h"
#include <QStandardPaths>
#include <fstream>
#include <cstdio>
#include <QDir>
#include <QFile>
#include <QFileInfo>

Settings::Settings(const QString &configFile, const QString &defaultsFile)
{
    values = parse(configFile);
    defaults = parse(defaultsFile);

    m_configurationPath = configFile;
    m_defaultsPath = defaultsFile;

    homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    settingsLocation = QFileInfo(configFile).dir().absolutePath();
    cacheLocation = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);

#ifdef QT_DEBUG
    qDebug(">> Configuration");
    qDebug("- values: [%s]", qUtf8Printable(configFile));
    qDebug("- defaults: [%s]", qUtf8Printable(defaultsFile));
    qDebug("- $home     | '%s'", qUtf8Printable(homeLocation));
    qDebug("- $settings | '%s'", qUtf8Printable(settingsLocation));
    qDebug("- $cache    | '%s'", qUtf8Printable(cacheLocation));
#endif
}

Settings::~Settings()
{
}

QString Settings::configurationPath() const
{
    return m_configurationPath;
}

QString Settings::defaultsPath() const
{
    return m_defaultsPath;
}

bool Settings::isEmpty() const
{
    return values.empty();
}

bool Settings::contains(const QString &key)
{
    const toml::Value *x = values.find(key.toStdString());
    if(x) {
        return true;
    } else {
        return false;
    }
}

QVariant Settings::value(const QString &key) const
{
    const toml::Value *cValue = values.find(key.toStdString());
    const toml::Value *dValue = defaults.find(key.toStdString());

    QVariant r;

    if(values.has(key.toStdString())) {
        r = valueToVariant(cValue);
    } else {
        if(defaults.has(key.toStdString())) {
            r = valueToVariant(dValue);
        }
    }

    // check if key is a path, in which case replace '~' with the home location
    if(key.endsWith(QLatin1String("path"), Qt::CaseInsensitive)) {
        QString value = r.toString();
        while(value.contains('$')) {
            value.replace("$settings", settingsLocation);
            value.replace("$cache", cacheLocation);
            value.replace("$home", homeLocation);
        }
        r = QVariant(value);
    }

    return r;
}

toml::Value Settings::parse(const QString &filename)
{
    toml::Value r;

    if(!filename.isEmpty()) {
        QFile file(filename);
        if(file.open(QIODevice::ReadOnly)) {
            std::stringstream d(file.readAll().toStdString());
            file.close();

            toml::ParseResult result = toml::parse(d);
            if(!result.valid()) {
                qWarning("Invalid configuration: %s", result.errorReason.c_str());
            }
            r = result.value;

        } else {
            qWarning("Cannot open configuration: %s", qUtf8Printable(filename));
        }
    }

    return r;
}

QVariant Settings::fromList(const toml::Value *list) const
{
    QStringList l;

    for(const toml::Value &v : list->as<toml::Array>()) {
        // TODO check value type
        l.append(QString::fromStdString(v.as<std::string>()));
    }

    return QVariant(l);
}

QVariant Settings::valueToVariant(const toml::Value *value) const
{
    QVariant r;
    switch (value->type()) {
    case toml::Value::NULL_TYPE:
        break;

    case toml::Value::BOOL_TYPE:
        r = QVariant(value->as<bool>());
        break;

    case toml::Value::INT_TYPE:
        r = QVariant(value->as<int>());
        break;

    case toml::Value::DOUBLE_TYPE:
        r = QVariant(value->as<double>());
        break;

    case toml::Value::STRING_TYPE:
        r = QVariant(QString::fromStdString(value->as<std::string>()));
        break;

    case toml::Value::ARRAY_TYPE:
        r = fromList(value);
        break;

    default:
        qWarning("Unhandled type in configuration");
        break;
    }

    return r;
}