#define Q_OS_LINUX
#include <QCommonStyle>
#include "editwizard.h"


editwizard::editwizard(PRights * userrights, QWidget *parent) 
{
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    setAttribute(Qt::WA_DeleteOnClose);
	ScanningActive=0;
	UserRights = userrights;
	CurrentUser = new PUserInfo(); 		

	MainLayout = new QStackedLayout;	
	MainLayout->setContentsMargins(0,0,0,0);	
MainLayout->addWidget(CreateSearchPage());	
MainLayout->addWidget(CreateSettingsPage());
	
	MainLayout->setCurrentIndex(0);
    this->setLayout(MainLayout);
	
	
utility::SetKeyboardMode(utility::KBMode_Alpha);
connect(UserList,SIGNAL(clicked(const QModelIndex &)),this,SLOT(userSelected(const QModelIndex &)));
connect(SearchButton,SIGNAL(clicked()),this,SLOT(Search()));



}

void editwizard::keypressed(QByteArray &keyt){

if(ScanningActive){
	emit DataForScan(keyt); 
	return;
	}


if(keyt[0]=='#'){ 
QChar key = utility::WinToUtf(keyt[1]);
if (SearchEdit->hasFocus()){
		SearchEdit->Keypressed(keyt);
		return;
	}
if (NameEdit->hasFocus()){
	NameEdit->Keypressed(keyt);
	return;

}
if (SurnameEdit->hasFocus()){
	SurnameEdit->Keypressed(keyt);
	return;

}

if (PinEdit->hasFocus()){
	if((key=='#')){
		PinEdit->backspace();
		}		
		else{
		PinEdit->setText(PinEdit->text().replace(PinEdit->text().length(),1,key));		
		}
}


}

}

void editwizard::userSelected(const QModelIndex & index){

	int id;
	MainLayout->setCurrentIndex(1);
	id = UserList->model()->data(UserList->model()->index(index.row(),0)).toInt();

	CurrentUser->SetUserID(id);
	RefreshData();
}

void editwizard::Search(void){

UserList->setModel(CurrentUser->GetUserList(UserRights,SearchEdit->text()));


}

QWidget* editwizard::CreateSettingsPage(void){

QWidget* PageWidget = new QWidget();

QVBoxLayout* mainlayout = new QVBoxLayout();


//******************* střed stránky ****************


QTabWidget* tabwidget = new QTabWidget;
tabwidget->addTab(CreateNameTab(),"&Údaje");
tabwidget->addTab(CreateFprTab(),"&Otisk");
tabwidget->addTab(CreateRfidTab(),"&RFID");
tabwidget->addTab(CreatePinTab(),"&PIN");
tabwidget->addTab(CreateGroupTab(),"&Skupina");
tabwidget->setUsesScrollButtons(false);
//****************** zápatí *********************

QCommonStyle style;

	Save = new QPushButton("&Uložit", this);
	Save->setIcon(style.standardIcon(QStyle::SP_DialogSaveButton));
	Save->setLayoutDirection(Qt::RightToLeft);
	
	Prev = new QPushButton("&Zpět", this);
	Prev->setIcon(style.standardIcon(QStyle::SP_ArrowBack));
	
	Cancel = new QPushButton("&Zrušit", this);
	Cancel->setIcon(style.standardIcon(QStyle::SP_DialogCancelButton));
	
	
	QHBoxLayout* buttonLayout = new QHBoxLayout;
    
    buttonLayout->addWidget(Cancel);
	buttonLayout->addStretch();    
	buttonLayout->addWidget(Prev);
    buttonLayout->addWidget(Save);
	buttonLayout->setSpacing(10);
	
	buttonLayout->setContentsMargins(11,11,11,11);
	connect(Save, SIGNAL(clicked()), this, SLOT(buttSave()));
	connect(Prev, SIGNAL(clicked()), this, SLOT(buttPrev()));
	connect(Cancel, SIGNAL(clicked()), this, SLOT(buttCancel()));

	mainlayout->addLayout(utility::CreateHeader("    Editace uživatele","EditUser"));
	mainlayout->addWidget(tabwidget,1);
	mainlayout->addLayout(buttonLayout);
	mainlayout->setContentsMargins(0,0,0,0);
	mainlayout->setSpacing(0);

PageWidget->setLayout(mainlayout);

return(PageWidget);

}


QWidget* editwizard::CreateSearchPage(void){

	QWidget* PageWidget = new QWidget();
	QHBoxLayout* searchlayout = new QHBoxLayout();
	QVBoxLayout* mainlayout = new QVBoxLayout();

	QLabel* lab1 = new QLabel("Vyberte uživatele k editaci :");
	 UserList = new QTableView();
	SearchEdit = new PLineEdit();
	SearchButton = new QPushButton("&Filtrovat");


	searchlayout->addWidget(SearchEdit);
	searchlayout->addWidget(SearchButton);

      	UserList->setModel(CurrentUser->GetUserList(UserRights));
	UserList->verticalScrollBar()->setFixedWidth(30);
	
	UserList->hideColumn(0);
	UserList->verticalHeader()->hide();
	UserList->horizontalHeader()->hide();
	UserList->setSelectionBehavior(QAbstractItemView::SelectRows);
	
	UserList->setSelectionMode(QAbstractItemView::SingleSelection);
	UserList->horizontalHeader()->setStretchLastSection(true);


	mainlayout->addWidget(lab1);
	mainlayout->addLayout(searchlayout);
	mainlayout->addWidget(UserList);
	
	PageWidget->setLayout(mainlayout);

	return(PageWidget);
}


void editwizard::buttSave(void){

CurrentUser->SetNameSurname(NameEdit->text(),SurnameEdit->text());
CurrentUser->SetRFID(RfidEdit->text().toAscii());

if(PinEdit->text().size()!=0)
CurrentUser->SetPIN(PinEdit->text().toInt());

char rights=' ';

		if(SuperAdmin->isChecked())
		rights=PRights::SuperAdmin;
		else if(GroupAdmin->isChecked())
		rights=PRights::GroupAdmin;
		else if(StandardUser->isChecked())
		rights=PRights::User;

		CurrentUser->SetRights(rights);
		CurrentUser->SetGroup(GroupSelect->currentIndex());
 if(CurrentUser->UserDataUpdate())
	QMessageBox::warning(this,"PRST","Uživatel aktualizován.", QMessageBox::Ok);
// CurrentUser->UserDataUpdate();

}

void editwizard::buttPrev(void){
MainLayout->setCurrentIndex(0);
}

void editwizard::buttCancel(void){

utility::SetKeyboardMode(utility::KBMode_Leaving);
delete CurrentUser;
this->close();

}

void editwizard::buttDelete(void){

if(CurrentUser->UserDelete())
QMessageBox::warning(this,"PRST","Uživatel smazán.", QMessageBox::Ok);
UserList->setModel(CurrentUser->GetUserList(UserRights));
buttPrev();
}

void editwizard::buttScan(void){
Scanwizard = new addwizard(0,0,addwizard::UpdateFprOnly,CurrentUser->GetID());
ScanningActive = true;
connect(this, SIGNAL(DataForScan(QByteArray&)), Scanwizard, SLOT(keypressed(QByteArray&)));
connect(Scanwizard, SIGNAL(destroyed()),this, SLOT(WizzardClose()));
Scanwizard->showFullScreen();



}


QWidget* editwizard::CreateNameTab(void){
	QWidget* PageWidget = new QWidget();
	QVBoxLayout* mainlayout = new QVBoxLayout();
	
	QCommonStyle style;

	NameEdit = new PLineEdit();
	SurnameEdit = new PLineEdit(); 
	Delete = new QPushButton("&Smazat uživatele");
	Delete->setIcon(style.standardIcon(QStyle::SP_TrashIcon));  

	QFormLayout* formLayout = new QFormLayout;
	formLayout->addRow("&Jméno:", NameEdit);
 	formLayout->addRow("&Příjmení:", SurnameEdit);
	formLayout->setFormAlignment(Qt::AlignCenter);
	
	mainlayout->addLayout(formLayout);
	mainlayout->addWidget(Delete);

	connect(Delete, SIGNAL(clicked()), this, SLOT(buttDelete()));

	PageWidget->setLayout(mainlayout);
	return(PageWidget);

}
QWidget* editwizard::CreateFprTab(void){
	QWidget* PageWidget = new QWidget();
	QVBoxLayout* mainlayout = new QVBoxLayout();

	FprDate = new QLabel();
	Scan = new QPushButton("Aktualizovat otisk");

	mainlayout->addWidget(FprDate,Qt::AlignCenter);
	mainlayout->addWidget(Scan);

	connect(Scan, SIGNAL(clicked()), this, SLOT(buttScan()));

	PageWidget->setLayout(mainlayout);
	return(PageWidget);

}
QWidget* editwizard::CreateRfidTab(void){
	QWidget* PageWidget = new QWidget();
	QVBoxLayout* mainlayout = new QVBoxLayout();
	
	QLabel* lab = new QLabel("Přiložte kartu pro aktualizaci...");
	RfidEdit = new QLineEdit();

	mainlayout->addWidget(lab);
	mainlayout->addWidget(RfidEdit);

	PageWidget->setLayout(mainlayout);
	return(PageWidget);

}
QWidget* editwizard::CreatePinTab(void){
	QWidget* PageWidget = new QWidget();
	QVBoxLayout* mainlayout = new QVBoxLayout();
	

	QLabel* lab = new QLabel("Zadejte nový pin :");
	PinEdit = new QLineEdit();

	PinEdit->setMaxLength(4);

	
	mainlayout->addWidget(lab);
	mainlayout->addWidget(PinEdit);
	
	PageWidget->setLayout(mainlayout);
	return(PageWidget);

}
QWidget* editwizard::CreateGroupTab(void){

	QWidget* PageWidget = new QWidget();
	QVBoxLayout* mainlayout = new QVBoxLayout();

	QLabel* lab = new QLabel("Vyberte ze seznamu skupinu :");
	GroupSelect = new QComboBox;

	
	GroupSelect->addItems(*(CurrentUser->GetGroupList()));
	
	SuperAdmin = new QRadioButton("Hlavní správce");
	GroupAdmin = new QRadioButton("Správce oddělení");
	StandardUser = new QRadioButton("Uživatelský účet");
	
	if(UserRights->GetRights() == PRights::GroupAdmin){
	SuperAdmin->setEnabled(false);
	GroupAdmin->setEnabled(false);
	
	GroupSelect->setEnabled(false);	
	}
	
	GroupSelect->setCurrentIndex(UserRights->GetGroup());
		
	mainlayout->addWidget(SuperAdmin);	
	mainlayout->addWidget(GroupAdmin);
	mainlayout->addWidget(StandardUser);
	mainlayout->addWidget(lab);
	mainlayout->addWidget(GroupSelect);

	PageWidget->setLayout(mainlayout);
	return(PageWidget);

}


void editwizard::WizzardClose(void){

ScanningActive = false;
CurrentUser->dbReconnect();
RefreshData();
}


void editwizard::RefreshData(void){
	//Pin ???
	CurrentUser->ObtainUserData();
	QChar permissions = (CurrentUser->GetRights());	
	if(permissions == PRights::SuperAdmin)
		SuperAdmin->setChecked(true);
	else if(permissions == PRights::GroupAdmin)
		GroupAdmin->setChecked(true);
	else 
		StandardUser->setChecked(true);
	NameEdit->setText(CurrentUser->GetName());
	SurnameEdit->setText(CurrentUser->GetSurname());
	FprDate->setText("Aktuální otisk sejmut:\n" + CurrentUser->GetFprDate());
	RfidEdit->setText(CurrentUser->GetRFID());
	GroupSelect->setCurrentIndex(CurrentUser->GetGroup());

}
