//
// Created by Krad on 2021/11/23.
//

#include "UserOperationLogForm.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QAbstractListModel>
#include "log/UserOperationLog.h"
#include "log/LogFileTableModel.h"
#include <QHeaderView>
#include <QLabel>
#include <QTableView>
#include "components/SlideableTableView.h"
#include <QPushButton>
#include "SelectDialog.h"
#include "mainwindow.h"
#include "event/EventCenter.h"
QString fileNameToDate(QString fileName)
{
	return fileName.split("log/")[1].replace("-op.log", "");
}
QString dateToFileName(QString date)
{
	return QString("./log/") + date + QString("-op.log");
}

UserOperationLogForm::UserOperationLogForm(QWidget* parent) {
	layout = new QVBoxLayout(this);
	QWidget* header = new QWidget(this);
	QHBoxLayout* headerLayout = new QHBoxLayout(header);
	QLabel* logdate = new QLabel(tr("Log Date:"));
	headerLayout->addWidget(logdate);
	btn = new QPushButton(header);
	headerLayout->addWidget(btn, 0, Qt::AlignLeft);
	headerLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));
	table = new SlideableTableView(this);
	layout->addWidget(header);
	layout->addWidget(table);
	model = new LogFileTableModel(this);
	//暂时先放构造函数，之后需要移除，等需要时再调用
	loadUserOperationLog();
	connect(btn, &QPushButton::clicked, [=]() {
		auto files = UserOperationLog::getHistoryLogFiles();
		QStringList dates;
		for (auto f : files)
		{
			dates << fileNameToDate(f);
		}
		if (!dialog) {
			dialog = new SelectDialog(this);
			dialog->setWindowModality(Qt::WindowModal);
		}
		dialog->setAvailableDates(dates);
		if (!selectedDateStr.isEmpty()) dialog->setSelectedValue(selectedDateStr);
		if (dialog->exec() == QDialog::Accepted)
		{
			QString date = dialog->getSelectedValue();
			QString f = dateToFileName(date);
			this->loadUserOperationLog(f, date);
		}
		});

	connect(EventCenter::Default(), &EventCenter::ReloadLanguage, [=]() {
		logdate->setText(tr("Log Date:"));
		});

}

UserOperationLogForm::~UserOperationLogForm() {

}


void UserOperationLogForm::loadUserOperationLog() {

	QString filePath = UserOperationLog::Default()->currentLogFile();
	model->setFileName(filePath);
	btn->setText(fileNameToDate(filePath));
	loadUserOperationLog(filePath, fileNameToDate(filePath));
	selectedDateStr = fileNameToDate(filePath);
}

void UserOperationLogForm::loadUserOperationLog(const QString& fileName, const QString& date) {
	selectedDateStr = date;
	model->setFileName(dateToFileName(date));
	btn->setText(date);
	QStringList header;
	header << "Operation Date" << "Operation Time" << "User" << "Operation";
	model->setHeader(header);
	//    UserOperationLog::getHistoryLogFiles();
	delete table;
	table = new SlideableTableView(this);
	layout->addWidget(table);
	table->setModel(model);

	table->setAlternatingRowColors(true);
	table->setSelectionMode(QAbstractItemView::NoSelection);
	table->setEditTriggers(QAbstractItemView::NoEditTriggers);
	table->verticalHeader()->setDefaultSectionSize(38);
	table->horizontalHeader()->setStretchLastSection(true);
	table->setColumnWidth(0, 250);
	table->setColumnWidth(1, 250);
}
