#ifndef ACCOMMODATIONS_DIALOG_H
#define ACCOMMODATIONS_DIALOG_H

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

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

private:
	Ui::AddRoomDialog ui_;
	unsigned long price_;
	unsigned long totalPoints_;
	accommodation_type accommodationType_;
signals:
	void submitSignal();
	void cancelSignal();

private slots:
	void submitButtonClickedSlot(bool checked) {
		emit submitSignal();
	}

	void cancelButtonClickedSlot(bool checked) {
		emit cancelSignal();
	}

	void countChangedSlot(unsigned long long count) {
		unsigned long freePoints = totalPoints_ - price_ * count;
		ui_.pointsValue->setText(Settings::instance().formatNumber(freePoints));
	}
public:
	virtual void show() {
		QDialog::show();
	}
private:
	void showIfHidden() {
		if (!isVisible()) {
			show();
			raise();
			activateWindow();
		}
	}

public:
	AddRoomDialog(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)));
		connected = connected && connect(ui_.countSlider, SIGNAL(valueChangedSignal(unsigned long long)), this, SLOT(countChangedSlot(unsigned long long)));
		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. */

	~AddRoomDialog() {};

	void setDialog(const accommodation_status & state, accommodation_type accommodationType) {
		unsigned long freePoints = state.totalPoints - state.consumedPoints;
		ui_.pointsValue->setText(Settings::instance().formatNumber(freePoints));

		ui_.countSlider->setMinimum(0);

		price_ = 0;

		switch (accommodationType) {
		case ACCOMMODATION_COMFORTABLE_ROOM:
			price_ = state.comfortableRoomsPrice;
			break;
		case ACCOMMODATION_COMMON_CABIN:
			price_ = state.commonCabinsPrice;
			break;
		case ACCOMMODATION_LUXURY_APPARTEMENT:
			price_ = state.luxuryAccommodationsPrice;
			break;
		case ACCOMMODATION_WAREHOUSE_WITH_LIFE_SUPPORT:
			price_ = state.warehousesWithLifeSupportPrice;
			break;
		}

		if (price_ != 0) {	
			ui_.countSlider->setMaximum(freePoints / price_);
		}

		// At the beginning we have all free points
		totalPoints_ = freePoints;

		ui_.countSlider->setValue(0);
		ui_.priceValue->setText(Settings::instance().formatNumber(price_));

		this->accommodationType_ = accommodationType;
	}

	unsigned long getRoomCount() {
		return ui_.countSlider->getValue();
	}

	accommodation_type getAccommodationType() {
		return accommodationType_;
	}
};

#endif