#ifndef WAREHOUSE_ITEM_WIDGET_H
#define WAREHOUSE_ITEM_WIDGET_H

#include <QtGui>
#include <list>
#include <cassert>
#include <cstdlib>
#include <string>
#include "ui_warehouseItemWidget.h"
#include "softProj\clientServer\structs.h"
#include "softProj\settings.h"

class WarehouseItemWidget : public QWidget {
	Q_OBJECT

private:
	Ui::WarehouseItemWidget ui_;
	std::list<QLabel *> labels_;
	warehouse_item_status item_;
signals:
	void clickedSignal(const warehouse_item_status & item);
private slots:
	void itemClickedSlot(bool checked) {
		emit clickedSignal(item_);
	}
public:
	WarehouseItemWidget(QWidget * parent, const warehouse_item_status & item, bool showDamaged) : QWidget(parent) {
		ui_.setupUi(this);

		bool connected = connect(ui_.nameButton, SIGNAL(clicked(bool)), this, SLOT(itemClickedSlot(bool)));
		assert(connected && "Signals not connected");

		if (item.isStackable && showDamaged) {
			ui_.nameButton->setProperty("itemType", "stackable");
			ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());

			ui_.frame->setProperty("itemType", "stackable");
			ui_.frame->setStyleSheet(ui_.frame->styleSheet());

			ui_.workingLabel->resize(45, 12);
			ui_.damagedLabel->resize(45, 12);
			ui_.damagedLabel->setVisible(true);

			ui_.workingLabel->setText(QString::number(item.working));
			ui_.damagedLabel->setText(QString::number(item.damaged));
		}
		else {
			ui_.nameButton->setProperty("itemType", "separate");
			ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());

			ui_.frame->setProperty("itemType", "separate");
			ui_.frame->setStyleSheet(ui_.frame->styleSheet());

			ui_.workingLabel->resize(45, 25);
			ui_.damagedLabel->setVisible(false);

			ui_.workingLabel->setText(QString::number(1));
		}

		if (showDamaged) {
			resize(204, 31);
			ui_.mainFrame->setGeometry(0, 0, 204, 31);
			ui_.deviceFrame->setGeometry(1, 1, 202, 29);
			ui_.frame->setGeometry(2, 2, 198, 25);
			ui_.nameButton->setGeometry(0, 0, 150, 25);
			ui_.valuesFrame->setGeometry(0, 30, 204, 0); 			
			ui_.workingLabel->setGeometry(150, 0, 50, 12);
			ui_.damagedLabel->setGeometry(150, 12, 50, 12); 		
		}
		else {
			resize(290, 31);
			ui_.mainFrame->setGeometry(0, 0, 290, 31);
			ui_.deviceFrame->setGeometry(1, 1, 288, 29);
			ui_.frame->setGeometry(2, 2, 285, 25);
			ui_.nameButton->setGeometry(0, 0, 210, 25);
			ui_.valuesFrame->setGeometry(0, 30, 288, 0); 
			ui_.workingLabel->setGeometry(210, 0, 70, 12);
			ui_.damagedLabel->setGeometry(210, 12, 70, 12); 	
		}

		this->item_ = item;

		ui_.nameButton->setText(Settings::instance().getGameNameQString(item.patternName));		
	}

	id_t getItemId() const {
		return item_.id;
	}

	std::string getPatternName() const {
		return item_.patternName;
	}

	warehouse_item_status getItem() const {
		return item_;
	}

	unsigned long getWorking() {
		return item_.working;
	}

	unsigned long getDamaged() {
		return item_.damaged;
	}

	void addParameter(const warehouse_item_status::parameter_t & parameter) {
		int oldLabelCount = labels_.size();
		int newLabelCount = labels_.size() + 2;

		this->resize(width(), 30 + (newLabelCount / 2) * 15);

		int height = this->height();

		ui_.mainFrame->resize(this->width(), this->height());
		height = ui_.mainFrame->height();
		ui_.deviceFrame->resize(this->width() - 2, this->height() - 2);
		height = ui_.deviceFrame->height();
		ui_.valuesFrame->resize(this->width() - 2, (newLabelCount / 2) * 15);
		height = ui_.valuesFrame->height();

		QLabel * nameLabel = createNameLabel(parameter.name, (oldLabelCount / 2) * 15);
		labels_.push_back(nameLabel);

		QString valueResult;

		switch (parameter.type) {
		case WAREHOUSE_PARAMETER_UNSIGNED_LONG_LONG:
			{
				unsigned long long value = _strtoui64(parameter.value.c_str(), NULL, 10);
				valueResult = Settings::instance().formatNumber(value);
				break;
			}
		case WAREHOUSE_PARAMETER_UNSIGNED_LONG:
			{
				unsigned long value = strtoul(parameter.value.c_str(), NULL, 10);
				valueResult = Settings::instance().formatNumber(value);
				break;
			}
		case WAREHOUSE_PARAMETER_SIGNED_LONG_LONG:
			{
				long long value = _strtoi64(parameter.value.c_str(), NULL, 10);
				valueResult = Settings::instance().formatNumber(value);
				break;
			}
		case WAREHOUSE_PARAMETER_SIGNED_LONG:
			{
				long value = _strtoi64(parameter.value.c_str(), NULL, 10);
				valueResult = Settings::instance().formatNumber(value);
				break;
			}
		case WAREHOUSE_PARAMETER_DOUBLE:
			{
				double value = stod(parameter.value);
				valueResult = Settings::instance().formatNumber(value);
				break;
			}
		}

		QLabel * valueLabel = createValueLabel(valueResult, (oldLabelCount / 2) * 15);
		labels_.push_back(valueLabel);
	}

	virtual bool operator<(const WarehouseItemWidget & that) const {
		return this->item_.id < that.getItemId();
	}

	void setItemCount(unsigned long long working, unsigned long long damaged) {
		ui_.workingLabel->setText(Settings::instance().formatNumber(working));
		ui_.damagedLabel->setText(Settings::instance().formatNumber(damaged));

		this->item_.working = working;
		this->item_.damaged = damaged;
	}
private:
	QLabel * createNameLabel(const std::string & text, int yPos) {
		QLabel * label = new QLabel(ui_.valuesFrame);
		label->setText(Settings::instance().getGameNameQString(text));
		label->setGeometry(0, yPos, ui_.valuesFrame->width() / 2, 15);

		label->setStyleSheet(
			"QLabel {"
				"border: 0;"
				"background: transparent;"
				"color: #eeeeee;"
				"padding-left: 2px;"
				"font-size: 9px;"
				"text-align: left;"
			"}");

		return label;
	}
	 
	QLabel * createValueLabel(const QString& text, int yPos) {
		QLabel * label = new QLabel(ui_.valuesFrame);
		label->setText(text);
		label->setGeometry(ui_.valuesFrame->width() / 2, yPos, ui_.valuesFrame->width() / 2, 15);
		label->setAlignment(Qt::AlignRight);

		label->setStyleSheet(
			"QLabel {"
				"border: 0;"
				"background: transparent;"
				"color: #eeeeee;"
				"padding-right: 2px;"
				"font-size: 9px;"
			"}");

		return label;
	}
};

class WarehouseTableItemWidget : public QTableWidgetItem {
public:
	WarehouseTableItemWidget(const std::string & name) : QTableWidgetItem() {
		QString value = QString::fromUtf8(name.c_str());
		this->setData(Qt::UserRole, QVariant::fromValue(value));
	}

	virtual bool operator<(const QTableWidgetItem & that) const {
		QString & thisName = this->data(Qt::UserRole).toString();
		QString & thatName = that.data(Qt::UserRole).toString();

		return thisName < thatName;
	}
};

#endif