#ifndef FACTORIES_WIDGET_H
#define FACTORIES_WIDGET_H

#include <QtGui>
#include <cassert>
#include "ui_factoriesWidget.h"
#include "softProj\DetailsWidget\ParamDetailsWidget.h"
#include "softProj\clientServer\structs.h"
#include "softProj\Main\INotifyUpdate.h"

///Factory widget show factories and their states.
class FactoriesWidget : public NotifyWidget {
	Q_OBJECT

private:
	Ui::FactoriesWidget ui_;

	QString weightUnit_;
	QString sizeUnit_;

	factories_status state_;
signals:
	/** Signals that user wants to unmount items
	@param patternName Name of pattern for device to unmount
	@param maxWorking Maximum number of working items to unmount
	@param maxDamaged Maximum number of damaged items to unmount
	*/
	void unmountSignal(const std::string & patternName, unsigned long long maxWorking, unsigned long long maxDamaged);
private slots:
	void ilmeniteFurnaceUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_ILMENITE_FURNACE, state_.ilmeniteFurnace.working, state_.ilmeniteFurnace.damaged);
	}

	void deuteriumDecomposerUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_DEUTERIUM_DECOMPOSER, state_.deuteriumDecomposer.working, state_.deuteriumDecomposer.damaged);
	}

	void magneticalFilterUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_MAGNETICAL_FILTER, state_.magneticalFilter.working, state_.magneticalFilter.damaged);
	}

	void zirconiumReactorUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_ZIRCONIUM_REACTOR, state_.zirconiumReactor.working, state_.zirconiumReactor.damaged);
	}
public:
	FactoriesWidget(QWidget * parent) : NotifyWidget(parent) {
		ui_.setupUi(this);

		ui_.factoriesSection->setName(L"Továrny");

		ui_.ilmeniteFurnace->setName(L"Ilmenitové pece");
		ui_.deuteriumDecomposer->setName(L"Deuteriové rozkladače");
		ui_.magneticalFilter->setName(L"Magnetické filtry");		
		ui_.zirconiumReactor->setName(L"Zirconiové reaktory");

		ui_.magneticalFilter->setConsumptionVisible(false);
		ui_.magneticalFilter->setEffectivityVisible(false);
		ui_.magneticalFilter->setSpaceEffectivityVisible(false);
		
		bool connected = true;
		connected = connected && connect(ui_.deuteriumDecomposer, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(deuteriumDecomposerUnmountButtonClickedSlot()));
		connected = connected && connect(ui_.ilmeniteFurnace, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(ilmeniteFurnaceUnmountButtonClickedSlot()));		
		connected = connected && connect(ui_.magneticalFilter, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(magneticalFilterUnmountButtonClickedSlot()));
		connected = connected && connect(ui_.zirconiumReactor, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(zirconiumReactorUnmountButtonClickedSlot()));
		assert(connected && "Signals not connected");

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

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

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

		ui_.deuteriumDecomposer->setAvailable(state.deuteriumDecomposer.isAvailable);
		ui_.deuteriumDecomposer->setEffectivity(state.deuteriumDecomposer.actualMetamorphosisEffectivity);
		ui_.deuteriumDecomposer->setSpaceEffectivity(state.deuteriumDecomposer.actualSpaceMetamorphosisEffectivity);
		ui_.deuteriumDecomposer->setConsumption(state.deuteriumDecomposer.actualTotalConsumption);
		ui_.deuteriumDecomposer->setProduction(state.deuteriumDecomposer.actualTotalProduction);
		ui_.deuteriumDecomposer->setDamageStatus(state.deuteriumDecomposer.working, state.deuteriumDecomposer.damaged);

		ui_.ilmeniteFurnace->setAvailable(state.ilmeniteFurnace.isAvailable);
		ui_.ilmeniteFurnace->setEffectivity(state.ilmeniteFurnace.actualMetamorphosisEffectivity);
		ui_.ilmeniteFurnace->setSpaceEffectivity(state.ilmeniteFurnace.actualSpaceMetamorphosisEffectivity);
		ui_.ilmeniteFurnace->setConsumption(state.ilmeniteFurnace.actualTotalConsumption);
		ui_.ilmeniteFurnace->setProduction(state.ilmeniteFurnace.actualTotalProduction);
		ui_.ilmeniteFurnace->setDamageStatus(state.ilmeniteFurnace.working, state.deuteriumDecomposer.damaged);

		ui_.zirconiumReactor->setAvailable(state.zirconiumReactor.isAvailable);
		ui_.zirconiumReactor->setEffectivity(state.zirconiumReactor.actualMetamorphosisEffectivity);
		ui_.zirconiumReactor->setSpaceEffectivity(state.zirconiumReactor.actualSpaceMetamorphosisEffectivity);
		ui_.zirconiumReactor->setConsumption(state.zirconiumReactor.actualTotalConsumption);
		ui_.zirconiumReactor->setProduction(state.zirconiumReactor.actualTotalProduction);
		ui_.zirconiumReactor->setDamageStatus(state.zirconiumReactor.working, state.deuteriumDecomposer.damaged);

		ui_.magneticalFilter->setAvailable(state.magneticalFilter.isAvailable);
		ui_.magneticalFilter->setProduction(state.magneticalFilter.actualTotalProduction);
		ui_.magneticalFilter->setDamageStatus(state.magneticalFilter.working, state.deuteriumDecomposer.damaged);

		this->state_ = state;
	} /**< Sets and displays factories states. */ 

	void notifyUpdate() {}
};

#endif