/*****************************************************************************
 * settingsdialog.cpp - QStarDict, a dictionary application for learning     *
 *                      languages                                            *
 * Copyright (C) 2007 Alexander Rodin                                        *
 *                                                                           *
 * 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 2 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, write to the Free Software Foundation, Inc.,   *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
 *****************************************************************************/

#include "settingsdialog.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QInputDialog>
#include <QRegularExpression>
#include <QSettings>
#include <QKeySequence>
#include <math.h>
#include "dictcore.h"
#include "ipaspeaker.h"
#include "mainwindow.h"
#include "popupwindow.h"
#include "application.h"
#include "speaker.h"
#include "trainer.h"
#include "tray.h"
#include "../qxt/qxtglobalshortcut.h"

namespace
{
int toPercents(double value)
{
    int integralValue = static_cast<int>(value * 100.0);
    if (value * 100.0 - 0.5 > integralValue)
        ++integralValue;
    return integralValue;
}
}

namespace QStarDict {

SettingsDialog::SettingsDialog(QWidget *parent, SettingsDialog::Tab activeTab)
        : QDialog(parent)
{
    setupUi(this);
    Application * const app = Application::instance();

    DictCore *dict = app->dictCore();
    m_oldPlugins = dict->loadedPlugins();
    m_oldDicts = dict->loadedDicts();

    m_pluginsModel = new QStandardItemModel(this);
    m_pluginsModel->setHorizontalHeaderLabels(
            QStringList() << tr("Enabled") << tr("Name"));
    loadPluginsList();
    pluginsTableView->setModel(m_pluginsModel);
    pluginsTableView->verticalHeader()->hide();
    pluginsTableView->setColumnWidth(0, 60);
    pluginsTableView->setColumnWidth(1, 320);

    m_dictsModel = new QStandardItemModel(this);
    m_dictsModel->setHorizontalHeaderLabels(QStringList() << tr("Enabled") << tr("Name") << tr("Plugin"));
    loadDictsList();
    dictsTableView->setModel(m_dictsModel);
    dictsTableView->verticalHeader()->hide();
    dictsTableView->setColumnWidth(0, 60);
    dictsTableView->setColumnWidth(1, 200);
    dictsTableView->setColumnWidth(2, 120);

    // Load global settings
    systemTrayBox->setChecked(app->tray()->isVisible());
    instantSearchBox->setChecked(app->mainWindow()->isInstantSearch());
#ifdef Q_OS_LINUX
	QFile desktop(QDir::homePath() + "/.config/autostart/qstardict.desktop");
	if (desktop.open(QIODevice::ReadOnly) && QString(desktop.readAll())
		.contains(QRegularExpression("\\bhidden\\s*=\\s*false", QRegularExpression::CaseInsensitiveOption))) {
		autostartBox->setChecked(true);
	}
#elif defined(Q_OS_WIN)
	QSettings reg("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\"
				  "CurrentVersion\\Run", QSettings::NativeFormat);
	autostartBox->setChecked(
			reg.contains(QCoreApplication::applicationName()));
#else
	autostartBox->setVisible(false);
#endif
    speechCmdEdit->setText(app->speaker()->speechCmd());
    ipaPronounceBox->setChecked(app->mainWindow()->showIpaPronouncers());
    espeakCmdEdit->setText(app->ipaSpeaker()->speechCmd());

    // Load popup shortcut settings
    shortcutPopupEdit->setText(app->popupShortcut()->shortcut().toString());
    shortcutPopupBox->setChecked(app->popupShortcut()->isEnabled());

    // Load popup window settings
    PopupWindow *popup = app->popupWindow();
    useScanBox->setChecked(popup->isScan());
    if (popup->modifierKey())
    {
        useScanModifierBox->setChecked(true);
        QString modifierName;
        switch (popup->modifierKey())
        {
        case Qt::ShiftModifier:
            modifierName = "Shift";
            break;
        case Qt::ControlModifier:
            modifierName = "Control";
            break;
        case Qt::AltModifier:
            modifierName = "Alt";
            break;
        case Qt::MetaModifier:
            modifierName = "Win";
            break;
        }
        modifierKeyBox->setCurrentIndex(modifierKeyBox->findText(modifierName));
    }
    showIfNotFoundBox->setChecked(popup->showIfNotFound());
    popupOpacitySpin->setValue(toPercents(popup->windowOpacity()));
    timeoutBeforeHideSpin->setValue(popup->timeoutBeforeHide() / 1000.0);
    popupDefaultWidthSpin->setValue(popup->defaultSize().width());
    popupDefaultHeightSpin->setValue(popup->defaultSize().height());
    pronounceWordBox->setChecked(popup->pronounceWord());

    // Load the trainer settings
    Trainer *trainer = app->trainer();
    wordsPerRoundBox->setValue(trainer->wordsPerRound());
    wordsPerDayBox->setValue(trainer->wordsPerDay());

    // Load translations CSS
    QHash<QString, QString> cssAliases;
    cssAliases["body"] = tr("All translation");
    cssAliases["font.dict_name"] = tr("Dictionary name");
    cssAliases["font.title"] = tr("Title");
    cssAliases["font.explanation"] = tr("Explanation");
    cssAliases["font.abbreviature"] = tr("Abbreviation");
    cssAliases["font.example"] = tr("Example");
    cssAliases["font.transcription"] = tr("Transcription");
    appearanceCSSEdit->setElementsAliases(cssAliases);
    appearanceCSSEdit->setCSS(Application::instance()->mainWindow()->defaultStyleSheet());

    connect(m_pluginsModel, &QStandardItemModel::itemChanged,
            this, &SettingsDialog::pluginsItemChanged);

    tabWidget->setCurrentIndex(static_cast<int>(activeTab));
}

void SettingsDialog::accept()
{
    Application * const app = Application::instance();

    // Save dicts and plugins settings
    DictCore *dict = app->dictCore();
    QStringList loadedPlugins;
    int rowCount = m_pluginsModel->rowCount();
    for (int i = 0; i < rowCount; ++i)
        if (m_pluginsModel->item(i, 0)->checkState() == Qt::Checked)
            loadedPlugins << m_pluginsModel->item(i, 1)->text();
    dict->setLoadedPlugins(loadedPlugins);

    QList<DictCore::Dictionary> loadedDicts;
    rowCount = m_dictsModel->rowCount();
    for (int i = 0; i < rowCount; ++i)
        if (m_dictsModel->item(i, 0)->checkState() == Qt::Checked)
            loadedDicts << DictCore::Dictionary(m_dictsModel->item(i, 2)->text(), m_dictsModel->item(i, 1)->text());
    dict->setLoadedDicts(loadedDicts);

    // Save global settings
    app->tray()->setVisible(systemTrayBox->isChecked());
    app->mainWindow()->setInstantSearch(instantSearchBox->isChecked());
#ifdef Q_OS_LINUX
	QDir home = QDir::home();
	if (!home.exists(".config/autostart")) {
		home.mkpath(".config/autostart");
	}
	QFile desktopFile(QSTARDICT_INSTALL_PREFIX "/share/applications/qstardict.desktop");
	if (desktopFile.open(QIODevice::ReadOnly)) {
		QByteArray contents = desktopFile.readAll();
		QFile f(home.absolutePath() +
				"/.config/autostart/qstardict.desktop");

		if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
			f.write(contents.trimmed());
			f.write(QString("\nHidden=%1").arg(autostartBox->isChecked()?
											   "false\n":"true\n").toUtf8());
		}
	}
#elif defined(Q_OS_WIN)
	QSettings reg("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\"
				  "CurrentVersion\\Run", QSettings::NativeFormat);
	if(autostartBox->isChecked())
		reg.setValue(QCoreApplication::applicationName(), '"' +
					 QDir::toNativeSeparators(QCoreApplication::
											  applicationFilePath()) + '"');
	else
		reg.remove(QCoreApplication::applicationName());
#endif
    app->speaker()->setSpeechCmd(speechCmdEdit->text());
    app->mainWindow()->setShowIpaPronouncers(ipaPronounceBox->isChecked());
    app->popupWindow()->setShowIpaPronouncers(ipaPronounceBox->isChecked());
    app->ipaSpeaker()->setSpeechCmd(espeakCmdEdit->text());

    // Save popup shortcut settings
    app->popupShortcut()->setShortcut(QKeySequence(shortcutPopupEdit->text()));
    app->popupShortcut()->setEnabled(shortcutPopupBox->isChecked());

    // Save popup window settings
    PopupWindow *popup = app->popupWindow();
    popup->setScan(useScanBox->isChecked());
    int modifierKey = 0;
    if (useScanModifierBox->isChecked())
    {
        if (modifierKeyBox->currentText() == "Shift")
            modifierKey = Qt::ShiftModifier;
        else if (modifierKeyBox->currentText() == "Control")
            modifierKey = Qt::ControlModifier;
        else if (modifierKeyBox->currentText() == "Alt")
            modifierKey = Qt::AltModifier;
        else if (modifierKeyBox->currentText() == "Win")
            modifierKey = Qt::MetaModifier;
    }
    popup->setShowIfNotFound(showIfNotFoundBox->isChecked());
    popup->setModifierKey(modifierKey);
    popup->setWindowOpacity(popupOpacitySpin->value() / 100.0);
    popup->setTimeoutBeforeHide(static_cast<int>(timeoutBeforeHideSpin->value() * 1000.0));
    popup->setDefaultSize(QSize(popupDefaultWidthSpin->value(), popupDefaultHeightSpin->value()));
    popup->setPronounceWord(pronounceWordBox->isChecked());

    // Save the trainer settings
    Trainer *trainer = app->trainer();
    trainer->setWordsPerRound(wordsPerRoundBox->value());
    trainer->setWordsPerDay(wordsPerDayBox->value());

    // Save translations CSS
    app->mainWindow()->setDefaultStyleSheet(appearanceCSSEdit->css());
    app->popupWindow()->setDefaultStyleSheet(appearanceCSSEdit->css());

    if (! app->tray()->isVisible())
        app->mainWindow()->show();

    app->mainWindow()->reload();

    app->dictCore()->saveSettings();
    app->mainWindow()->saveSettings();
    app->tray()->saveSettings();
    app->popupWindow()->saveSettings();
    app->trainer()->saveSettings();

    QDialog::accept();
}

void SettingsDialog::reject()
{
    DictCore *dict = Application::instance()->dictCore();
    dict->setLoadedPlugins(m_oldPlugins);
    dict->setLoadedDicts(m_oldDicts);
    QDialog::reject();
}

void SettingsDialog::loadDictsList()
{
    int i;
    QList<DictCore::Dictionary> loadedDicts = Application::instance()->dictCore()->loadedDicts();
    m_dictsModel->setRowCount(0);
    for (i = 0; i < loadedDicts.size(); ++i)
    {
        QStandardItem *item = new QStandardItem();
        item->setCheckable(true);
        item->setCheckState(Qt::Checked);
        m_dictsModel->setItem(i, 0, item);
        m_dictsModel->setItem(i, 1, new QStandardItem(loadedDicts[i].name()));
        m_dictsModel->setItem(i, 2, new QStandardItem(loadedDicts[i].plugin()));
    }
    QList<DictCore::Dictionary> dicts = Application::instance()->dictCore()->availableDicts();
    for (QList<DictCore::Dictionary>::const_iterator iter = dicts.begin(); iter != dicts.end(); ++iter)
    {
        if (! loadedDicts.contains(*iter))
        {
            QStandardItem *item = new QStandardItem();
            item->setCheckable(true);
            item->setCheckState(Qt::Unchecked);
            m_dictsModel->setItem(i, 0, item);
            m_dictsModel->setItem(i, 1, new QStandardItem(iter->name()));
            m_dictsModel->setItem(i, 2, new QStandardItem(iter->plugin()));
            ++i;
        }
    }
}

void SettingsDialog::loadPluginsList()
{
    m_pluginsModel->setRowCount(0);
    DictCore *dict = Application::instance()->dictCore();
    QStringList plugins = dict->availablePlugins();
    QStringList loaded = dict->loadedPlugins();
    for (int i = 0; i < plugins.size(); ++i)
    {
        QStandardItem *item = new QStandardItem();
        item->setCheckable(true);
        item->setCheckState(loaded.contains(plugins[i]) ? Qt::Checked : Qt::Unchecked);
        m_pluginsModel->setItem(i, 0, item);
        m_pluginsModel->setItem(i, 1, new QStandardItem(plugins[i]));
    }
}

void SettingsDialog::on_dictsMoveUpButton_clicked()
{
    int currentRow = dictsTableView->currentIndex().row();
    if (currentRow > 0)
    {
        m_dictsModel->insertRow(currentRow - 1, m_dictsModel->takeRow(currentRow));
        dictsTableView->selectRow(currentRow - 1);
    }
}

void SettingsDialog::on_dictsMoveDownButton_clicked()
{
    int currentRow = dictsTableView->currentIndex().row();
    if (currentRow < m_dictsModel->rowCount() - 1)
    {
        m_dictsModel->insertRow(currentRow + 1, m_dictsModel->takeRow(currentRow));
        dictsTableView->selectRow(currentRow + 1);
    }
}

void SettingsDialog::on_dictsShowInfoButton_clicked()
{
    int currentRow = dictsTableView->currentIndex().row();
    if (currentRow == -1)
        return;
    QString dict = m_dictsModel->item(currentRow, 1)->text();
    QString plugin = m_dictsModel->item(currentRow, 2)->text();
    DictPlugin::DictInfo info = Application::instance()->dictCore()->plugin(plugin)->dictInfo(dict);
    QMessageBox::information(this,
            tr("Information about dictionary \"%1\"").arg(dict),
            tr("<b>Name:</b> %1<br>").arg(dict) +
            tr("<b>Plugin:</b> %1<br>").arg(plugin) +
            tr("<b>Author:</b> %1<br>").arg(info.author()) +
            tr("<b>Words count:</b> %1<br>").arg((info.wordsCount() == -1) ? tr("unknown") : QString::number(info.wordsCount())) +
            tr("<b>Description:</b> %1").arg(info.description()));
}

void SettingsDialog::on_pluginsShowInfoButton_clicked()
{
    int currentRow = pluginsTableView->currentIndex().row();
    if (currentRow == -1)
        return;
    DictPlugin *plugin = Application::instance()->dictCore()->plugin(m_pluginsModel->item(currentRow, 1)->text());
    if (! plugin)
        return;
    QMessageBox::information(this,
            tr("Information about %1 plugin").arg(plugin->name()),
            tr("<b>Name:</b> %1<br>").arg(plugin->name()) +
            tr("<b>Version:</b> %1<br>").arg(plugin->version()) +
            tr("<b>Authors:</b> %1<br>").arg(plugin->authors().replaceInStrings("<", "&lt;").replaceInStrings(">", "&gt;").join(tr("<br>"))) +
            tr("<b>Can search similar words:</b> %1<br>").arg(plugin->features().testFlag(DictPlugin::SearchSimilar) ? tr("yes") : tr("no")) +
            tr("<b>Description:</b> %1").arg(plugin->description()));
}

void SettingsDialog::on_pluginsConfigureButton_clicked()
{
    int currentRow = pluginsTableView->currentIndex().row();
    if (currentRow == -1)
        return;
    DictCore *dict = Application::instance()->dictCore();
    DictPlugin *plugin = dict->plugin(m_pluginsModel->item(currentRow, 1)->text());
    if (plugin && plugin->execSettingsDialog(this) == QDialog::Accepted)
    {
        dict->reloadDicts();
        loadDictsList();
    }
}

void SettingsDialog::pluginsItemChanged(QStandardItem *item)
{
    if (item->isCheckable())
    {
        DictCore *dict = Application::instance()->dictCore();
        QStringList loadedPlugins;
        int rowCount = m_pluginsModel->rowCount();
        for (int i = 0; i < rowCount; ++i)
            if (m_pluginsModel->item(i, 0)->checkState() == Qt::Checked)
                loadedPlugins << m_pluginsModel->item(i, 1)->text();
        dict->setLoadedPlugins(loadedPlugins);
        dict->reloadDicts();
        loadDictsList();
    }
}

}

// vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc

