#include "applicationmanagerpage.h"

#include <QSvgRenderer>
#include <QPainter>
#include "applicationmanager.h"
#include "config.h"

ApplicationManagerPage::ApplicationManagerPage(int category, ApplicationManager *application_manager,QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	this->category = category;
	this->application_manager = application_manager;
	sqlite_helper.get_buttons(button_structs);
	layout = new QVBoxLayout;
	sqlite_helper.get_all_software(softwares, category);
	table = new QTableWidget(this);
	table->setColumnCount(8);
	QStringList titles;
	titles<< QString::fromLocal8Bit("图标") << QString::fromLocal8Bit("名称") << QString::fromLocal8Bit("全名") << QString::fromLocal8Bit("开发者") << QString::fromLocal8Bit("最后使用日期") << QString::fromLocal8Bit("添加到导航条") <<QString::fromLocal8Bit("设置") << QString::fromLocal8Bit("打开");
	table->setHorizontalHeaderLabels(titles);
	table->setEditTriggers(QAbstractItemView::NoEditTriggers);
	table->setStyleSheet("QTableWidget{border:1px;outline:1px;align: center;}  QQTableWidget::widget  QTableWidget::item {text-align: center;}");
	table->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	int i = 0;
	for (i = 0; i < 8; i++)
	{
		table->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
		
	}
	i = 0;
	QTableWidgetItem* item = new QTableWidgetItem;
	item->setTextAlignment(Qt::AlignCenter);
	table->setItemPrototype(item);
	for (auto software : softwares)
	{
		table->insertRow(i);
		
		table->setItem(i, 1, new QTableWidgetItem(software.name));
		table->setItem(i, 2, new QTableWidgetItem(software.orig_name));
		table->setItem(i, 3, new QTableWidgetItem(software.dev));
		QString date;
		std::tm* time;
		time = std::localtime(&software.use_time);
		date = QString::number(1900 + time->tm_year, 10);
		date += "/";
		date += QString::number(1 + time->tm_mon, 10);
		date += "/";
		date += QString::number(time->tm_mday, 10);
		table->setItem(i, 4, new QTableWidgetItem(date));
		Record2 record2;
		record2.icon = new QSvgWidget;
		QFile file(software.logo);
		if(file.exists())
		{
			record2.icon->load(software.logo);
		}
		else {
			record2.icon->load(QApplication::applicationDirPath() + DEFAULT_IMAGE);
		}
		record2.icon->setMaximumSize(80, 80);
		table->setCellWidget(i, 0, record2.icon);
		record2.settings = new MiniButton(software.orig_name, "settings");
		record2.settings->setText(QString::fromLocal8Bit("设置"));
		record2.settings->setMaximumSize(60, 40);
		connect(record2.settings, &MiniButton::click0, record2.settings, &MiniButton::onclick2);
		connect(record2.settings, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
		record2.open = new MiniButton(software.orig_name, "open");
		record2.open->setText(QString::fromLocal8Bit("打开"));
		record2.open->setMaximumSize(60, 40);
		connect(record2.open, &MiniButton::click0, record2.open, &MiniButton::onclick2);
		connect(record2.open, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
		record2.exe_file = software.path;
		record2.orig_name = software.orig_name;
		record2.url = software.url;
		for(int i=0;i<8;i++)
		{
			record2.categories[i] = false;
		}
		for(int i=0;i<8;i++)
		{
			if (software.category_id.contains(QString::number(i)))
			{
				record2.categories[i] = true;
			}
		}
		record2.sort = software.sort;
		record2.check_box = new QCheckBox;
		if(software.type=="navbar")
		{
			record2.check_box->setCheckState(Qt::Checked);
			checked.insert(software.orig_name, true);
		}
		else
		{
			record2.check_box->setCheckState(Qt::Unchecked);
			checked.insert(software.orig_name, false);
		}
		connect(record2.check_box, &QCheckBox::clicked, this, &ApplicationManagerPage::onclicked);
		QTableWidgetItem* check_box_item = new QTableWidgetItem;
		table->setItem(i, 5, check_box_item);
		table->setCellWidget(i, 5, record2.check_box);
		check_box_item->setTextAlignment(Qt::AlignCenter);
		table->setCellWidget(i, 6, record2.settings);
		//table->item(i, 5)->setTextAlignment(Qt::AlignCenter);
		table->setCellWidget(i, 7, record2.open);
		//table->item(i, 6)->setTextAlignment(Qt::AlignCenter);
		rows.insert(software.orig_name, record2);
		table->resizeColumnToContents(0);
		//table->item(i, 0)->setTextAlignment(Qt::AlignCenter);
		//table->verticalHeader()->setSectionResizeMode(i, QHeaderView::ResizeToContents);
		i++;
	}
	layout->addWidget(table);
	setLayout(layout);

}
ApplicationManagerPage::~ApplicationManagerPage()
{
	for (auto button : buttons)
	{
		delete button;
	}
	delete table;
	delete layout;
}

void ApplicationManagerPage::onclicked()
{
	for(int i=0;i<table->rowCount();i++)
	{
		if(((QCheckBox *)table->cellWidget(i,4))->checkState()==Qt::Checked)
		{
			if(!checked[table->item(i,1)->text()])
			{
				QString orig_name = table->item(i, 1)->text();
				bool ok=sqlite_helper.edit_software(table->item(i, 0)->text(), orig_name, rows[orig_name].exe_file, QString::number(rows[orig_name].sort), rows[orig_name].categories,"navbar");
				if(!ok)
				{
					QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("更新部分或全部失败"));
					break;
				}
			}
		}else
		{
			if (checked[table->item(i, 1)->text()])
			{
				QString orig_name = table->item(i, 1)->text();
				bool ok = sqlite_helper.edit_software(table->item(i, 0)->text(), orig_name, rows[orig_name].exe_file, QString::number(rows[orig_name].sort), rows[orig_name].categories, "app");
				if (!ok)
				{
					QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("更新部分或全部失败"));
					break;
				}
			}
		}
	}
	emit application_manager->refresh();
}

void ApplicationManagerPage::refresh()
{
	int rowcount = table->rowCount();
	for(int i=0;i<rowcount;i++)
	{
		table->removeRow(0);
	}
	for(auto row:rows)
	{
		disconnect(row.settings, &MiniButton::click0, row.settings, &MiniButton::onclick2);
		disconnect(row.settings, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
		disconnect(row.open, &MiniButton::click0, row.open, &MiniButton::onclick2);
		disconnect(row.open, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
		disconnect(row.check_box, &QCheckBox::clicked, this, &ApplicationManagerPage::onclicked);
	}
	softwares.clear();
	sqlite_helper.get_all_software(softwares, category);
	int i = 0;
	for (auto software : softwares)
	{
		table->insertRow(i);
		table->setItem(i, 0, new QTableWidgetItem(software.name));
		table->setItem(i, 1, new QTableWidgetItem(software.orig_name));
		table->setItem(i, 2, new QTableWidgetItem(software.dev));
		QString date;
		std::tm* time;
		time = std::localtime(&software.use_time);
		date = QString::number(1900 + time->tm_year, 10);
		date += "/";
		date += QString::number(1 + time->tm_mon, 10);
		date += "/";
		date += QString::number(time->tm_mday, 10);
		table->setItem(i, 3, new QTableWidgetItem(date));
		Record2 record2;
		record2.settings = new MiniButton(software.orig_name, "settings");
		record2.settings->setText(QString::fromLocal8Bit("设置"));
		record2.settings->setMaximumSize(60, 40);
		connect(record2.settings, &MiniButton::click0, record2.settings, &MiniButton::onclick2);
		connect(record2.settings, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
		record2.open = new MiniButton(software.orig_name, "open");
		record2.open->setText(QString::fromLocal8Bit("打开"));
		record2.open->setMaximumSize(60, 40);		connect(record2.open, &MiniButton::click0, record2.open, &MiniButton::onclick2);
		connect(record2.open, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
		record2.exe_file = software.path;
		record2.orig_name = software.orig_name;
		for (int i = 0; i < 8; i++)
		{
			record2.categories[i] = false;
		}
		for (int i = 0; i < 8; i++)
		{
			if (software.category_id.contains(QString::number(i)))
			{
				record2.categories[i] = true;
			}
		}
		record2.sort = software.sort;
		record2.check_box = new QCheckBox;
		if (software.type == "navbar")
		{
			record2.check_box->setCheckState(Qt::Checked);
			checked.insert(software.orig_name, true);
		}
		else
		{
			record2.check_box->setCheckState(Qt::Unchecked);
			checked.insert(software.orig_name, false);
		}
		connect(record2.check_box, &QCheckBox::clicked, this, &ApplicationManagerPage::onclicked);
		table->setCellWidget(i, 4, record2.check_box);
		table->setCellWidget(i, 5, record2.settings);
		//table->item(i, 5)->setTextAlignment(Qt::AlignCenter);
		table->setCellWidget(i, 6, record2.open);
		//table->item(i, 6)->setTextAlignment(Qt::AlignCenter);
		rows.insert(software.orig_name, record2);
		i++;
	}
}
