/*
 * Squawk messenger.
 * Copyright (C) 2019 Yury Gubich <blue@macaw.me>
 *
 * 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 "pageappearance.h"
#include "ui_pageappearance.h"

#include <QDebug>
#include <QStandardPaths>
#include <QDir>

static const QStringList filters = {"*.colors"};

PageAppearance::PageAppearance(QWidget* parent):
    QWidget(parent),
    m_ui(new Ui::PageAppearance()),
    styles(),
    themes()
{
    m_ui->setupUi(this);

    m_ui->styleInput->addItem(tr("System"));
    styles.push_back("system");
    QStringList systemStyles = QStyleFactory::keys();
    for (const QString& key : systemStyles) {
        m_ui->styleInput->addItem(key);
        styles.push_back(key);
    }

    QSettings settings;
    QVariant vs = settings.value("style");
    if (vs.isValid()) {
        m_ui->styleInput->setCurrentText(vs.toString());
    } else {
        m_ui->styleInput->setCurrentText(tr("System"));
    }

    connect(m_ui->styleInput, qOverload<int>(&QComboBox::currentIndexChanged), this, &PageAppearance::onStyleChanged);

    if (Shared::Global::supported("colorSchemeTools")) {
        themes.push_back("system");
        m_ui->themeInput->addItem(tr("System"));
        const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "color-schemes", QStandardPaths::LocateDirectory);
        QStringList schemeFiles;
        QString currentTheme(tr("System"));
        QString requestedTheme("");
        QVariant vtheme = settings.value("theme");
        if (vtheme.isValid()) {
            requestedTheme = vtheme.toString();
        }
        for (const QString &dir : dirs) {
            const QStringList fileNames = QDir(dir).entryList(filters);
            for (const QString &file : fileNames) {
                QString path = dir + QDir::separator() + file;
                themes.push_back(path);
                QString themeName = Shared::Global::getColorSchemeName(path);
                m_ui->themeInput->addItem(Shared::Global::createThemePreview(path), themeName);

                if (path == requestedTheme) {
                    currentTheme = themeName;
                }
            }
        }

        m_ui->themeInput->setCurrentText(currentTheme);

        connect(m_ui->themeInput, qOverload<int>(&QComboBox::currentIndexChanged), this, &PageAppearance::onThemeChanged);
    } else {
         m_ui->themeInput->setEnabled(false);
    }
}

PageAppearance::~PageAppearance()
{
}

void PageAppearance::onThemeChanged(int index)
{
    if (index >= 0) {
        emit variableModified("theme", themes[index]);
    }
}

void PageAppearance::onStyleChanged(int index)
{
    if (index >= 0) {
        emit variableModified("style", styles[index]);
    }
}