#ifndef UNMOUNT_DIALOG_H
#define UNMOUNT_DIALOG_H

#include <QtGui>
#include "ui_unmountDialog.h"
#include "softProj\settings.h"
#include "softProj\commonTypes.h"
#include <cassert>

enum EUnmountType {
	UNMOUNT_TYPE_SEPARATE, UNMOUNT_TYPE_CONCRETE, UNMOUNT_TYPE_STACKABLE,
};

///Dialog which is displayed during unmounting item
class UnmountDialog : public QDialog
{
    Q_OBJECT

private:
	Ui::UnmountDialog ui_;
	EUnmountType unmountType_;

	std::string patternName_;
	id_t itemId_;

	QIcon okIcon;
	QIcon notokIcon;
	QIcon waitIcon;

	void showIfHidden() {
		if (!isVisible()) {
			show();
			raise();
			activateWindow();
		}
	}

	bool isCanceled_;
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();
	}
public:
	UnmountDialog(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);

		ui_.workingSlider->setFontColor(QColor::fromRgb(40, 40, 40));
		ui_.damagedSlider->setFontColor(QColor::fromRgb(40, 40, 40));
	}

	void setTitle(const std::wstring & title) {
		ui_.titleLabel->setText(QString::fromUtf16(title.c_str()));
	}

	void setMessage(const std::wstring & message) {
		ui_.desciptionLabel->setText(QString::fromUtf16(message.c_str()));
	}

	void setDialog(const std::string & patternName, unsigned long long workingMax, unsigned long long damagedMax) {
		ui_.resultImageButton->setVisible(false);

		ui_.workingSlider->setMinimum(0);
		ui_.workingSlider->setMaximum(workingMax);
		ui_.workingSlider->setVisible(true);
		ui_.workingLabel->setVisible(true);

		ui_.damagedSlider->setMinimum(0);
		ui_.damagedSlider->setMaximum(damagedMax);
		ui_.damagedSlider->setVisible(true);
		ui_.damagedLabel->setVisible(true);		

		this->patternName_ = patternName;

		setTitle(L"Odmontování zařízení " + Settings::instance().getGameName(patternName));
		setMessage(L"Opravdu chceš zařízení odmontovat? Pokud ano, tak zadej požadované množství.");

		ui_.cancelButton->setVisible(true);
		ui_.cancelButton->setGeometry(45, 215, ui_.cancelButton->width(), ui_.cancelButton->height());
		ui_.cancelButton->setText(QString::fromUtf16(L"Zrušit, nic neměnit"));

		ui_.submitButton->setVisible(true);
		ui_.submitButton->setGeometry(205, 215, ui_.submitButton->width(), ui_.submitButton->height());
		ui_.submitButton->setText(QString::fromUtf16(L"Potvrdit a odmontovat"));

		isCanceled_ = false;
		unmountType_ = UNMOUNT_TYPE_STACKABLE;

		showIfHidden();
	}

	void setDialog(const std::string & patternName) {
		ui_.resultImageButton->setVisible(false);

		ui_.workingSlider->setVisible(false);
		ui_.workingLabel->setVisible(false);
		ui_.damagedSlider->setVisible(false);
		ui_.damagedLabel->setVisible(false);

		setTitle(L"Odmontování zařízení " + Settings::instance().getGameName(patternName));
		setMessage(L"Opravdu chceš zařízení odmontovat?");

		this->patternName_ = patternName;

		ui_.cancelButton->setVisible(true);
		ui_.cancelButton->setGeometry(45, 215, ui_.cancelButton->width(), ui_.cancelButton->height());
		ui_.cancelButton->setText(QString::fromUtf16(L"Zrušit, nic neměnit"));

		ui_.submitButton->setVisible(true);
		ui_.submitButton->setGeometry(205, 215, ui_.submitButton->width(), ui_.submitButton->height());
		ui_.submitButton->setText(QString::fromUtf16(L"Potvrdit a odmontovat"));

		isCanceled_ = false;
		unmountType_ = UNMOUNT_TYPE_SEPARATE;

		showIfHidden();
	}

	void setDialog(const std::string & patternName, id_t itemId) {
		ui_.resultImageButton->setVisible(false);

		ui_.workingSlider->setVisible(false);
		ui_.workingLabel->setVisible(false);
		ui_.damagedSlider->setVisible(false);
		ui_.damagedLabel->setVisible(false);

		setTitle(L"Odmontování zařízení " + Settings::instance().getGameName(patternName));
		setMessage(L"Opravdu chceš zařízení odmontovat?");

		this->patternName_ = patternName;
		this->itemId_ = itemId;

		ui_.cancelButton->setVisible(true);
		ui_.cancelButton->setGeometry(45, 215, ui_.cancelButton->width(), ui_.cancelButton->height());
		ui_.cancelButton->setText(QString::fromUtf16(L"Zrušit, nic neměnit"));

		ui_.submitButton->setVisible(true);
		ui_.submitButton->setGeometry(205, 215, ui_.submitButton->width(), ui_.submitButton->height());
		ui_.submitButton->setText(QString::fromUtf16(L"Potvrdit a odmontovat"));

		isCanceled_ = false;

		unmountType_ = UNMOUNT_TYPE_CONCRETE;

		showIfHidden();
	}

	void setAsDoneSuccess(const std::string & patternName, const std::string & mountingResult) {
		ui_.resultImageButton->setVisible(true);
        ui_.resultImageButton->setIcon(okIcon);
        ui_.resultImageButton->setIconSize(QSize(40, 40));		

		ui_.workingSlider->setVisible(false);
		ui_.workingLabel->setVisible(false);
		ui_.damagedSlider->setVisible(false);
		ui_.damagedLabel->setVisible(false);

		setTitle(L"Odmontování zařízení " + Settings::instance().getGameName(patternName));
		setMessage(L"Server potvrdil žádost o odmontování předmětu.\n" + Settings::instance().getGameName(mountingResult) );	

		ui_.cancelButton->setVisible(false);

		ui_.submitButton->setVisible(true);
		ui_.submitButton->setGeometry(120, 215, ui_.submitButton->width(), ui_.submitButton->height());
		ui_.submitButton->setText(QString::fromUtf16(L"Zavřít okno"));

		isCanceled_ = true;

		showIfHidden();
	}

	void setAsDoneFail(const std::string & patternName, const std::string & mountingResult) {
		ui_.resultImageButton->setVisible(true);
        ui_.resultImageButton->setIcon(notokIcon);
        ui_.resultImageButton->setIconSize(QSize(40, 40));		

		ui_.workingSlider->setVisible(false);
		ui_.workingLabel->setVisible(false);
		ui_.damagedSlider->setVisible(false);
		ui_.damagedLabel->setVisible(false);

		setMessage(L"Server potvrdil žádost o odmontování předmětu.\n" + Settings::instance().getGameName(mountingResult) );	

		ui_.cancelButton->setVisible(false);

		ui_.submitButton->setVisible(true);
		ui_.submitButton->setGeometry(120, 215, ui_.submitButton->width(), ui_.submitButton->height());
		ui_.submitButton->setText(QString::fromUtf16(L"Zavřít okno"));

		isCanceled_ = true;

		showIfHidden();
	}

	unsigned long long getWorking() const {
		return ui_.workingSlider->getValue();
	}

	unsigned long long getDamaged() const {
		return ui_.damagedSlider->getValue();
	}

	std::string getPatternName() const {
		return patternName_;
	}

	id_t getItemId() const {
		return itemId_;
	}

	~UnmountDialog() {};

	bool isCanceled() const {
		return isCanceled_;
	}

	EUnmountType getUnmountType() {
		return unmountType_;
	}
};

#endif