#ifndef TRADE_OFFER_DIALOG_H
#define TRADE_OFFER_DIALOG_H

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

///Dialog which is displayed during trade to submit od cancel the trade offer.
class TradeOfferDialog : public QDialog
{
    Q_OBJECT

private:
	Ui::TradeOfferDialog ui_;
	TradeItem * item_;
signals:
	void submitSignal();
	void cancelSignal();

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

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

	void offerAllButtonClickedSlot(bool checked) {
		ui_.offerWorkingSlider->setValue(item_->getWarehouseItem().working);

		if (item_->type == STACKABLE_TRADE_ITEM) {
			ui_.offerDamagedSlider->setValue(item_->getWarehouseItem().damaged);
		}

		emit submitSignal();
	}
public:
	virtual void show() {
		QDialog::show();
	}
public:
	TradeOfferDialog(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)));
		ok = ok && QObject::connect(ui_.offerAllButton, SIGNAL(clicked(bool)), this, SLOT(offerAllButtonClickedSlot(bool)));

		ui_.offerWorkingSlider->setFontColor(QColor::fromRgb(40, 40, 40));
		ui_.offerDamagedSlider->setFontColor(QColor::fromRgb(40, 40, 40));

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

	void setItem(TradeItem * item) {
		this->item_ = item;

		if (item->type == STACKABLE_TRADE_ITEM) {
			ui_.offerWorkingSlider->setMinimum(0);
			ui_.offerWorkingSlider->setMaximum(item_->getWarehouseItem().working);

			ui_.offerDamagedSlider->setMinimum(0);
			ui_.offerDamagedSlider->setMaximum(item_->getWarehouseItem().damaged);

			ui_.offerDamagedSlider->setVisible(true);
			ui_.offerDamagedLabel->setVisible(true);

			ui_.offerWorkingSlider->setGeometry(5, ui_.offerWorkingSlider->y(),
				ui_.offerWorkingSlider->width(), ui_.offerWorkingSlider->height());

			ui_.offerWorkingLabel->setGeometry(5, ui_.offerWorkingLabel->y(),
				ui_.offerWorkingLabel->width(), ui_.offerWorkingLabel->height());

			ui_.offerWorkingLabel->setText(QString::fromUtf16(L"Nabídnout nepoškozených"));

			ui_.offerDamagedSlider->setGeometry(ui_.mainFrame->width() - ui_.offerDamagedSlider->width() - 5, ui_.offerDamagedSlider->y(),
				ui_.offerDamagedSlider->width(), ui_.offerDamagedSlider->height());

			ui_.offerDamagedLabel->setGeometry(ui_.mainFrame->width() - ui_.offerDamagedLabel->width() - 5, ui_.offerDamagedLabel->y(),
				ui_.offerDamagedLabel->width(), ui_.offerDamagedLabel->height());

			ui_.offerDamagedLabel->setText(QString::fromUtf16(L"Nabídnout poškozených"));

		}
		else {
			// Show just working count slider
			ui_.offerWorkingSlider->setMinimum(0);
			ui_.offerWorkingSlider->setMaximum(item_->getWarehouseItem().working);

			ui_.offerDamagedSlider->setVisible(false);
			ui_.offerDamagedLabel->setVisible(false);

			ui_.offerWorkingSlider->setGeometry((ui_.mainFrame->width() - ui_.offerWorkingSlider->width()) / 2, ui_.offerWorkingSlider->y(),
				ui_.offerWorkingSlider->width(), ui_.offerWorkingSlider->height());

			ui_.offerWorkingLabel->setGeometry((ui_.mainFrame->width() - ui_.offerWorkingLabel->width()) / 2, ui_.offerWorkingLabel->y(),
				ui_.offerWorkingLabel->width(), ui_.offerWorkingLabel->height());

			ui_.offerWorkingLabel->setText(QString::fromUtf16(L"Nabídnout"));					
		}

	}

	TradeItem * getItem() {
		return item_;
	}

	~TradeOfferDialog() {};

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

	unsigned long getDamaged() {
		if (item_->type == STACKABLE_TRADE_ITEM) {
			return ui_.offerDamagedSlider->getValue();
		}
		else {
			return 0;
		}
	}
};

#endif