#include "pch.h"
#include <QTreeWidget>
#include <QAction>
#include <QMenu>
#include "ProjectManager.h"

ProjectManager::ProjectManager(QWidget *parent, metavis* owner)
	: QWidget(parent), owner(owner)
{
	QGridLayout* gridlayout = new QGridLayout();
	gridlayout->setContentsMargins(0, 0, 0, 0);
	treeWidget = new QTreeWidget();
	treeWidget->setColumnCount(1);
	treeWidget->setHeaderHidden(true);
	treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(treeWidget, &QTreeWidget::customContextMenuRequested, this, &ProjectManager::prepareMenu);

	QTreeWidgetItem* projectItem = new QTreeWidgetItem(treeWidget, QStringList("Project"), ProjectManager::Types::Project);
	projectItem->setExpanded(true);
	QTreeWidgetItem* filesItem = new QTreeWidgetItem(projectItem, QStringList("Metalog Files"), ProjectManager::Types::LogManger);
	new QTreeWidgetItem(filesItem, QStringList("run.metalog"), ProjectManager::Types::LogFile);
	new QTreeWidgetItem(filesItem, QStringList("run2.metalog"), ProjectManager::Types::LogFile);
	new QTreeWidgetItem(filesItem, QStringList("run3.metalog"), ProjectManager::Types::LogFile);
	new QTreeWidgetItem(filesItem, QStringList("run4.metalog"), ProjectManager::Types::LogFile);
	new QTreeWidgetItem(filesItem, QStringList("run5.metalog"), ProjectManager::Types::LogFile);
	filesItem->setExpanded(true);
	QTreeWidgetItem* graphItem = new QTreeWidgetItem(projectItem, QStringList("Graph Views"), ProjectManager::Types::GraphManger);
	new QTreeWidgetItem(graphItem, QStringList("Best vs Avg"), ProjectManager::Types::Graph);
	new QTreeWidgetItem(graphItem, QStringList("Mean Hamming Distance"), ProjectManager::Types::Graph);
	new QTreeWidgetItem(graphItem, QStringList("Min Max Average"), ProjectManager::Types::Graph);
	new QTreeWidgetItem(graphItem, QStringList("Particle"), ProjectManager::Types::Graph);
	graphItem->setExpanded(true);
	gridlayout->addWidget(treeWidget);
	this->setLayout(gridlayout);



	//Create Menus
	filesMenu = new QMenu(this);
	QAction* metaLogAddAction = new QAction("Add new .metalog-File...");
	connect(metaLogAddAction, &QAction::triggered, owner, &metavis::openFile);
	QAction* removeAllFilesAction = new QAction("Remove all metalog-Files");
	filesMenu->addAction(metaLogAddAction);
	filesMenu->addAction(removeAllFilesAction);
	
	metalogFileMenu = new QMenu(this);
	QIcon removeIcon(":/metavis/Resources/close_big_red.svg");
	QAction* removeMetalogFileAction = new QAction("Remove");
	removeMetalogFileAction->setIcon(removeIcon);
	QAction* assignToGraphAction = new QAction("Assign...");
	metalogFileMenu->addAction(removeMetalogFileAction);
	metalogFileMenu->addAction(assignToGraphAction);


	graphMenu = new QMenu(this);
	QAction* assignMetalogFileAction = new QAction("Remove");
	assignMetalogFileAction->setIcon(removeIcon);
	graphMenu->addAction(assignMetalogFileAction);

}

ProjectManager::~ProjectManager()
{
}

void ProjectManager::prepareMenu(const QPoint& pos) {
	qDebug() << "TestMenu";
	QTreeWidgetItem* item = treeWidget->itemAt(pos);
	qDebug() << item->text(0) << "  " << item->type();
	switch (item->type()) {
	case Types::Graph:
		graphMenu->exec(treeWidget->mapToGlobal(pos));
		break;
	case Types::GraphManger:
		break;
	case Types::LogFile:
		metalogFileMenu->exec(treeWidget->mapToGlobal(pos));
		break;
	case Types::LogManger:
		filesMenu->exec(treeWidget->mapToGlobal(pos));
		break;
	default:
		break;
	}

}

FileTreeItem::FileTreeItem(QTreeWidget* parent, const QStringList& strings, RunData* rundata, std::list<RunData>* list, int type) 
	: QTreeWidgetItem(parent, strings, type), list(list), rundata(rundata)
{

}

void FileTreeItem::remove()
{
	qDebug() << "Remove";
	// Remove from graphs

	//Remove from List
	//list->remove(*rundata);
	//Delete self
	delete this;
}
