#include <qfile.h>
#include <qmessagebox.h>
#include <qfiledialog.h>

#include "scriptprefs.h"
#include "templates.h"
#include "settings.h"


ScriptPrefs::ScriptPrefs(QWidget *parent, const QString &tempEntry)
	: QDialog(parent)
{
	_tempEntry = tempEntry;
	ui.setupUi(this);

	_prefBtnGroup.addButton(ui.defPrefsButton);
	_prefBtnGroup.addButton(ui.customPrefsButton);
	_prefBtnGroup.setExclusive(true);

	connect(&_prefBtnGroup, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(radioButtonClicked(QAbstractButton *)));
	connect(ui.okButton, SIGNAL(pressed()), this, SLOT(accept()));
	connect(ui.cancelButton, SIGNAL(pressed()), this, SLOT(reject()));
	connect(ui.pyFileButton, SIGNAL(pressed()), this, SLOT(pyFileOpen()));

	ui.defPrefsButton->toggle();

	if(tempEntry.isEmpty()) {
		setWindowTitle(tr("New Template"));

		ui.showSimProgressNetworkCheck->setChecked(AppSettings::showSimulationProgressNetwork());
		ui.showSimProgressOutputCheck->setChecked(AppSettings::showSimulationProgressOutputWindow());
		ui.changeNodeColorsCheck->setChecked(AppSettings::changeNodeColor());
		ui.changeNodeColorsCheck->setChecked(AppSettings::changeNodeColor());
		ui.showCustomScriptCheck->setChecked(AppSettings::showCustomSimulationProgress());
		ui.showSimErrorsCheck->setChecked(AppSettings::showSimulationErrors());
		ui.showSimOutputCheck->setChecked(AppSettings::showSimulationOutput());
		ui.debugModeCheck->setChecked(AppSettings::debugMode());
	}
	else {
		SimulationTemplates temps;
		QString tempName, tempScript;

		if(!temps.get(tempEntry, tempName, tempScript)) {
			QMessageBox::critical(this, windowTitle(),
				tr("Cannot find template entry."));
			return;
		}

		setWindowTitle(tr("%1 Properties").arg(tempName));

		ui.tempNameEdit->setText(tempName);
		ui.pyFilenameEdit->setText(tempScript);
		
		if(temps.customPrefs(tempEntry))
			ui.customPrefsButton->toggle();

		ui.showSimProgressNetworkCheck->setChecked(temps.showSimulationProgressNetwork(tempEntry));
		ui.showSimProgressOutputCheck->setChecked(temps.showSimulationProgressOutputWindow(tempEntry));
		ui.changeNodeColorsCheck->setChecked(temps.changeNodeColor(tempEntry));
		ui.showCustomScriptCheck->setChecked(temps.showCustomSimulationProgress(tempEntry));
		ui.showSimErrorsCheck->setChecked(temps.showSimulationErrors(tempEntry));
		ui.showSimOutputCheck->setChecked(temps.showSimulationOutput(tempEntry));
		ui.debugModeCheck->setChecked(temps.debugMode(tempEntry));
	}

	ui.customGroupBox->setEnabled(ui.customPrefsButton->isChecked());
}

ScriptPrefs::~ScriptPrefs()
{

}

void ScriptPrefs::pyFileOpen()
{
	QFileDialog fileDlg(this, tr("Select Script File"));
	QString dirName;
	QDir dir;
	SimulationTemplates temps;

	dirName = temps.lastTemplateDir();

	dir.setPath(dirName);
	if(!dir.exists()) {
		dir = QDir::home();
	}

	fileDlg.setFileMode(QFileDialog::ExistingFile);
	fileDlg.setDirectory(dir);
	fileDlg.setFilter(tr("All Python files (*.py)"));

	if(fileDlg.exec() == QDialog::Accepted) {
		QStringList files = fileDlg.selectedFiles();
		QString selected;
		if(!files.isEmpty())
			selected = files[0];
		ui.pyFilenameEdit->setText(selected);

		QFileInfo info(selected);
		temps.setLastTemplateDir(info.dir().absolutePath());
	}
}

void ScriptPrefs::radioButtonClicked(QAbstractButton *btn)
{
	QRadioButton *radio = (QRadioButton *) btn;
	
	bool defPrefs = (radio == ui.defPrefsButton);

	ui.customGroupBox->setEnabled(!defPrefs);
}

void ScriptPrefs::accept()
{
	QFile file(ui.pyFilenameEdit->text());

	if(ui.tempNameEdit->text().isEmpty()) {
		QMessageBox::critical(this, windowTitle(),
			tr("Name cannot be empty."));
		ui.tempNameEdit->setFocus(Qt::OtherFocusReason);
		return;
	}
	else if(!file.exists()) {
		QMessageBox::critical(this, tr("Add Template"),
			tr("Python file %1 does not exist.").arg(ui.pyFilenameEdit->text()));
		ui.pyFilenameEdit->setFocus(Qt::OtherFocusReason);
		return;
	}

	SimulationTemplates temps;

	if(_tempEntry.isEmpty()) {
		_tempEntry = temps.add(ui.tempNameEdit->text(), ui.pyFilenameEdit->text());
	}
	else {
		temps.set(_tempEntry, ui.tempNameEdit->text(), ui.pyFilenameEdit->text());
	}

	if(ui.customPrefsButton->isChecked()) {
		temps.setCustomPrefs(_tempEntry, true);
		temps.setShowSimulationProgressNetwork(_tempEntry, ui.showSimProgressNetworkCheck->isChecked());
		temps.setShowSimulationProgressOutputWindow(_tempEntry, ui.showSimProgressOutputCheck->isChecked());
		temps.setChangeNodeColor(_tempEntry, ui.changeNodeColorsCheck->isChecked());
		temps.setShowCustomSimulationProgress(_tempEntry, ui.showCustomScriptCheck->isChecked());
		temps.setShowSimulationErrors(_tempEntry, ui.showSimErrorsCheck->isChecked());
		temps.setShowSimulationOutput(_tempEntry, ui.showSimOutputCheck->isChecked());
		temps.setDebugMode(_tempEntry, ui.debugModeCheck->isChecked());
	}
	else {
		temps.setCustomPrefs(_tempEntry, false);
	}

	QDialog::accept();
}