#include "addapp.h"
#include <QFileDialog>
#include <QProcessEnvironment>
#include <QMessageBox>
#include <sqlitehelper.h>

#include "applicationmanager.h"

#if _MSC_VER >=1936
#include <filesystem>
namespace fs = std::filesystem;
#else
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif

AddApp::AddApp(bool isEdit, Record2 *row , QWidget* parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	SQLiteHelper sqlite_helper;
	setFixedSize(450, 450);
	setWindowFlags(Qt::WindowCloseButtonHint);
	form = new QFormLayout;
	name = new QLineEdit;
	form->addRow(QString::fromLocal8Bit("名称"), name);
	if(isEdit)
	{
		name_label = new QLabel(orig_name_str);
		form->addRow(QString::fromLocal8Bit("全名"), name_label);
	}
	else
	{
		orig_name = new QLineEdit;
		form->addRow(QString::fromLocal8Bit("全名"), orig_name);
	}
	if(row==nullptr)
	{
		path = new QPushButton(QString::fromLocal8Bit("浏览"));
		connect(path, &QPushButton::clicked, this, &AddApp::broswer_exe);
		form->addRow(QString::fromLocal8Bit("路径"), path);
		sort = new QLineEdit;
		sort->setValidator(new QIntValidator(10, 100));
		form->addRow(QString::fromLocal8Bit("排序"), sort);
		QList<Categrory> categrories;
		layout_inner = new QVBoxLayout;
		sqlite_helper.get_category(categrories);

		for (auto category : categrories)
		{
			if ((category.id == 1) || (category.id == 8) || (category.id == 4) || (category.id == 5 || category.id == 3))
			{
				continue;
			}
			QCheckBox* check_box = new QCheckBox(category.name);
			layout_inner->addWidget(check_box);
			categories_list.insert(category.id, check_box);
		}
		form->addRow(QString::fromLocal8Bit("类别"), layout_inner);
		submit_btn = new QPushButton(QString::fromLocal8Bit("提交"));
		form->addWidget(submit_btn);
		this->setLayout(form);
		connect(path, &QPushButton::click, this, &AddApp::broswer_exe);
		connect(submit_btn, &QPushButton::clicked, this, &AddApp::submit);
		this->isEdit = isEdit;
	}else
	{
		path = new QPushButton(QString::fromLocal8Bit("浏览"));
		path_str = row->exe_file;
		path->setText(QString::fromWCharArray(fs::path(path_str.toStdWString()).filename().c_str()));
		connect(path, &QPushButton::clicked, this, &AddApp::broswer_exe);
		form->addRow(QString::fromLocal8Bit("路径"), path);
		sort = new QLineEdit;
		sort->setText(QString::number(row->sort));
		sort->setValidator(new QIntValidator(10, 100));
		form->addRow(QString::fromLocal8Bit("排序"), sort);
		QList<Categrory> categrories;
		layout_inner = new QVBoxLayout;
		sqlite_helper.get_category(categrories);

		for (auto category : categrories)
		{
			if ((category.id == 1) || (category.id == 8) || (category.id == 4) || (category.id == 5 || category.id == 3))
			{
				continue;
			}
			QCheckBox* check_box = new QCheckBox(category.name);
			if(row->categories[category.id])
			{
				check_box->setCheckState(Qt::Checked);
			}
			layout_inner->addWidget(check_box);
			categories_list.insert(category.id, check_box);
		}
		form->addRow(QString::fromLocal8Bit("类别"), layout_inner);
		submit_btn = new QPushButton(QString::fromLocal8Bit("提交"));
		form->addWidget(submit_btn);
		this->setLayout(form);
		connect(path, &QPushButton::click, this, &AddApp::broswer_exe);
		connect(submit_btn, &QPushButton::clicked, this, &AddApp::submit);
		this->isEdit = isEdit;
	}
	
}

AddApp::~AddApp()
{
	//delete categories;
	delete name;
	if(isEdit)
	{
		delete name_label;
	}else
	{
		delete orig_name;
	}
	delete path;
	delete sort;
	delete submit_btn;
	delete form;
}

void AddApp::broswer_exe(){
	path_str = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择程序"),
		QApplication::applicationDirPath(), QString::fromLocal8Bit("应用程序 (*.exe)"));
	fs::path file(path_str.toStdWString());
	path->setText(QString::fromStdWString(file.filename().c_str()));
}

void AddApp::submit() {
	if((!orig_name->text().isEmpty())&&(!path->text().isEmpty()))
	{
		orig_name_str = orig_name->text();
		name_str = orig_name_str;
		sort_str = sort->text();
	}
	else
	{
		QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("未填写名称或路径"));
		return;
	}
	categories = new bool[8];
	categories[0] = true;
	categories[2] = true;
	categories[7] = true;
	categories[3] = false;
	categories[4] = false;
	for(auto key:categories_list.keys())
	{
		if(key==0||key==2||key==7||key==3||key==4)
		{
			continue;
		}
		else
		{
			categories[key - 1] = categories_list[key]->checkState();
		}
	}
	categories[0] = true;
	categories[7] = true;
	changed = true;
	close();
}
