/*
 * Copyright (C) 2014-2015 CZ.NIC
 *
 * 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/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */


#include "dlg_preferences.h"
#include "ui_dlg_preferences.h"


DlgPreferences::DlgPreferences(QWidget * parent)
    : QDialog(parent)
{
	setupUi(this);
	initPrefDialog();
}

void DlgPreferences::initPrefDialog(void)
{
	this->download_at_start->setChecked(globPref.download_at_start);
	this->auto_download_whole_messages->setChecked(globPref.auto_download_whole_messages);
	this->download_on_background->setChecked(globPref.download_on_background);
	this->timerSpinBox->setValue(globPref.timer_value);
//	this->send_stats_with_version_checks->setChecked(globPref.send_stats_with_version_checks);
	this->check_new_versions->setChecked(globPref.check_new_versions);
	this->store_messages_on_disk->setChecked(globPref.store_messages_on_disk);
	this->store_additional_data_on_disk->setChecked(globPref.store_additional_data_on_disk);
	this->check_crl->setChecked(globPref.check_crl);
	this->language->setCurrentIndex(getLangugeIndex(globPref.language));
	/* TODO - this choice must be disabled */
//	this->send_stats_with_version_checks->setEnabled(this->check_new_versions->isChecked());
	this->timerLabelPre->setEnabled(this->download_on_background->isChecked());
	this->timerLabelPost->setEnabled(this->download_on_background->isChecked());
	this->timerSpinBox->setEnabled(this->download_on_background->isChecked());

	connect(this->check_new_versions, SIGNAL(stateChanged(int)),
	    this, SLOT(setActiveCheckBox(int)));
	connect(this->download_on_background, SIGNAL(stateChanged(int)),
	    this, SLOT(setActiveTimerSetup(int)));

	connect(this->prefButtonBox, SIGNAL(accepted()), this, SLOT(saveChanges(void)));

	if (GlobPreferences::SELECT_NEWEST == globPref.after_start_select) {
		this->after_start_select_1->setChecked(true);
		this->after_start_select_2->setChecked(false);
		this->after_start_select_3->setChecked(false);
	} else if (GlobPreferences::SELECT_LAST_VISITED == globPref.after_start_select) {
		this->after_start_select_1->setChecked(false);
		this->after_start_select_2->setChecked(true);
		this->after_start_select_3->setChecked(false);
	} else if (GlobPreferences::SELECT_NOTHING == globPref.after_start_select) {
		this->after_start_select_1->setChecked(false);
		this->after_start_select_2->setChecked(false);
		this->after_start_select_3->setChecked(true);
	} else {
		Q_ASSERT(0);
	}

	if (GlobPreferences::DOWNLOAD_DATE == globPref.certificate_validation_date) {
		this->certificate_validation_date_1->setChecked(true);
		this->certificate_validation_date_2->setChecked(false);
	} else if (GlobPreferences::CURRENT_DATE == globPref.certificate_validation_date) {
		this->certificate_validation_date_1->setChecked(false);
		this->certificate_validation_date_2->setChecked(true);
	} else {
		Q_ASSERT(0);
	}
}

int DlgPreferences::getLangugeIndex(const QString &language)
{
	if (langCs == language) {
		return 1;
	} else if (langEn == language) {
		return 2;
	} else {
		return 0;
	}
}


const QString & DlgPreferences::getIndexFromLanguge(int index)
{
	switch (index) {
	case 1:
		return langCs;
		break;
	case 2:
		return langEn;
		break;
	default:
		return langSystem;
		break;
	}
}

const QString DlgPreferences::langCs("cs");
const QString DlgPreferences::langEn("en");
const QString DlgPreferences::langSystem("system");


void DlgPreferences::setActiveTimerSetup(int state)
{
	this->timerLabelPre->setEnabled(Qt::Checked == state);
	this->timerLabelPost->setEnabled(Qt::Checked == state);
	this->timerSpinBox->setEnabled(Qt::Checked == state);
}


void DlgPreferences::setActiveCheckBox(int state)
{
	(void) state;
	/* TODO - this choice must be disabled */
//	this->send_stats_with_version_checks->setEnabled(Qt::Checked == state);
}


void DlgPreferences::saveChanges(void) const
{
	globPref.auto_download_whole_messages =
	    this->auto_download_whole_messages->isChecked();
	globPref.download_on_background =
	    this->download_on_background->isChecked();
	globPref.download_at_start =
	    this->download_at_start->isChecked();
	globPref.store_messages_on_disk =
	    this->store_messages_on_disk->isChecked();
	globPref.store_additional_data_on_disk =
	    this->store_additional_data_on_disk->isChecked();
	globPref.certificate_validation_date =
	    this->certificate_validation_date_1->isChecked() ?
	        GlobPreferences::DOWNLOAD_DATE :
	        GlobPreferences::CURRENT_DATE;
	globPref.check_crl = this->check_crl->isChecked();
	globPref.check_new_versions = this->check_new_versions->isChecked();
	globPref.send_stats_with_version_checks =
	    this->send_stats_with_version_checks->isChecked();
	globPref.timer_value = this->timerSpinBox->value();
	globPref.language =
	    getIndexFromLanguge(this->language->currentIndex());
	if (this->after_start_select_1->isChecked()) {
		globPref.after_start_select = GlobPreferences::SELECT_NEWEST;
	} else if (this->after_start_select_2->isChecked()) {
		globPref.after_start_select =
		    GlobPreferences::SELECT_LAST_VISITED;
	} else {
		globPref.after_start_select = GlobPreferences::SELECT_NOTHING;
	}
}
