#include "cmwauthoritypermission.h"
#include "ui_cmwauthoritypermission.h"
#include <QMessageBox>
CMWAuthorityPermission::CMWAuthorityPermission(QWidget *parent)
	: QWidget(parent)
{
	ui = new Ui::CMWAuthorityPermission();
	ui->setupUi(this);


	QStringList headerList;
	headerList << "name" << "tag" << "description" << "id";
	ui->tablePermission->setColumnCount(headerList.size());
	ui->tablePermission->setHorizontalHeaderLabels(headerList);
	//ui->tablePermission->hideColumn(3);

	ui->tablePermission->setSelectionBehavior(QAbstractItemView::SelectRows);

	InitAuthority();

	//connect(ui->tablePermission, &QTableWidget::currentCellChanged, this, &CMWAuthorityPermission::on_tablePermission_currentCellChanged);
}

CMWAuthorityPermission::~CMWAuthorityPermission()
{
	delete ui;
}

void CMWAuthorityPermission::FreshPermission()
{
	QTableWidget* tableWidget = ui->tablePermission;
	tableWidget->clearContents();
	auto mapPermissions = m_AuthorityManager->GetPermissions();
	tableWidget->setRowCount(mapPermissions.size());
	int iRow = 0;
	for each (auto pair in mapPermissions)
	{
		Permission& permission = pair.second;
		QTableWidgetItem* item;
		item = new QTableWidgetItem(permission.Name.c_str());
		tableWidget->setItem(iRow, 0, item);
		item->setData(1000, permission.ID);
		item->setCheckState(Qt::Unchecked);

		item = new QTableWidgetItem(permission.TagName.c_str());
		tableWidget->setItem(iRow, 1, item);

		item = new QTableWidgetItem(permission.Description.c_str());
		tableWidget->setItem(iRow, 2, item);

		item = new QTableWidgetItem(QString::number(permission.ID));
		tableWidget->setItem(iRow, 3, item);

		iRow++;
	}
}

void CMWAuthorityPermission::FreshPermissionState()
{
	std::map<int, Permission> permissionList = m_AuthorityManager->GetPermissionsByUser(m_AuthorityManager->GetLoginUser().ID);
	for (int i = 0; i < ui->tablePermission->rowCount(); i++)
	{
		QTableWidgetItem* item = ui->tablePermission->item(i, 0);
		int permissionId = item->data(1000).toInt();
		if (permissionList.find(permissionId) != permissionList.end())
		{
			item->setCheckState(Qt::Checked);
		}
		else 
		{
			item->setCheckState(Qt::Unchecked);
		}
	}

}

void CMWAuthorityPermission::SetAuthorityManager(IAuthorityManager* instance)
{
	IAuthoritySupport::SetAuthorityManager(instance);
	m_AuthorityManager = static_cast<CSqliteAuthorityManager*>(instance);
}

void CMWAuthorityPermission::FreshAuthority()
{
	ui->btnRefresh->click();

	for (auto iter : m_mapObjectsPermission) {
		PermissionInfo& info = iter.second;

		if (m_AuthorityManager->HasPermission(info.sPermissionName)) {
			if (info.eDisplayBehaviour & NoPermissionBehaviour::kDisable) {
				iter.first->setEnabled(true);
			} 
			if (info.eDisplayBehaviour & NoPermissionBehaviour::kHide) {
				iter.first->setEnabled(true);
			}
		} else {
			if (info.eDisplayBehaviour & NoPermissionBehaviour::kHide) {
				iter.first->setVisible(false);
			}

			if (info.eDisplayBehaviour & NoPermissionBehaviour::kDisable) {
				iter.first->setEnabled(false);
			}
			
		}
		
	}
}

void CMWAuthorityPermission::on_btnRefresh_clicked()
{
	FreshPermission();
	FreshPermissionState();
}

void CMWAuthorityPermission::on_btnDel_clicked()
{
	m_AuthorityManager->DeletePermission(m_iSelPermissionId);
	FreshPermission();
}

void CMWAuthorityPermission::on_btnAdd_clicked()
{
	QString error = "";
	Permission permission;
	permission.Name = ui->txtName->text().toStdString();
	permission.TagName= ui->txtTagName->text().toStdString();
	permission.Description = ui->txtDescription->text().toStdString();
	if (permission.Name.empty() && permission.TagName.empty())
	{
		QMessageBox::information(this, QString("error"), "use empty name");
		return;
	}
	if (false == m_AuthorityManager->AddPermission(permission))
	{
		QMessageBox::information(this, QString("error"), "failed");
		return;
	}

	FreshPermission();
}

void CMWAuthorityPermission::on_btnModify_clicked()
{
	QString error = "";
	Permission permission;
	permission.ID = m_iSelPermissionId;
	permission.Name = ui->txtName->text().toStdString();
	permission.TagName = ui->txtTagName->text().toStdString();
	permission.Description = ui->txtDescription->text().toStdString();
	if (m_iSelPermissionId <= 0)
	{
		QMessageBox::information(this, QString("error"), "please select permission for modify");
		return;
	}
	if (permission.Name.empty() && permission.TagName.empty())
	{
		QMessageBox::information(this, QString("error"), "use empty name");
		return;
	}

	if (false == m_AuthorityManager->MotifyPermission(permission))
	{
		QMessageBox::information(this, QString("error"), "failed");
		return;
	}

	FreshPermission();
}

void CMWAuthorityPermission::on_btnLogin_clicked()
{
	if (!m_AuthorityManager->Login(ui->txtLoginName->text().toStdString(), ui->txtPassword->text().toStdString()))
	{
		QMessageBox::information(this, "error", "login error");
		return;
	}

	FreshPermissionState();
}

void CMWAuthorityPermission::on_tablePermission_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
	if (m_iLastUserSelRow != currentRow)
	{
		if (currentRow == -1)
		{
			m_iSelPermissionId = -1;
		}
		else
		{
			m_iSelPermissionId = ui->tablePermission->item(currentRow, 0)->data(1000).toInt();
		}

		//FreshPermission();
	}
}

void CMWAuthorityPermission::InitAuthority()
{
	SetControlPermission(ui->btnDel, PermissionInfo("add", NoPermissionBehaviour::kHide));
    SetControlPermission(ui->btnDisable, PermissionInfo("modify", NoPermissionBehaviour::kDisable));
}

void CMWAuthorityPermission::SetControlPermission(QWidget* object, const PermissionInfo& permissionInfo)
{
	m_mapObjectsPermission.emplace(object, permissionInfo);
}
