#include "piqt_connection_edit.h"

#include <QApplication>
#include <QFileDialog>
#include <piintrospection_server.h>
#include <qad_types.h>
#include <qpiconfig.h>

int main(int argc, char * argv[]) {
	PIINTROSPECTION_START(piconnedit)
	QApplication a(argc, argv);
	enableHighDPI();
	ConnectionEdit w;
	if (a.arguments().size() > 1) {
		QPIConfig cfg(a.arguments()[1]);
		QByteArray model = cfg.getValue("connectionmodel").toByteArray();
		if (!model.isEmpty()) w.setModel(model);
	} else
		w.recreateConnection();
	if (w.exec() == QDialog::Accepted) {
		QString c = QFileDialog::getSaveFileName(&w, "Save config to file", a.applicationDirPath(), "*.conf");
		if (!c.isEmpty()) {
			QFile f(c);
			if (f.open(QIODevice::WriteOnly)) {
				QTextStream ts(&f);
				ts << w.configuration();
				ts << "connectionmodel = " << QByteArray2QString(w.model()) << "\n";
				f.close();
			}
		}
	}
	return 0;
}
