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


groupwizard::groupwizard(QWidget *parent) 
{
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    setAttribute(Qt::WA_DeleteOnClose);
	
	QVBoxLayout* mainLayout = new QVBoxLayout;
	QHBoxLayout* buttonLayout = new QHBoxLayout;

	GroupList = new QTableWidget();

	NameEdit = new PLineEdit();
	CurrentGroup = new PGroupInfo();

QCommonStyle style;
	RenameButton = new QPushButton("&Přejmenovat", this);
	RenameButton->setIcon(style.standardIcon(QStyle::SP_FileDialogInfoView));
	
	InsertButton = new QPushButton("&Nová", this);
	InsertButton->setIcon(style.standardIcon(QStyle::SP_FileDialogNewFolder));
	
	DeleteButton = new QPushButton("&Smazat", this);
	DeleteButton->setIcon(style.standardIcon(QStyle::SP_DialogCancelButton));

	connect(RenameButton, SIGNAL(clicked()), this, SLOT(Rename()));
	connect(InsertButton, SIGNAL(clicked()), this, SLOT(Insert()));
	connect(DeleteButton, SIGNAL(clicked()), this, SLOT(Delete()));

	buttonLayout->addWidget(InsertButton);
	buttonLayout->addWidget(RenameButton);
	buttonLayout->addWidget(DeleteButton);	
	buttonLayout->setContentsMargins(6,3,6,8);
	
	
	
	GroupList->horizontalHeader()->setStretchLastSection(true);
	GroupList->verticalHeader()->hide();
	GroupList->horizontalHeader()->hide();
	GroupList->setSelectionBehavior(QAbstractItemView::SelectRows);
    GroupList->setColumnCount(1);
	GroupList->setSelectionMode(QAbstractItemView::SingleSelection);
	
	

	mainLayout->setContentsMargins(0,0,0,0);		
	mainLayout->addLayout(utility::CreateHeader(("    Editace Skupin"), "AddUser"));
	mainLayout->addWidget(GroupList);
	mainLayout->addLayout(buttonLayout);
	
	
    this->setLayout(mainLayout);
	this->LoadGroups();
	
utility::SetKeyboardMode(utility::KBMode_Alpha);
//connect(GroupList,SIGNAL(clicked(const QModelIndex &)),this,SLOT(groupSelected(const QModelIndex &)));


}

void groupwizard::keypressed(QByteArray &keyt){

if(NameEdit->hasFocus())
	NameEdit->Keypressed(keyt);
else if(keyt[1]=='#')
	this->Cancel();

}

void groupwizard::LoadGroups(){
QStringList groups = CurrentGroup->GetGroupList();
GroupList->clear();
GroupList->setRowCount(0);

	for(int i = 0; i < groups.size(); i++)
	{
		GroupList->insertRow(i);
		GroupList->setItem(i, 0,new QTableWidgetItem(groups.at(i)));
	}

}

void groupwizard::Delete(void){

int ret;
ret = utility::PixmapMessageBox("Smazat?","Skutečně smazat skupinu "+ GroupList->currentItem()->text() +" ?",QPixmap("/sdcard/UserWarning.png"),QMessageBox::Yes | QMessageBox::No);
if(ret == QMessageBox::No)
	return;
if(CurrentGroup->DeleteGroup(GroupList->currentRow()))
	QMessageBox::information(this,"Úspěch","Skupina smazána.", QMessageBox::Ok);
	this->LoadGroups();

}

void groupwizard::Insert(void){

if(GenerateInputDialog("Nová skupina","Zadejte název vytvářené skupiny :") == QDialog::Rejected)
	return;
if(CurrentGroup->AddGroup(NameEdit->text()))
	QMessageBox::information(this,"Úspěch","Skupina vytvořena.", QMessageBox::Ok);
 	this->LoadGroups();

}

void groupwizard::Rename(void){

if(GenerateInputDialog("Změna názvu","Zadejte nový název skupiny :") == QDialog::Rejected)
	return;
if(CurrentGroup->RenameGroup(GroupList->currentRow(),NameEdit->text()))
	QMessageBox::information(this,"Úspěch","Skupina přejmenována.", QMessageBox::Ok);
 this->LoadGroups();

}

void groupwizard::Cancel(void){

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

}

bool groupwizard::GenerateInputDialog(QString title,QString content){

	Getname = new QDialog(this);
	QVBoxLayout* dialogLayout = new QVBoxLayout;
	QHBoxLayout* buttonLayout = new QHBoxLayout;
	QPushButton* accept = new QPushButton("Potvrdit");
	QPushButton* storno = new QPushButton("Storno");
	buttonLayout -> addWidget(storno);
	buttonLayout -> addWidget(accept);	
	QLabel* lab = new QLabel(content);
	
	dialogLayout->addWidget(lab);
	dialogLayout->addWidget(NameEdit);
	dialogLayout->addLayout(buttonLayout);

	connect(accept,SIGNAL(clicked()),Getname,SLOT(accept()));	
	connect(storno,SIGNAL(clicked()),Getname,SLOT(reject()));

	Getname->setWindowTitle(title);
	Getname->setLayout(dialogLayout);
	return(Getname->exec());

	
}




