#ifndef TRADE_INVITATION_DIALOG_H
#define TRADE_INVITATION_DIALOG_H

#include <QtGui>
#include "ui_tradeInvitationDialog.h"
#include "tradeItem.h"
#include <cassert>

///ConfirmDialog used to submit or cancel important actions.
class TradeInvitationDialog : public QDialog {
	Q_OBJECT

private:
	Ui::TradeInvitationDialog ui_;
	trade_invitation_data invitation_;
	bool isTradeCanceled_;

	QIcon okIcon;
	QIcon notokIcon;
	QIcon waitIcon;
	QIcon invitationIcon;

	void showIfHidden() {
		if (!isVisible()) {
			show();
			raise();
			activateWindow();
		}
	}
signals:
	void submitSignal();
	void cancelSignal();
	void postponeSignal();
private slots:
	void cancelButtonClickedSlot(bool checked) {
		emit cancelSignal();
	}

	void submitButtonClickedSlot(bool checked) {
		emit submitSignal();
	}

	void postponeButtonClickedSlot(bool checked) {
		emit postponeSignal();
	}
public:
	TradeInvitationDialog(QWidget * parent) : QDialog(parent) {
		ui_.setupUi(this);

		bool connected = true;

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

        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);

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

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

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

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

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

	void setCancelButton(const std::wstring & value) {
		ui_.cancelButton->setText(QString::fromUtf16(value.c_str()));
	}

	void setDialog(const trade_invitation_data & invitation, bool isOwned) {
		ui_.tradeSelfButton->setPartner(invitation.hostFortressId, invitation.hostFortressName,
			invitation.hostName, true, invitation.hostIsShip);
		
		ui_.tradePartnerButton->setPartner(invitation.invitedFortressId, invitation.invitedFortressName,
			invitation.invitedName, isOwned, invitation.invitedIsShip);

		this->invitation_ = invitation;

		ui_.postponeButton->setVisible(true);
		ui_.submitButton->setVisible(true);

		setMessage(L"Přišla ti pozvánka na zahájení obchodování s dalším hráčem");
		setCancelButton(L"Odmítnout pozvánku");

		ui_.selfImageButton->setIcon(invitationIcon);
        ui_.selfImageButton->setIconSize(QSize(60, 60));

		ui_.partnerImageButton->setIcon(okIcon);
        ui_.partnerImageButton->setIconSize(QSize(60, 60));

		isTradeCanceled_ = false;

		showIfHidden();
	}
	
	const trade_invitation_data & getInvitation() const {
		return invitation_;
	}

	void setAsPartnerCanceled() {
		ui_.postponeButton->setVisible(false);
		ui_.submitButton->setVisible(false);

		setMessage(L"Protihráč zrušil pozvánku.");
		setCancelButton(L"Zavřít okno");

		// Do not change self image, it can have arbitrary state now

		ui_.partnerImageButton->setIcon(notokIcon);
        ui_.partnerImageButton->setIconSize(QSize(60, 60));

		isTradeCanceled_ = true;

		showIfHidden();
	}

	void setAsPartnerRejected() {
		ui_.postponeButton->setVisible(false);
		ui_.submitButton->setVisible(false);

		setMessage(L"Protihráč tvou pozvánku odmítnul, obchod se nezahájí.");
		setCancelButton(L"Zavřít okno");

		ui_.partnerImageButton->setIcon(okIcon);
        ui_.partnerImageButton->setIconSize(QSize(60, 60));

		ui_.partnerImageButton->setIcon(notokIcon);
        ui_.partnerImageButton->setIconSize(QSize(60, 60));

		isTradeCanceled_ = true;

		showIfHidden();
	}

	void setAsSelfRejected() {
		ui_.postponeButton->setVisible(false);
		ui_.submitButton->setVisible(false);

		setMessage(L"Odmítnul jsi obdrženou pozvánku, obchod se nezahájí.");
		setCancelButton(L"Zavřít okno");

		ui_.selfImageButton->setIcon(notokIcon);
        ui_.selfImageButton->setIconSize(QSize(60, 60));

		ui_.partnerImageButton->setIcon(notokIcon);
        ui_.partnerImageButton->setIconSize(QSize(60, 60));

		isTradeCanceled_ = true;

		showIfHidden();
	}

	void setAsSelfCanceled() {
		ui_.postponeButton->setVisible(false);
		ui_.submitButton->setVisible(false);

		setMessage(L"Server potrvrdil tvou žádost o zrušení tvé pozvánky. Obchodování bylo zrušeno.");
		setCancelButton(L"Zavřít okno");

		// Do not change partner image, it can have arbitrary state now

		ui_.selfImageButton->setIcon(notokIcon);
        ui_.selfImageButton->setIconSize(QSize(60, 60));

		isTradeCanceled_ = true;

		showIfHidden();
	}

	void setAsWaitForPartnerDecision() {
		ui_.postponeButton->setVisible(false);
		ui_.submitButton->setVisible(false);

		setMessage(L"Nyní musíš čekat na potvrzení od protihráče. Pokud si to rozmyslíš, můžeš kdykoliv pozvánku na obchodování zruši.");
		setCancelButton(L"Zrušit pozvánku");

		ui_.selfImageButton->setIcon(okIcon);
        ui_.selfImageButton->setIconSize(QSize(60, 60));

		ui_.partnerImageButton->setIcon(waitIcon);
        ui_.partnerImageButton->setIconSize(QSize(60, 60));

		isTradeCanceled_ = false;

		showIfHidden();
	}

	void setAsWaitForPartnerDecisionServerReceived() {
		ui_.postponeButton->setVisible(false);
		ui_.submitButton->setVisible(false);

		setMessage(L"Server přijmul pozvánku, počkej na potvrzení od protihráče.");
		setCancelButton(L"Zrušit pozvánku");

		ui_.selfImageButton->setIcon(okIcon);
        ui_.selfImageButton->setIconSize(QSize(60, 60));

		ui_.partnerImageButton->setIcon(waitIcon);
        ui_.partnerImageButton->setIconSize(QSize(60, 60));

		isTradeCanceled_ = false;

		showIfHidden();
	}

	bool isTradeCanceled() {
		return isTradeCanceled_;
	}
};

#endif