#include <qstringlist.h>
#include <qdir.h>

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


SimulationTemplates::SimulationTemplates()
{
	_lastEntry = -1;
}

QString SimulationTemplates::add(const QString &name, const QString &scriptFile)
{
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(TEMPLATE_ENTRIES_SECTION);
	
	int i = 0;
	
	// find an empty entry for the new template.
	QStringList groups = settings.childGroups();
	QString tempId = QString("template%1").arg(i);

	while(groups.contains(tempId)) {
		i++;
		tempId = QString("template%1").arg(i);
	}
	
	settings.beginGroup(tempId);
	settings.setValue("name", name);
	settings.setValue("script", QDir::current().relativeFilePath(scriptFile));
	settings.endGroup();
	settings.endGroup();

	settings.sync();

	return tempId;
}

void SimulationTemplates::remove(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();
	
	settings.beginGroup(QString("%1/%2").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry));
	settings.remove("");
	settings.endGroup();

	settings.sync();
}

int SimulationTemplates::count()
{
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(TEMPLATE_ENTRIES_SECTION);

	QStringList tempList = settings.childGroups();

	settings.endGroup();

	return tempList.size();
}

bool SimulationTemplates::get(const QString &tempEntry, QString &tempName, QString &tempScript)
{
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(TEMPLATE_ENTRIES_SECTION);
	settings.beginGroup(tempEntry);

	tempName = settings.value("name").toString();
	tempScript = settings.value("script").toString();

	settings.endGroup();
	settings.endGroup();

	if(tempName.isEmpty())
		return false;
	return true;
}

QString SimulationTemplates::name(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();
	QString ret;

	settings.beginGroup(TEMPLATE_ENTRIES_SECTION);
	settings.beginGroup(tempEntry);

	ret = settings.value("name").toString();

	settings.endGroup();
	settings.endGroup();

	return ret;
}

QString SimulationTemplates::scriptFile(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();
	QString ret;

	settings.beginGroup(TEMPLATE_ENTRIES_SECTION);
	settings.beginGroup(tempEntry);

	ret = settings.value("script").toString();

	settings.endGroup();
	settings.endGroup();

	return ret;
}

bool SimulationTemplates::customPrefs(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg("customPrefs"), false).toBool();
}

void SimulationTemplates::setCustomPrefs(const QString &tempEntry, bool custom)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg("customPrefs"), custom);
	settings.sync();
}

bool SimulationTemplates::showSimulationProgressOutputWindow(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_PROGRESS_OUTPUT_WINDOW_ENTRY), AppSettings::showSimulationProgressOutputWindow()).toBool();
}

void SimulationTemplates::setShowSimulationProgressOutputWindow(const QString &tempEntry, bool show)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_PROGRESS_OUTPUT_WINDOW_ENTRY), show);
	settings.sync();
}

bool SimulationTemplates::showSimulationProgressNetwork(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_PROGRESS_NETWORK_ENTRY), AppSettings::showSimulationProgressNetwork()).toBool();
}

void SimulationTemplates::setShowSimulationProgressNetwork(const QString &tempEntry, bool show)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_PROGRESS_NETWORK_ENTRY), show);
	settings.sync();
}

bool SimulationTemplates::changeNodeColor(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SIMULATION_CHANGE_NODES_COLOR_ENTRY), AppSettings::showSimulationProgressNetwork()).toBool();
}

void SimulationTemplates::setChangeNodeColor(const QString &tempEntry, bool change)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SIMULATION_CHANGE_NODES_COLOR_ENTRY), change);
	settings.sync();
}

bool SimulationTemplates::showCustomSimulationProgress(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SIMULATION_CUSTOM_PROGRESS_ENTRY), AppSettings::showSimulationOutput()).toBool();
}

void SimulationTemplates::setShowCustomSimulationProgress(const QString &tempEntry, bool show)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SIMULATION_CUSTOM_PROGRESS_ENTRY), show);
	settings.sync();
}

bool SimulationTemplates::showSimulationOutput(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_OUTPUT_ENTRY), AppSettings::showSimulationOutput()).toBool();
}

void SimulationTemplates::setShowSimulationOutput(const QString &tempEntry, bool show)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_OUTPUT_ENTRY), show);
	settings.sync();
}

bool SimulationTemplates::showSimulationErrors(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_ERRORS_ENTRY), AppSettings::showSimulationErrors()).toBool();
}

void SimulationTemplates::setShowSimulationErrors(const QString &tempEntry, bool show)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SHOW_SIMULATION_ERRORS_ENTRY), show);
	settings.sync();
}

bool SimulationTemplates::debugMode(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	return settings.value(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SIMULATION_DEBUG_MODE_ENTRY), AppSettings::debugMode()).toBool();
}

void SimulationTemplates::setDebugMode(const QString &tempEntry, bool mode)
{
	QSettings &settings = AppSettings::settings();

	settings.setValue(QString("%1/%2/%3").arg(TEMPLATE_ENTRIES_SECTION).arg(tempEntry).arg(SIMULATION_DEBUG_MODE_ENTRY), mode);
	settings.sync();
}

bool SimulationTemplates::set(const QString &tempEntry, const QString &tempName, const QString &tempScript)
{
	QSettings &settings = AppSettings::settings();
	bool ret = false;

	settings.beginGroup(TEMPLATE_ENTRIES_SECTION);
	settings.beginGroup(tempEntry);

	if(!settings.value("name").toString().isEmpty()) {
		ret = true;
		
		settings.setValue("name", tempName);
		settings.setValue("script", QDir::current().relativeFilePath(tempScript));

		settings.sync();
	}

	settings.endGroup();
	settings.endGroup();

	return ret;
}

bool SimulationTemplates::next(QString &tempEntry, QString &tempName, QString &tempScript)
{
	bool ret;
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(TEMPLATE_ENTRIES_SECTION);

	QStringList tempList = settings.childGroups();

	if(tempList.size() <= ++_lastEntry) {
		_lastEntry = -1;
		ret = false;
	}
	else {
		ret = true;
		
		tempEntry = tempList[_lastEntry];

		settings.beginGroup(tempEntry);

		tempName = settings.value("name").toString();
		tempScript = settings.value("script").toString();

		settings.endGroup();
	}

	settings.endGroup();

	return ret;
}

int SimulationTemplates::fill(QComboBox *combo, const QString &currTempEntry)
{
	QString tempEntry;
	QString tempName;
	QString tempScript;
	int count = 0;

	reset();
	while(next(tempEntry, tempName, tempScript)) {
		if(!tempEntry.isEmpty() && !tempName.isEmpty()) {
			count++;
			combo->addItem(tempName, tempEntry);
			if(tempEntry == currTempEntry)
				combo->setCurrentIndex(count-1);
		}
	}

	return count;
}

QString SimulationTemplates::lastDeployFile(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();
	QString deployFile;

	settings.beginGroup(TEMPLATE_SECTION);
	settings.beginGroup(tempEntry);
	deployFile = settings.value("deploy").toString();
	settings.endGroup();
	settings.endGroup();

	return deployFile;
}

void SimulationTemplates::setLastDeployFile(const QString &tempEntry, const QString &deployFile)
{
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(TEMPLATE_SECTION);
	settings.beginGroup(tempEntry);
	settings.setValue("deploy", QDir::current().relativeFilePath(deployFile));
	settings.endGroup();
	settings.endGroup();
	settings.sync();
}

void SimulationTemplates::setLastTemplateRun(const QString &tempEntry)
{
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(TEMPLATE_SECTION);
	settings.setValue("last-template-run", tempEntry);
	settings.endGroup();
	settings.sync();
}

QString SimulationTemplates::lastTemplateRun()
{
	QSettings &settings = AppSettings::settings();
	QString ret;

	settings.beginGroup(TEMPLATE_SECTION);
	ret = settings.value("last-template-run").toString();
	settings.endGroup();

	return ret;
}

void SimulationTemplates::setLastTemplateDir(const QString &dir)
{
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(TEMPLATE_SECTION);
	settings.setValue(TEMPLATE_LAST_DIR, QDir::current().relativeFilePath(dir));
	settings.endGroup();
	settings.sync();
}

QString SimulationTemplates::lastTemplateDir()
{
	QSettings &settings = AppSettings::settings();
	QString dir;

	settings.beginGroup(TEMPLATE_SECTION);
	dir = settings.value(TEMPLATE_LAST_DIR).toString();
	settings.endGroup();

	return dir;
}

void SimulationTemplates::setLastDeployDir(const QString &dir)
{
	QSettings &settings = AppSettings::settings();

	settings.beginGroup(DEPLOY_SECTION);
	settings.setValue(DEPLOY_LAST_DIR, QDir::current().relativeFilePath(dir));
	settings.endGroup();
	settings.sync();
}

QString SimulationTemplates::lastDeployDir()
{
	QSettings &settings = AppSettings::settings();
	QString dir;

	settings.beginGroup(DEPLOY_SECTION);
	dir = settings.value(DEPLOY_LAST_DIR).toString();
	settings.endGroup();

	return dir;
}

