#ifndef WAREHOUSES_WIDGET_H
#define WAREHOUSES_WIDGET_H

#include <QtGui>
#include <cassert>
#include <map>
#include "ui_warehousesWidget.h"
#include "warehouseItemWidget.h"
#include "stockTableWidget.h"
#include "softProj\clientServer\structs.h"
#include "softProj\helpers.h"
#include "softProj\Main\INotifyUpdate.h"
#include "softProj\Main\mountDialog.h"

class WarehousesWidget;


class WarehousesWidget : public NotifyWidget {
	Q_OBJECT

private:
	Ui::WarehousesWidget ui_;
	int updateNumber_;
	QString weightUnit_;
	QString sizeUnit_;
	MountDialog * mountDialog_;
signals:
	/** Signals that widget wants to show dialog */
	void showDialogSignal();

	/** Signals that widget wants to hide dialog */
	void hideDialogSignal();

	/** Signals that user wants to mount items
	@param itemId Id of item to mount
	@param count Number of items to mount
	*/
	void mountSignal(id_t itemId, unsigned long long count);
private:
	void createMountDialog() {
		mountDialog_ = new MountDialog(this);
																						   
		bool connected = connect(mountDialog_, SIGNAL(submitSignal()), this, SLOT(submitMountDialogSlot()));
		connected = connected && connect(mountDialog_, SIGNAL(cancelSignal()), this, SLOT(cancelMountDialogSlot()));
		assert(connected && "Signals not connected");

		mountDialog_->setWindowFlags(Qt::SplashScreen);
	}
private slots:
	void itemClickedSlot(const warehouse_item_status & item) {
		emit showDialogSignal();

		if (mountDialog_ == NULL) {
			createMountDialog();
		}	

		if (item.isStackable) {
			mountDialog_->setDialog(item.patternName, item.id, item.working);	
		}
		else {
			mountDialog_->setDialog(item.patternName, item.id);	
		}
	 
		mountDialog_->show();
		mountDialog_->raise();
		mountDialog_->activateWindow();
	}
public:
	WarehousesWidget(QWidget * parent) : NotifyWidget(parent) {
		ui_.setupUi(this);		

		mountDialog_ = NULL;

		weightUnit_ = " kg";
		sizeUnit_ = " m^3";

		bool ok = connect(ui_.warehouseItemsTable, SIGNAL(itemClickedSignal(const warehouse_item_status &)), this, SLOT(itemClickedSlot(const warehouse_item_status &)));
		assert(ok && "Signals not connected");

		ui_.energeticCell->setName(L"Energetické články");
		ui_.energeticCell->setIcon(":/pictures/images/others/energetickeClanky.png");
		ui_.energeticCell->setCapacityUnit(" TJ");
		ui_.energeticCell->setAllCapacityUnit(" TJ");
		ui_.energeticCell->setCapacityLabel(L"Kapacita jednoho článku");

		ui_.magneticGrid->setName(L"Magnetické mřížky");
		ui_.magneticGrid->setIcon(":/pictures/images/others/magnetickeMrizky.png");
		ui_.magneticGrid->setCapacityUnit(" mg");
		ui_.magneticGrid->setAllCapacityUnit(" mg");
		ui_.magneticGrid->setCapacityLabel(L"Kapacita jedné mřížky");

		ui_.transportContainer->setName(L"Transportní kontejnery");
		ui_.transportContainer->setIcon(":/pictures/images/others/transportniKontejnery.png");
		ui_.transportContainer->setCapacityUnit(" m^3");
		ui_.transportContainer->setAllCapacityUnit(" m^3");
		ui_.transportContainer->setCapacityLabel(L"Kapacita jednoho kontejneru");

		ui_.warehousesSection->setName(L"Sklady");

		updateNumber_ = 0;
	}

	void setState(warehouses_status & state) {
		this->rememberUpdateTime();

		ui_.weightValue->setText(Settings::instance().formatNumber(state.weight) + weightUnit_);
		ui_.sizeValue->setText(Settings::instance().formatNumber(state.size) + sizeUnit_);

		/* Magnetical grids */

		if (state.energeticCell.count > 0) {
			ui_.energeticCell->setCapacity(state.energeticCell.actualCapacity / (double) state.energeticCell.count);
		}
		else {
			ui_.energeticCell->setCapacity(0);
		}

		ui_.energeticCell->setAllCapacity(state.energeticCell.actualCapacity * state.energeticCell.count);
		ui_.energeticCell->setDamageStatus(state.energeticCell.working, state.energeticCell.damaged);

		/* Magnetical grids */

		if (state.magneticGrid.count > 0) {
			ui_.magneticGrid->setCapacity(state.magneticGrid.actualCapacity);
		}
		else {
			ui_.magneticGrid->setCapacity(0);
		}
		
		ui_.magneticGrid->setAllCapacity(state.magneticGrid.actualCapacity * state.magneticGrid.count);
		ui_.magneticGrid->setDamageStatus(state.magneticGrid.working, state.magneticGrid.damaged);

		/* Magnetical grids */

		if (state.transportContainer.count > 0) {
			ui_.transportContainer->setCapacity(state.transportContainer.actualCapacity);
		}
		else {
			ui_.transportContainer->setCapacity(0);
		}

		ui_.transportContainer->setAllCapacity(state.transportContainer.actualCapacity * state.transportContainer.count);
		ui_.transportContainer->setDamageStatus(state.transportContainer.working, state.transportContainer.damaged);

		/* Other items */

		std::map<id_t, warehouse_item_status> newItems;

		for (warehouses_status::items_list_t::iterator itemIt = state.warehouseItems.begin(); itemIt != state.warehouseItems.end(); ++itemIt) {
			newItems.insert(std::make_pair(itemIt->id, *itemIt));
		}

		ui_.warehouseItemsTable->updateItems(newItems);
	}

	void notifyUpdate() {}

private slots:
	/** Slot for informing that user canceled mount dialog */
	void cancelMountDialogSlot() {
		mountDialog_->reject();
		emit hideDialogSignal();
	}

	/** Slot for informing that user confirmed mount dialog */
	void submitMountDialogSlot() {	
		unsigned long count = mountDialog_->getCount();
		id_t itemId = mountDialog_->getItemId();
		std::string patternName = mountDialog_->getPatternName();
		mountDialog_->accept();

		emit hideDialogSignal();
		emit mountSignal(itemId, count);
	}
};

#endif