#include "cmwauthorityrole.h"
#include "ui_cmwauthorityrole.h"


CMWAuthorityRole::CMWAuthorityRole(QWidget *parent)
	: QWidget(parent)
{
	ui = new Ui::CMWAuthorityRole();
	ui->setupUi(this);

	QStringList headerList;
	headerList << "name" << "description" << "Level" << "id";
	ui->tableRole->setColumnCount(headerList.size());
	ui->tableRole->setHorizontalHeaderLabels(headerList);
	ui->tableRole->hideColumn(3);

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

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

	connect(ui->tableRole, &QTableWidget::currentCellChanged, this, &CMWAuthorityRole::on_role_currentCellChanged);
}

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

void CMWAuthorityRole::FreshRole()
{
	ui->tableRole->clearContents();
	auto mapRoles = m_AuthorityManager->GetRoles();
	ui->tableRole->setRowCount(mapRoles.size());
	int iRow = 0;
	for each (auto pair in mapRoles)
	{
		Role& role = pair.second;
		QTableWidgetItem* item;
		item = new QTableWidgetItem(role.Name.c_str());
		ui->tableRole->setItem(iRow, 0, item);
		item->setData(1000, role.ID);
		//item->setCheckState(Qt::Unchecked);

		item = new QTableWidgetItem(role.Description.c_str());
		ui->tableRole->setItem(iRow, 1, item);

		item = new QTableWidgetItem(QString::number(role.Level));
		ui->tableRole->setItem(iRow, 2, item);

		item = new QTableWidgetItem(QString::number(role.ID));
		ui->tableRole->setItem(iRow, 3, item);

		iRow++;
	}
}

void CMWAuthorityRole::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 CMWAuthorityRole::FreshPermissionState()
{
	std::map<int, int> mapRolePermission = m_AuthorityManager->GetPermissionsByRole(m_iSelRoleId);
	for (int i = 0; i < ui->tablePermission->rowCount(); i++)
	{
		QTableWidgetItem* item = ui->tablePermission->item(i, 0);
		int permissionId = item->data(1000).toInt();
		if (mapRolePermission.find(permissionId) != mapRolePermission.end())
		{
			item->setCheckState(Qt::Checked);
		}
		else
		{
			item->setCheckState(Qt::Unchecked);
		}
	}
}

void CMWAuthorityRole::FocusSelectedRole()
{
	if (m_iSelRoleId <= 0)
	{
		ui->tableRole->setCurrentItem(nullptr);
	}
	else
	{
		for (int i = 0; i < ui->tableRole->rowCount(); i++)
		{
			if (m_iSelRoleId == ui->tableRole->item(i, 0)->data(1000).toInt())
			{
				ui->tableRole->setCurrentItem(ui->tableRole->item(i, 0));
			}
		}
	}

	ui->tableRole->setFocus();
}

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

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

void CMWAuthorityRole::InitAuthority()
{
	
}

void CMWAuthorityRole::on_btnRefresh_clicked()
{
	FreshRole();
	FreshPermission();
	FreshPermissionState();
}

void CMWAuthorityRole::on_btnDel_clicked()
{

}

void CMWAuthorityRole::on_btnAdd_clicked()
{

}

void CMWAuthorityRole::on_btnModify_clicked()
{

}


void CMWAuthorityRole::on_role_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
	if (m_iLastUserSelRow != currentRow)
	{
		if (currentRow == -1)
		{
			m_iSelRoleId = -1;
		}
		else
		{
			m_iSelRoleId = ui->tableRole->item(currentRow, 0)->data(1000).toInt();
		}

		FreshPermissionState();
	}
}


void CMWAuthorityRole::on_btnModifyRole_clicked()
{
	if (m_iSelRoleId <= 0)return;

	std::vector<int> idList;
	for (int iRow = 0; iRow < ui->tablePermission->rowCount(); iRow++)
	{
		QTableWidgetItem* item = ui->tablePermission->item(iRow, 0);
		if (!item)continue;

		if (item->checkState() == Qt::Checked)
		{
			idList.push_back(item->data(1000).toInt());
		}
	}
	m_AuthorityManager->UpdateRolePermissions(m_iSelRoleId, idList);

	FocusSelectedRole();
}