/*
 *  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 "LoginWidget.h"

#include "crd_ut10xx.h"

LoginWidget::LoginWidget(QWidget *parent): QStackedWidget(parent)
{
	QGridLayout *loginLayout;
	QGridLayout *logoutLayout;
	QGridLayout *layout;
	
	//////////////////////////////////////////////////////////////////////////////////////////////
	//
	//	Login widget.
	//
	//////////////////////////////////////////////////////////////////////////////////////////////
	mLoginWidget = new QWidget;
	loginLayout = new QGridLayout;
	QGroupBox *loginOptions = new QGroupBox(tr("Login settings"));
	mUserLoginBox = new QGroupBox(tr("Login"));
	mSoLoginBox = new QGroupBox(tr("Login"));
	
	layout = new QGridLayout;
	mChooseSoLogin = new QCheckBox(tr("Login as administrator"));
	connect(mChooseSoLogin, SIGNAL(stateChanged(int)), this, SLOT(chooseSoLoginSlot(int)));
	
	layout->addWidget(mChooseSoLogin, 0, 0);
	loginOptions->setLayout(layout);
	
	layout = new QGridLayout;
	mUserInfo = new QLabel;
	mUserPin = new QLineEdit;
	mUserPin->setMaxLength(8);
	mUserPin->setEchoMode(QLineEdit::Password);
	mUserLogin = new QPushButton(QIcon(QString(":img/ok_green.png")), tr("Login"));
	connect(mUserLogin, SIGNAL(clicked()), this, SLOT(userLoginSlot()));
	connect(mUserPin, SIGNAL(returnPressed()), this, SLOT(userLoginSlot()));

	layout->addWidget(mUserInfo, 0, 0, 1, 3);
	layout->addWidget(new QLabel(tr("Password:")), 1, 0);
	layout->addWidget(mUserPin, 1, 1, 1, 2);
	layout->addWidget(mUserLogin, 2, 2);
	layout->setColumnStretch(1, 1);
	mUserLoginBox->setLayout(layout);
	
	layout = new QGridLayout;
	mSoInfo = new QLabel;
	mSoPin = new QLineEdit;
	mSoPin->setMaxLength(8);
	mSoPin->setEchoMode(QLineEdit::Password);
	mSoLogin = new QPushButton(QIcon(QString(":img/ok_green.png")), tr("Login"));
	connect(mSoLogin, SIGNAL(clicked()), this, SLOT(soLoginSlot()));
	connect(mSoPin, SIGNAL(returnPressed()), this, SLOT(soLoginSlot()));
		
	layout->addWidget(mSoInfo, 0, 0, 1, 3);
	layout->addWidget(new QLabel(tr("Password:")), 1, 0);
	layout->addWidget(mSoPin, 1, 1, 1, 2);
	layout->addWidget(mSoLogin, 2, 2);
	layout->setColumnStretch(1, 1);
	mSoLoginBox->setLayout(layout);
	
	loginLayout->addWidget(loginOptions, 0, 0);
	loginLayout->addWidget(mUserLoginBox, 1, 0);
	loginLayout->addWidget(mSoLoginBox, 2, 0);
	loginLayout->setRowStretch(3, 1);
	
	mSoLoginBox->hide();
	
	mLoginWidget->setLayout(loginLayout);
	
	//////////////////////////////////////////////////////////////////////////////////////////////
	//
	//	Logout widget.
	//
	//////////////////////////////////////////////////////////////////////////////////////////////
	mLogoutWidget = new QWidget;
	logoutLayout = new QGridLayout;
	mLoginInfo = new QLabel;
	mLogout = new QPushButton(tr("Logout"));
	connect(mLogout, SIGNAL(clicked()), this, SLOT(logoutSlot()));
	logoutLayout->addWidget(mLoginInfo, 1, 0, 1, 3);
	logoutLayout->addWidget(mLogout, 2, 1);
	logoutLayout->setColumnStretch(0, 1);
	logoutLayout->setColumnStretch(2, 1);
	logoutLayout->setRowStretch(0, 1);
	logoutLayout->setRowStretch(3, 1);
	mLogoutWidget->setLayout(logoutLayout);
	
	//////////////////////////////////////////////////////////////////////////////////////////////
	//
	//	Add all widgets to stack.
	//
	//////////////////////////////////////////////////////////////////////////////////////////////
	addWidget(mLoginWidget);
	addWidget(mLogoutWidget);
}

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

void
LoginWidget::setInfo(const CRD_UT10XX_info_t *info)
{
	mSoInfo->setText(tr("Number of wrong entered administrator password: <b>%1</b><br>Administrator password tries limit:: <b>%2</b><br>").
		arg(info->so_pin_trys).arg(info->so_pin_limit));
	mUserInfo->setText(tr("Number of wrong entered User password: <b>%1</b><br>User password tries limit:: <b>%2</b><br>").
		arg(info->user_pin_trys).arg(info->user_pin_limit));
}

void
LoginWidget::logoutSlot()
{
	clearInputs();
	clearFields();
	setCurrentWidget(mLoginWidget);
	emit logoutSig();
}

void
LoginWidget::chooseSoLoginSlot(int state)
{
	mSoPin->clear();
	mUserPin->clear();
	
	if (state == Qt::Checked)
	{
		mSoLoginBox->show();
		mUserLoginBox->hide();
	}
	else
	{
		mSoLoginBox->hide();
		mUserLoginBox->show();
	}
}

void
LoginWidget::soLoginSlot()
{
	bool correct;
	
	mSoPinBuf = (mSoPin->text()).toUtf8();
	clearInputs();
	emit soLoginSig(mSoPinBuf, correct);
	
	if (correct)
	{
		mLoginInfo->setText(tr("<h2 align=center><font color=red>You are logged in as"
			" Administrator</font></h2>"));
		setCurrentWidget(mLogoutWidget);
	}
	else
	{
		clearFields();
	}
}

void
LoginWidget::userLoginSlot()
{
	bool correct;
	
	mUserPinBuf = (mUserPin->text()).toUtf8();
	clearInputs();
	emit userLoginSig(mUserPinBuf, correct);
	
	if (correct)
	{
		mLoginInfo->setText(tr("<h2 align=center><font color=green>You are logged in as User</font></h2>"));
		setCurrentWidget(mLogoutWidget);
	}
	else
	{
		clearFields();
	}
}

void
LoginWidget::clearInputs()
{
	mSoPin->clear();
	mUserPin->clear();
}

void
LoginWidget::clearFields()
{
	mSoPinBuf.clear();
	mUserPinBuf.clear();
}

void
LoginWidget::focus2PIN()
{
	if(mChooseSoLogin->checkState() == Qt::Checked)
		mSoPin->setFocus();
	else
		mUserPin->setFocus();
}
