#ifndef ADD_CREW_DIALOG_H
#define ADD_CREW_DIALOG_H

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

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

private:
	Ui::AddCrewDialog ui_;
	crew_type crewType_;
	bool isAdding_;
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();
		}
	}
public:
	AddCrewDialog(QWidget * parent) : QDialog(parent) {
		ui_.setupUi(this);

		bool connected = true;
		connected = connected && connect(ui_.submitButton, SIGNAL(clicked(bool)), this, SLOT(submitButtonClickedSlot(bool)));
		connected = connected && connect(ui_.cancelButton, SIGNAL(clicked(bool)), this, SLOT(cancelButtonClickedSlot(bool)));
		assert(connected && "Signals not connected");	
	}

	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_.descriptionLabel->setText(QString::fromUtf16(value.c_str()));
	} /**<Sets message string. */

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

	~AddCrewDialog() {};

	void setDialog(const crew_status & state, crew_type crewType, bool isAdding) {
		unsigned long ableCount = 0;
		unsigned long woundedCount = 0;

		switch (crewType) {
		case CREW_TYPE_CAPTAIN:
			ableCount = state.ABLE_CREW_CAPITAN;
			woundedCount = state.WOUNDED_CREW_CAPITAN;
			break;
		case CREW_TYPE_PILOTS:
			ableCount = state.ABLE_CREW_PILOT;
			woundedCount = state.WOUNDED_CREW_PILOT;
			break;
		case CREW_TYPE_ENGINEERS:
			ableCount = state.ABLE_CREW_ENGINEER;
			woundedCount = state.WOUNDED_CREW_ENGINEER;
			break;
		case CREW_TYPE_SCIENTISTS:
			ableCount = state.ABLE_CREW_SCIENTIST;
			woundedCount = state.WOUNDED_CREW_SCIENTIST;
			break;
		case CREW_TYPE_OFFICERS:
			ableCount = state.ABLE_CREW_OFFICER;
			woundedCount = state.WOUNDED_CREW_OFFICER;
			break;
		case CREW_TYPE_DOCTORS:
			ableCount = state.ABLE_CREW_DOCTOR;
			woundedCount = state.WOUNDED_CREW_DOCTOR;
			break;
		case CREW_TYPE_PASSENGERS:
			ableCount = state.ABLE_CREW_PASSENGER;
			woundedCount = state.WOUNDED_CREW_PASSENGER;
			break;
		}

		if (isAdding) {
			ui_.ableStackedWidget->setCurrentWidget(ui_.addAblePage);
			ui_.woundedStackedWidget->setCurrentWidget(ui_.addWoundedPage);

			ui_.descriptionLabel->setText(QString::fromUtf16(L"Kolik členů posádky chceš přidat?"));
			ui_.ableLabel->setText(QString::fromUtf16(L"Kolik přidat zdravých členů"));
			ui_.woundedLabel->setText(QString::fromUtf16(L"Kolik přidat zraněných členů"));
			ui_.submitButton->setText(QString::fromUtf16(L"Potvrdit a přidat"));
			ui_.cancelButton->setText(QString::fromUtf16(L"Zrušit a nepřidávat"));
		}
		else {
			ui_.ableStackedWidget->setCurrentWidget(ui_.removeAblePage);
			ui_.woundedStackedWidget->setCurrentWidget(ui_.removeWoundedPage);

			ui_.descriptionLabel->setText(QString::fromUtf16(L"Kolik členů posádky chceš odebrat?"));
			ui_.ableLabel->setText(QString::fromUtf16(L"Kolik odebrat zdravých členů"));
			ui_.woundedLabel->setText(QString::fromUtf16(L"Kolik odebrat zraněných členů"));
			ui_.submitButton->setText(QString::fromUtf16(L"Potvrdit a odebrat"));
			ui_.cancelButton->setText(QString::fromUtf16(L"Zrušit a neodebírat"));

			ui_.ableSlider->setMinimum(0);
			ui_.ableSlider->setMaximum(ableCount);

			ui_.woundedSlider->setMinimum(0);
			ui_.woundedSlider->setMaximum(woundedCount);
		}

		this->crewType_ = crewType;
		this->isAdding_ = isAdding;
	}

	unsigned long getAble() const {
		if (isAdding_) {
			return ui_.ableEdit->text().toULong();
		}
		else {
			return ui_.ableSlider->getValue();
		}
	}

	unsigned long getWounded() const {
		if (isAdding_) {
			return ui_.woundedEdit->text().toULong();
		}
		else {
			return ui_.woundedSlider->getValue();
		}
	}

	crew_type getCrewType() const {
		return crewType_;
	}

	bool isAdding() {
		return isAdding_;
	}
};

#endif