/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

#include "AdminWidget.h"
#include "MainWindow.h"

AdminWidget::AdminWidget(QWidget *parent): QWidget(parent)
{
	mLayout = new QGridLayout();
	setLayout(mLayout);
	
	// user PIN
	mUserPinChangeBox = new QGroupBox(tr("Change User password"));
	QGridLayout *boxLayout = new QGridLayout();
	mUserPinChangeBox->setLayout(boxLayout);
	
	boxLayout->addWidget(new QLabel(tr("New password:")), 0, 0);
	mNewUserPin = new QLineEdit();
	mNewUserPin->setMaxLength(8);
	mNewUserPin->setEchoMode(QLineEdit::Password);
	boxLayout->addWidget(mNewUserPin, 0, 1);
	
	boxLayout->addWidget(new QLabel(tr("Re-entered new password:")), 1, 0);
	mNewUserPinCheck = new QLineEdit();
	mNewUserPinCheck->setMaxLength(8);
	mNewUserPinCheck->setEchoMode(QLineEdit::Password);
	boxLayout->addWidget(mNewUserPinCheck, 1, 1);

	mUserPinChange = new QPushButton(QIcon(QString(":img/ok_green.png")), tr("Change password"));
	boxLayout->addWidget(mUserPinChange, 2, 1, Qt::AlignRight);

	connect(mUserPinChange, SIGNAL(clicked()), this, SLOT(userPinChangeSlot()));
	boxLayout->setColumnStretch(1, 1);
	mLayout->addWidget(mUserPinChangeBox, 0, 0);

	// admin PIN
	mSoPinChangeBox = new QGroupBox(tr("Change administrator password"));
	boxLayout = new QGridLayout();
	mSoPinChangeBox->setLayout(boxLayout);
	
	boxLayout->addWidget(new QLabel(tr("New password:")), 0, 0);
	mNewSoPin = new QLineEdit();
	mNewSoPin->setMaxLength(8);
	mNewSoPin->setEchoMode(QLineEdit::Password);
	boxLayout->addWidget(mNewSoPin, 0, 1);
	
	boxLayout->addWidget(new QLabel(tr("Re-entered new password:")), 1, 0);
	mNewSoPinCheck = new QLineEdit();
	mNewSoPinCheck->setMaxLength(8);
	mNewSoPinCheck->setEchoMode(QLineEdit::Password);
	boxLayout->addWidget(mNewSoPinCheck, 1, 1);

	mSoPinChange = new QPushButton(QIcon(QString(":img/ok_green.png")), tr("Change password"));
	boxLayout->addWidget(mSoPinChange, 2, 1, Qt::AlignRight);

	connect(mSoPinChange, SIGNAL(clicked()), this, SLOT(soPinChangeSlot()));
	boxLayout->setColumnStretch(1, 1);
	mLayout->addWidget(mSoPinChangeBox, 1, 0);
	
	// erase token
	mEraseBox = new QGroupBox(tr("Erase token"));
	boxLayout = new QGridLayout();
	mEraseBox->setLayout(boxLayout);

	QPixmap pixmap(QString(":img/warning.png"));
	QLabel *warning = new QLabel;
	warning->setPixmap(pixmap);
	boxLayout->addWidget(warning, 0, 0);
	boxLayout->addWidget(new QLabel(tr("<b>Warning! All data stored on token will be lost!</b>")), 0, 1);

	mErase = new QPushButton(QIcon(QString(":img/cancel.png")), tr("Erase"));
	boxLayout->addWidget(mErase, 1, 1, Qt::AlignRight);
	connect(mErase, SIGNAL(clicked()), this, SLOT(eraseSlot()));

	boxLayout->setColumnStretch(1, 1);
	mLayout->addWidget(mEraseBox, 2, 0);

	// add layouts to main layout
	mLayout->setRowStretch(3, 1);

	mUserPinChangeBox->setEnabled(false);
	mSoPinChangeBox->setEnabled(false);
	mEraseBox->setEnabled(false);
}

AdminWidget::~AdminWidget()
{
	clearInputs();
	clearFields();
}

void
AdminWidget::enableSoMenu()
{
	mUserPinChangeBox->setEnabled(true);
	mSoPinChangeBox->setEnabled(true);
	mEraseBox->setEnabled(true);
}

void
AdminWidget::enableUserMenu()
{
	mUserPinChangeBox->setEnabled(true);
}

void
AdminWidget::disableMenu()
{
	mUserPinChangeBox->setEnabled(false);
	mSoPinChangeBox->setEnabled(false);
	mEraseBox->setEnabled(false);
}

void
AdminWidget::userPinChangeSlot()
{
	if (mNewUserPin->text() != mNewUserPinCheck->text())
	{
		QMessageBox::critical(MainWindow::mainWnd, tr("Error"), tr("Entered User "
			"passwords are not equal."));
		clearInputs();
		clearFields();
		return;
	}
	
	mNewUserPinBuf = (mNewUserPin->text()).toUtf8();
	
	if (mNewUserPinBuf.length() > 9)
	{
		QMessageBox::critical(MainWindow::mainWnd, tr("Error"), tr("User password is "
			"too long. It may contains up to 8 characters. National characters will "
			"be coded on 2 chars."));
		clearInputs();
		clearFields();
		return;
	}
	
	clearInputs();
	
	int button = QMessageBox::question(MainWindow::mainWnd, tr("Question"), tr("Do you really "
		"want to change User password?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
	
	if (button == QMessageBox::Yes)
		emit userPinChangeSig(mNewUserPinBuf);
	
	clearFields();
}

void
AdminWidget::soPinChangeSlot()
{
	if (mNewSoPin->text() != mNewSoPinCheck->text())
	{
		QMessageBox::critical(MainWindow::mainWnd, tr("Error"), tr("Entered administrator "
			"passwords are not equal."));
		clearInputs();
		clearFields();
		return;
	}
	
	mNewSoPinBuf = (mNewSoPin->text()).toUtf8();
	
	if (mNewSoPinBuf.length() > 9)
	{
		QMessageBox::critical(MainWindow::mainWnd, tr("Error"), tr("Administrator password "
			"is too long. It may contains up to 8 characters. National characters will "
			"be coded on 2 chars."));
		clearInputs();
		clearFields();
		return;
	}
	
	clearInputs();
	
	int button = QMessageBox::question(MainWindow::mainWnd, tr("Question"), tr("Do you really "
		"want to change administrator password?"), QMessageBox::Yes | QMessageBox::No, 
		QMessageBox::No);
	
	if (button == QMessageBox::Yes)
		emit soPinChangeSig(mNewSoPinBuf);
	
	clearFields();
}

void
AdminWidget::eraseSlot()
{
	int button = QMessageBox::question(MainWindow::mainWnd, tr("Question"), tr("Warning! All "
		"data stored on token will be lost! Do you really want to erase token?"), 
		QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
	
	if (button == QMessageBox::Yes)
		emit eraseSig();
}

void
AdminWidget::clearFields()
{
	mNewSoPinBuf.clear();
	mNewUserPinBuf.clear();
}

void
AdminWidget::clearInputs()
{
	mNewSoPin->clear();
	mNewSoPinCheck->clear();
	mNewUserPin->clear();
	mNewUserPinCheck->clear();
}
