#include "settings_pane.hh"

#include <QCheckBox>
#include <QDoubleSpinBox>
#include <QFormLayout>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QTabWidget>


SettingsPane::SettingsPane(QWidget *parent)
	:QWidget(parent) {
	QDoubleSpinBox *sphere_radius_input = new QDoubleSpinBox();
	sphere_radius_input->setSingleStep(.1);
	sphere_radius_input->setValue(sphere_radius);
	QDoubleSpinBox *speed_limit_input = new QDoubleSpinBox();
	speed_limit_input->setSingleStep(.1);
	speed_limit_input->setValue(speed_limit);
	QCheckBox *show_trajectories = new QCheckBox();
	QCheckBox *show_guides = new QCheckBox();
	collisions = new QListWidget();
	speeding_violations = new QListWidget();

	connect(sphere_radius_input, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
			[&](double radius) { sphere_radius = radius; });
	connect(sphere_radius_input, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
			this, &SettingsPane::sphereRadiusChanged);
	connect(speed_limit_input, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
			[&](double speed) { speed_limit = speed; });
	connect(speed_limit_input, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
			this, &SettingsPane::speedLimitChanged);
	connect(show_trajectories, &QCheckBox::stateChanged,
			this, &SettingsPane::toggledTrajectories);
	connect(show_guides, &QCheckBox::stateChanged,
			this, &SettingsPane::toggledGuides);
	connect(collisions, &QListWidget::itemClicked,
			[&](QListWidgetItem *item) { emit bookmarkClicked(((BookmarkItem *) item)->getFrame()); });
	connect(speeding_violations, &QListWidget::itemClicked,
			[&](QListWidgetItem *item) { emit bookmarkClicked(((BookmarkItem *) item)->getFrame()); });

	QTabWidget *tabs = new QTabWidget();

	QFormLayout *layout = new QFormLayout;
	layout->addRow("Taille de la sphère de collision", sphere_radius_input);
	layout->addRow("Limite de vitesse", speed_limit_input);
	layout->addRow("Afficher les trajectoires", show_trajectories);
	layout->addRow("Afficher les guides", show_guides);

	tabs->addTab(collisions, "Collisions");
	tabs->addTab(speeding_violations, "Excès de vitesse");
	layout->addRow(tabs);

	setLayout(layout);
}


double SettingsPane::getSphereRadius() const {
	return sphere_radius;
}

double SettingsPane::getSpeedLimit() const {
	return speed_limit;
}


void SettingsPane::addCollision(int idA, int idB, int frame) {
	BookmarkItem *item = new BookmarkItem("Frame " + QString::number(frame) + ": #"
										  + QString::number(idA) + " et #" + QString::number(idB),
										  frame);
	item->setFlags(Qt::ItemIsEnabled | Qt::ItemNeverHasChildren);
	collisions->addItem(item);
}


void SettingsPane::addSpeedingViolation(int id, double speed, int frame) {
	BookmarkItem *item = new BookmarkItem("Frame " + QString::number(frame) + ": #"
										  + QString::number(id) + " (" + QString::number(speed) + ")",
										  frame);
	item->setFlags(Qt::ItemIsEnabled | Qt::ItemNeverHasChildren);
	speeding_violations->addItem(item);
}


void SettingsPane::clearCollisions() {
	QListWidgetItem *item;
	while ((item = collisions->takeItem(0)) != nullptr) {
		delete item;
	}
}


void SettingsPane::clearSpeedingViolations() {
	QListWidgetItem *item;
	while ((item = speeding_violations->takeItem(0)) != nullptr) {
		delete item;
	}
}
