#include "LoadSetupDialog.h"
#include "../misc/fixc11.h"

#include <QLabel>
#include <QPushButton>
#include <QListView>
#include <QFile>
#include <QDir>
#include <QStringListModel>

#include <iostream>

#include <Indoor/Assertions.h>
#include "../Settings.h"

LoadSetupDialog::LoadSetupDialog() {

	// the folder all map-setups reside within
	const std::string base = Settings::Data::getMapDir();
	QDir mapFolder(QString(base.c_str()));

	// sanity check. folder must exist
	Assert::isTrue(mapFolder.exists(), "folder not found: " + base);

	// get all subfolders (each subfolder descibres one setup), skip the first two folders: "." and ".."
	QStringList subfolders = mapFolder.entryList(QDir::Dirs);
	subfolders.removeFirst();
	subfolders.removeFirst();
//	for (int i = 2; i < subfolders.size(); ++i) {
//		const QString subfolder = subfolders[i];
//		std::cout << subfolder.toStdString() << std::endl;
//	}

	int w = 350;
	int h = 350;

	const QFont font("Arial", 20);

	QListView* lst = new QListView(this);
	lst->setGeometry(5,5,w-5-5,h-5-5);
	lst->setFont(font);

	QStringListModel* mdl = new QStringListModel(subfolders);
	lst->setModel(mdl);

	// list item selected
	connect(lst, &QListView::clicked, [this, base, subfolders] (const QModelIndex& idx) {
		const int i = idx.row();
		selDir = base + subfolders[i].toStdString();
		this->close();
	});

//	QPushButton* btnOK = new QPushButton(this);
//	btnOK->setText("OK");
//	btnOK->setGeometry(5,h-32-5,w-5-5,32);

//	// OK button clicked
//	btnOK->connect(btnOK, &QPushButton::clicked, [&] () {
//		this->close();
//	});

	this->resize(w,h);

}

QDir LoadSetupDialog::pickSetupFolder() {


	LoadSetupDialog dlg;
	dlg.exec();
	return QDir(QString(dlg.selDir.c_str()));

}
