#ifndef LOGGED_WIDGET_H
#define LOGGED_WIDGET_H

#include <QtGui>
#include "ui_loggedWidget.h"
#include "softProj\clientServer\structs.h"

///Shows client connection state: logged or not logged. */
class LoggedWidget : public QWidget {
Q_OBJECT
private:
	Ui::LoggedWidget ui_;
	LoginDetails loginDetails_;
	LoginDetails previousLoginDetails;
	bool status_;
signals:
	void loginClickedSignal(LoginDetails & loginDetails);
	void unloginClickedSignal(LoginDetails & loginDetails);
public slots:
	void loginButtonClickedSlot(bool checked) {
		if (status_) {
			emit unloginClickedSignal(loginDetails_);
		}
		else {
			emit loginClickedSignal(loginDetails_);			
		}
	} /**< If not logged then log-in via given login user data, otherwise log-out */

public:
	LoggedWidget(QWidget * parent) : QWidget(parent) {
		ui_.setupUi(this);

		QObject::connect(ui_.loginButton, SIGNAL(clicked(bool)), this, SLOT(loginButtonClickedSlot(bool)));
		setStatus(false);

		loginDetails_.name = "";
		loginDetails_.password = "";
		loginDetails_.port = 0;
		loginDetails_.server = "";

		previousLoginDetails.name = "";
		previousLoginDetails.password = "";
		previousLoginDetails.port = 0;
		previousLoginDetails.server = "";
	}

	void setLogin(const LoginDetails & loginDetails) {
		this->previousLoginDetails = this->loginDetails_;
		this->loginDetails_ = loginDetails;		
	} /**< Sets user login data. */ 

	LoginDetails getLogin() const {
		return loginDetails_;
	}

	bool isLoginDifferentFromLastOne() {
		return (
			(loginDetails_.name != previousLoginDetails.name) &&
			(loginDetails_.server != previousLoginDetails.server) &&
			(loginDetails_.port != previousLoginDetails.port));
	}

	void setStatus(bool status) {		
		if (status) {
			ui_.statusLabel->setText(QString::fromUtf16(L"Přihlášen"));
			ui_.statusLabel->setStyleSheet("#statusLabel { padding-left: 5px; color: rgb(0, 152, 0); font-size: 9px; }");
			ui_.loginButton->setText(QString::fromUtf16(L"Odhlásit se"));
			ui_.nameLabel->setText(QString::fromUtf8(loginDetails_.name.c_str()));
		}
		else {
			ui_.statusLabel->setText(QString::fromUtf16(L"Nepřihlášen"));
			ui_.statusLabel->setStyleSheet("#statusLabel { padding-left: 5px; color: rgb(212, 0, 0); font-size: 9px; }");
			ui_.loginButton->setText(QString::fromUtf16(L"Přihlásit se"));
			ui_.nameLabel->setText(QString::fromUtf16(L"Nepřihlášen"));
		}

		this->status_ = status;
	} /**< Sets and shows user log state. */

	virtual void setEnabled(bool enabled) {
		QWidget::setEnabled(enabled);
	} /**< Activates and deactivates the widget. */
};

#endif