#ifndef COMMIT_DIALOG_H
#define COMMIT_DIALOG_H

#include <QtGui>
#include "ui_commitDialog.h"
#include <cassert>

///Dialog which is displayed during trade to commit trade
class CommitDialog : public QDialog
{
    Q_OBJECT

private:
	Ui::CommitDialog ui_;

	 QIcon okIcon;
	 QIcon notokIcon;
	 QIcon waitIcon;
	 QIcon invitationIcon;
signals:
	void submitSignal();
	void cancelSignal();
private slots:
	void submitButtonClickedSlot(bool checked) {
		emit submitSignal();
	}

	void cancelButtonClickedSlot(bool checked) {
		emit cancelSignal();
	}
public:
	virtual void show() {
		QDialog::show();
	}
private:
	void showIfHidden() {
		if (!isVisible()) {
			show();
			raise();
			activateWindow();
		}
	}

	void showOnlyCloseButton() {
		ui_.cancelButton->setVisible(false);

		ui_.submitButton->setVisible(true);
		ui_.submitButton->setText(QString::fromUtf16(L"Zavřít okno"));
		ui_.submitButton->setGeometry((ui_.mainFrame->width() - ui_.submitButton->width()) / 2, ui_.submitButton->y(),
			ui_.submitButton->width(), ui_.submitButton->height());	
	}

	void showBothButtons() {
		ui_.cancelButton->setVisible(true);
		ui_.cancelButton->setText(QString::fromUtf16(L"Ne, pokračovat v nabídce"));
		ui_.cancelButton->setGeometry(10, ui_.cancelButton->y(), 
			ui_.cancelButton->width(), ui_.cancelButton->height());		

		ui_.submitButton->setVisible(true);
		ui_.submitButton->setText(QString::fromUtf16(L"Ano, uzavřít a uskutečnit obchod"));
		ui_.submitButton->setGeometry(205, ui_.submitButton->y(),
			ui_.submitButton->width(), ui_.submitButton->height());		
	}
public:
	CommitDialog(QWidget * parent) : QDialog(parent) {
		ui_.setupUi(this);

		bool ok = QObject::connect(ui_.submitButton, SIGNAL(clicked(bool)), this, SLOT(submitButtonClickedSlot(bool)));
		ok = ok && QObject::connect(ui_.cancelButton, SIGNAL(clicked(bool)), this, SLOT(cancelButtonClickedSlot(bool)));

		assert(ok && "Signals not connected");

        okIcon.addFile(QString::fromUtf8(":/pictures/images/trade/ok-big.png"), QSize(), QIcon::Normal, QIcon::Off);
		notokIcon.addFile(QString::fromUtf8(":/pictures/images/trade/not-ok-big.png"), QSize(), QIcon::Normal, QIcon::Off);
		waitIcon.addFile(QString::fromUtf8(":/pictures/images/trade/wait-icon.png"), QSize(), QIcon::Normal, QIcon::Off);
		invitationIcon.addFile(QString::fromUtf8(":/pictures/images/trade/email-icon.png"), QSize(), QIcon::Normal, QIcon::Off);
	}

	void setTitle(const std::wstring & value) {
		ui_.titleLabel->setText(QString::fromUtf16(value.c_str()));
	} /**<Sets message box title. */

	void setTitle(const std::string & value) {
		ui_.titleLabel->setText(QString::fromUtf8(value.c_str()));
	} /**<Sets message box title. */

	void setMessage(const std::wstring & value) {
		ui_.messageLabel->setText(QString::fromUtf16(value.c_str()));
	} /**<Sets message string. */

	void setMessage(const std::string & value) {
		ui_.messageLabel->setText(QString::fromUtf8(value.c_str()));
	} /**<Sets message string. */

	void setReason(const std::wstring & value) {
		if (value != L"") {
			ui_.reasonLabel->setVisible(true);
			ui_.reasonLabel->setText(QString::fromUtf16(value.c_str()));
		}
		else {
			ui_.reasonLabel->setVisible(false);
		}

	} /**<Sets reason string. */

	void setAsDoneSuccess() {
		ui_.selfButton->setVisible(true);
		ui_.selfButton->setIcon(okIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfLabel->setVisible(true);

		ui_.serverButton->setVisible(true);
		ui_.serverButton->setIcon(okIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(okIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Uzavření obchodu proběhlo úspěšně, nabídky se úspěšně uskutečnily.");
		setReason(L"Obchod byl úspěšně uzavřen");

		showOnlyCloseButton();
		showIfHidden();
	}

	void setAsPartnerRejected() {
		ui_.selfButton->setVisible(true);
		ui_.selfButton->setIcon(okIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfButton->setVisible(true);

		ui_.serverButton->setVisible(true);
		ui_.serverButton->setIcon(okIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(notokIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Uzavření obchodu neproběhlo, protihráč odmítnul nabídku.");
		setReason(L"Obchod neproběhl");

		showOnlyCloseButton();
		showIfHidden();
	}

	void setAsSelfRejected() {
		ui_.selfButton->setVisible(true);
		ui_.selfButton->setIcon(okIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfButton->setVisible(true);

		ui_.serverButton->setVisible(true);
		ui_.serverButton->setIcon(okIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(notokIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Odmítnul jsi protihráčovu žádost o uzavření obchodu, obchod byl uskutečněn.");
		setReason(L"Obchod neproběhl");

		showOnlyCloseButton();
		showIfHidden();
	}

	void setAsDoneFailure(const std::wstring & reason) {
		ui_.selfButton->setVisible(true);
		ui_.selfButton->setIcon(okIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfButton->setVisible(true);

		ui_.serverButton->setVisible(true);
		ui_.serverButton->setIcon(notokIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(okIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Uzavření obchodu neproběhlo, protože byly porušeny některé podmínky.");
		setReason(reason);

		showOnlyCloseButton();
		showIfHidden();
	}

	void setAsWaitForPartnerDecision() {
		ui_.selfButton->setVisible(true);
        ui_.selfButton->setIcon(okIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfButton->setVisible(true);

		ui_.serverButton->setVisible(true);
        ui_.serverButton->setIcon(waitIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(waitIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Poslal jsi žádost o uzavření obchodu, počkej na rozhodnutí protihráče a serveru.");
		setReason(L"");

		showOnlyCloseButton();
		showIfHidden();
	}

	void setAsInvitation() {
		ui_.selfButton->setVisible(true);
        ui_.selfButton->setIcon(waitIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfButton->setVisible(true);

		ui_.serverButton->setVisible(true);
        ui_.serverButton->setIcon(waitIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(okIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Protihráč chce uzavřít obchod a provést přesun podle vytvořených nabídek. Souhlasíš?");
		setReason(L"");

		showBothButtons();
		showIfHidden();
	}

	void setAsWaitForAcceptResponse() {
		ui_.selfButton->setVisible(true);
        ui_.selfButton->setIcon(okIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfButton->setVisible(true);

		ui_.serverButton->setVisible(true);
        ui_.serverButton->setIcon(waitIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(okIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Přijmul jsi protihráčovu žádost o uzavření obchodu, počkej na rozhodnutí od serveru.");
		setReason(L"");

		showOnlyCloseButton();
		showIfHidden();
	}

	void setAsWaitForRejectResponse() {
		ui_.selfButton->setVisible(true);
        ui_.selfButton->setIcon(okIcon);
        ui_.selfButton->setIconSize(QSize(60, 60));
		ui_.selfButton->setVisible(true);

		ui_.serverButton->setVisible(true);
        ui_.serverButton->setIcon(waitIcon);
        ui_.serverButton->setIconSize(QSize(60, 60));
		ui_.serverButton->setVisible(true);

		ui_.partnerButton->setVisible(true);
        ui_.partnerButton->setIcon(notokIcon);
        ui_.partnerButton->setIconSize(QSize(60, 60));
		ui_.partnerButton->setVisible(true);

		setTitle(L"Uzavření obchodu");
		setMessage(L"Odmítnul jsi protihráčovu žádost o uzavření obchodu, počkej na potvrzení od serveru.");
		setReason(L"");

		showOnlyCloseButton();
		showIfHidden();
	}

	~CommitDialog() {};
};

#endif