#include <QtGui>

#include "preferences.h"
#include "settings.h"

Preferences::Preferences(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);

	ui.listWidget->setIconSize(QSize(48, 48));

	ui.listWidget->setCurrentRow(0);
	
	// more than zero
	QDoubleValidator *validator = new QDoubleValidator(ui.defaultCPUPowerEdit);
	validator->setBottom(1);
	ui.defaultCPUPowerEdit->setValidator(validator);

	validator = new QDoubleValidator(ui.defaultBandwidthEdit);
	validator->setBottom(1);
	ui.defaultBandwidthEdit->setValidator(validator);

	validator = new QDoubleValidator(ui.defaultLatencyEdit);
	validator->setBottom(0);
	ui.defaultLatencyEdit->setValidator(validator);

	ui.openLastCheck->setCheckState(AppSettings::openLastNetwork() ? Qt::Checked : Qt::Unchecked);
	ui.defaultCPUPowerEdit->setText(QString("%1").arg(AppSettings::defaultCPUPower()));
	ui.defaultBandwidthEdit->setText(QString("%1").arg(AppSettings::defaultBandwidth()));
	ui.defaultLatencyEdit->setText(QString("%1").arg(AppSettings::defaultLatency()));

	ui.openOutputCheck->setCheckState(AppSettings::openOutputWindow() ? Qt::Checked : Qt::Unchecked);
	ui.showSimProgressOutputCheck->setCheckState(AppSettings::showSimulationProgressOutputWindow() ? Qt::Checked : Qt::Unchecked);
	ui.showSimProgressNetworkCheck->setCheckState(AppSettings::showSimulationProgressNetwork() ? Qt::Checked : Qt::Unchecked);
	ui.changeNodeColorsCheck->setCheckState(AppSettings::changeNodeColor() ? Qt::Checked : Qt::Unchecked);
	ui.showCustomScriptCheck->setCheckState(AppSettings::showCustomSimulationProgress() ? Qt::Checked : Qt::Unchecked);
	ui.showSimErrorsCheck->setCheckState(AppSettings::showSimulationErrors() ? Qt::Checked : Qt::Unchecked);
	ui.showSimOutputCheck->setCheckState(AppSettings::showSimulationOutput() ? Qt::Checked : Qt::Unchecked);
	ui.debugModeCheck->setCheckState(AppSettings::debugMode() ? Qt::Checked : Qt::Unchecked);

    connect(ui.listWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
            this, SLOT(changePage(QListWidgetItem *, QListWidgetItem*)));
	//connect(this, SIGNAL(accepted()), this, SLOT)
}

void Preferences::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
	if (!current)
		current = previous;

	ui.pagesWidget->setCurrentIndex(ui.listWidget->row(current));
}

void Preferences::accept()
{
	if(!ui.defaultCPUPowerEdit->hasAcceptableInput()) {
		QMessageBox::critical(this, tr("Preferences"),
							  tr("Invalid Default CPU Power."));
		return;
	}
	if(!ui.defaultBandwidthEdit->hasAcceptableInput()) {
		QMessageBox::critical(this, tr("Preferences"),
							  tr("Invalid Default Bandwidth."));
		return;
	}
	if(!ui.defaultLatencyEdit->hasAcceptableInput()) {
		QMessageBox::critical(this, tr("Preferences"),
							  tr("Invalid Default Latency."));
		return;
	}
	
	AppSettings::setOpenLastNetwork(ui.openLastCheck->checkState() == Qt::Checked);
	AppSettings::setDefaultCPUPower(ui.defaultCPUPowerEdit->text().toDouble());
	AppSettings::setDefaultBandwidth(ui.defaultBandwidthEdit->text().toDouble());
	AppSettings::setDefaultLatency(ui.defaultLatencyEdit->text().toDouble());
	AppSettings::setOpenOutputWindow(ui.openOutputCheck->checkState() == Qt::Checked);
	AppSettings::setShowSimulationProgressOutputWindow(ui.showSimProgressOutputCheck->checkState() == Qt::Checked);
	AppSettings::setShowSimulationProgressNetwork(ui.showSimProgressNetworkCheck->checkState() == Qt::Checked);
	AppSettings::setChangeNodeColor(ui.changeNodeColorsCheck->checkState() == Qt::Checked);
	AppSettings::setShowCustomSimulationProgress(ui.showCustomScriptCheck->checkState() == Qt::Checked);
	AppSettings::setShowSimulationOutput(ui.showSimOutputCheck->checkState() == Qt::Checked);
	AppSettings::setShowSimulationErrors(ui.showSimErrorsCheck->checkState() == Qt::Checked);
	AppSettings::setDebugMode(ui.debugModeCheck->checkState() == Qt::Checked);

	QDialog::accept();
}