#ifndef SUBSPACE_DRIVE_WIDGET_H
#define SUBSPACE_DRIVE_WIDGET_H

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

///This widget manages and displays subspace drive.
class SubspaceDriveWidget : public NotifyWidget {
	Q_OBJECT

private:
	Ui::SubspaceDriveWidget ui_;
	subspace_drive_status state_;
signals:
	/** Signals that user wants to unmount items
	@param patternName Name of pattern for device to unmount
	*/
	void unmountSignal(const std::string & patternName);
private slots:
	void quarkGluonCoreUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_QUARK_GLUON_CORE);
	}

	void inertialDampersUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_INERTIAL_DAMPERS);
	}

	void navigationComputerUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_NAVIGATION_COMPUTER);
	}

	void subspaceBeaconUnmountButtonClickedSlot() {
		emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_SUBSPACE_BEACON);
	}
public:
	SubspaceDriveWidget(QWidget * parent) : NotifyWidget(parent) {
		ui_.setupUi(this);

		ui_.subspaceDriveSection->setName(L"Podprostorový pohon");

		ui_.navigationComputer->setSpeedUnit(L"ms");
		ui_.inertialDampers->setSpeedUnit(L"ms");
		ui_.subspaceBeacon->setSpeedUnit(L"ms");
		
		ui_.quarkGluonCore->setSpeedUnit(L"ms");
		ui_.quarkGluonCore->setConsumptionUnit(L"mg/s");

		bool connected = true;
		connected = connected && connect(ui_.inertialDampers, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(inertialDampersUnmountButtonClickedSlot()));
		connected = connected && connect(ui_.subspaceBeacon, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(subspaceBeaconUnmountButtonClickedSlot()));		
		connected = connected && connect(ui_.navigationComputer, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(navigationComputerUnmountButtonClickedSlot()));
		connected = connected && connect(ui_.quarkGluonCore, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(quarkGluonCoreUnmountButtonClickedSlot()));
		assert(connected && "Signals not connected");
	}

	void setState(subspace_drive_status & details) {
		this->rememberUpdateTime();

		if (details.quark_gluon_core_available) {
			ui_.quarkGluonCore->setAvailable(true);
			ui_.quarkGluonCore->setConsumption(details.quark_gluon_core_consumption);
			ui_.quarkGluonCore->setSpeed(details.quark_gluon_core_max_speed);
			ui_.quarkGluonCore->setDamageStatus(details.quark_gluon_core_status * 100);
		}
		else {
			ui_.quarkGluonCore->setAvailable(false);
		}

		if (details.navigation_computer_available) {
			ui_.navigationComputer->setAvailable(true);
			ui_.navigationComputer->setSpeed(details.navigation_computer_max_speed);
			ui_.navigationComputer->setDamageStatus(details.navigation_computer_status * 100);
		}
		else {
			ui_.navigationComputer->setAvailable(false);
		}

		if (details.navigation_computer_available) {
			ui_.subspaceBeacon->setAvailable(true);
			ui_.subspaceBeacon->setSpeed(details.subspace_beacon_max_speed);
			ui_.subspaceBeacon->setDamageStatus(details.subspace_beacon_status * 100);
		}
		else {
			ui_.subspaceBeacon->setAvailable(false);
		}

		if (details.inertial_dampers_available) {
			ui_.inertialDampers->setAvailable(true);
			ui_.inertialDampers->setSpeed(details.inertial_dampers_max_speed);
			ui_.inertialDampers->setDamageStatus(details.inertial_dampers_status * 100);
		}
		else {
			ui_.inertialDampers->setAvailable(false);
		}

		ui_.consumptionValue->setText(Settings::instance().formatNumber(details.actualConsumption) + " TW");
		ui_.speedValue->setText(Settings::instance().formatNumber(details.actualSpeedToC));

		this->state_ = details;
	}

	void notifyUpdate() {}
};

#endif