
#include <QtGui>


#include "vizualizace.h"



vizualizace::vizualizace(QWidget *parent)

    : QWidget(parent)

{

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

const QString days[]={"Pondělí","Úterý","Středa","Čtvrtek","Pátek","Sobota","Neděle"};
//! [6]
	setAttribute(Qt::WA_DeleteOnClose);
   
	clock = new AnalogClock(this);

	QGridLayout *mainLayout = new QGridLayout;
    	label = new QLabel(("<font color='green'>Identifikujte se, prosím.</font>"));

	QLabel *lab2 = new QLabel(days[(QDate::currentDate().dayOfWeek())-1] + QDateTime::currentDateTime().toString("\nd.M.yyyy"));

	QLabel *lab4 = new QLabel();
	progressbar = new QProgressBar(this);  
	timer = new QTimer(this);  
	inpipe = new QFile("/var/pipes/prstd_out");  

	QFont f( "Sans", 14, QFont::Bold);
        label->setFont(f);
	
	SpecRequest=0;
	
	progressbar -> setTextVisible(false);
	
	connect(timer, SIGNAL(timeout()), this, SLOT(TimTick()));
	progressbar->setMinimum(0);
	progressbar->setMaximum(100);
	progressbar->setVisible(false);
	lab4->setPixmap(QPixmap("/sdcard/logo.png"));

	mainLayout->addWidget(lab2, 0, 0,Qt::AlignLeft | Qt::AlignTop);
     	mainLayout->addWidget(lab4, 0, 1,Qt::AlignHCenter  | Qt::AlignTop);
     	mainLayout->addWidget(&(clock->ActualTime), 0, 2,Qt::AlignRight  | Qt::AlignTop);
     	mainLayout->addWidget(clock, 1, 0,1,3,Qt::AlignHCenter);
	mainLayout->addWidget(progressbar, 1, 0,1,3,Qt::AlignHCenter);
	
    	mainLayout->addWidget(label, 2, 0,1,3,Qt::AlignHCenter | Qt::AlignBottom);

	setLayout(mainLayout);
	
	inpipe->open(QFile::ReadOnly);
	
	notifuck = new QSocketNotifier(inpipe->handle(), QSocketNotifier::Read);

	notifuck->setEnabled(true);

  	connect(notifuck, SIGNAL(activated(int)), this, SLOT(IncomingDataSlot()));


	utility::SetKeyboardMode(utility::KBMode_Leaving);
	utility::LogMessage(utility::Log_Debug,(char*)"VIZUALIZACE Running");

}




void vizualizace::IncomingDataSlot(void)
 { //static int i;

IncomingData = inpipe->readLine().trimmed();

if(currwiz){
	emit DataForWindow(IncomingData);
	return;
}
	if(IncomingData[0]=='$'){
			QString leaving;
			switch(IncomingData[1]){
			case '0' :
			leaving = "Oběd";
			break;
			case '1' :
			leaving = "Pauza";
			break;
			case '2' :
			leaving = "Dovolená";
			break;
			case '3' :
			leaving = "Lékař";
			break;
			case '4' :
			leaving = "Služebně";
			break;
			case '5' :
			leaving = "Administrace systému";
			SpecRequest=1;
			break;
			}
		this->label->setText("<font color='blue'>" + leaving + "</font>");
		progressbar->setVisible(true);	
		clock->hide();	
		timer->start(50);
		return;
		
		
	}
	if(timer->isActive()){
		UserRights.LoadRights(IncomingData.left(3));
		if(SpecRequest && UserRights.isAdmin()){  //Uses has permission
			SpecRequest=0;
			TimTick();
			this->ShowMenu(IncomingData.left(2));
			return;
		}
		else if(SpecRequest){
			SpecRequest=0;
			TimTick();
			this->label->setText("<font color='brown'>K operaci nemáte oprávnění !</font>");
		}
		else
		this->label->setText(IncomingData.mid(2) + " - " +label->text());
	}			
	else{	
	this->label->setText(IncomingData.mid(2)); 
	timer->start(2000);
	}
	
}

void vizualizace::TimTick(void){


if(!(progressbar->isVisible())){
timer->stop();
this->label->setText("<font color='red'>Identifikujte se prosím</font>");
return;
}

int value = progressbar->value();
if(value>=98){
timer->stop();
progressbar->setVisible(false);
SpecRequest=0;
clock->show();

this->label->setText("<font color='red'>Přístupový registrační systém</font>");
progressbar->reset();

}

else
progressbar->setValue(value + 1);

}

void vizualizace::ShowMenu(QByteArray rights){
int ret;
menu mymenu;

mymenu.SetPermissions(UserRights.GetRights());



mymenu.showFullScreen();
ret=mymenu.exec();


switch (ret){
	case 1 :
		currwiz = new addwizard(0,&UserRights);
		currwiz->showFullScreen();	
	break;
	case 2 :
		currwiz = new editwizard(&UserRights);
		currwiz->showFullScreen();	
	break;
	case 3 :
		currwiz = new groupwizard();
		currwiz->showFullScreen();	
	break;
}
connect(this, SIGNAL(DataForWindow(QByteArray&)), currwiz, SLOT(keypressed(QByteArray&)));
connect(currwiz, SIGNAL(destroyed()), this, SLOT(WizzardClose()));



}

void vizualizace::WizzardClose(void){
//clock = new AnalogClock();
//	clock->show();
currwiz = NULL;

}
