#ifndef ALL_DEVICES_WIDGET_H
#define ALL_DEVICES_WIDGET_H

#include "ui_AllDevicesWidget.h"
#include "deviceItemWidget.h"
#include <QtGui>
#include <cassert>
#include "deviceItem.h"
#include "protocol.h"
#include "softProj\clientServer\commandClient.h"
#include "softProj\Main\messageDialog.h"
#include "softProj\Main\INotifyUpdate.h"


enum EDeviceType {
	ANDROIDS,
	HR_SENSORS,
	LIFE_SUPPORT,
	LR_SENSORS,
	MAINFRAME,
	MASKING,
	SHIELDS,
	TRANSPORT,
	WEAPONS,
};

///Manages various devices in ship and displays them. 
class AllDevicesWidget : public NotifyWidget {
	Q_OBJECT
private:
	Ui::AllDevicesWidget ui_;
	 
	int actRow;
	int actColumn;

	int energySupply;
	int energyAccumulator;

	bool isLockedForUpdate_;
	void lockForUpdate(bool lock);

	std::map<EDeviceType, DeviceItemWidget * > devices_;
	typedef std::map<EDeviceType, DeviceItemWidget *>::iterator DevicesIterator;

	DeviceItemWidget * addDevice(EDeviceType deviceType, const std::wstring & name, const std::string & deviceIconPath);

	MessageDialog * messageDialog_;

	DeviceBackgroundsHolder * backgroundsHolder_;

	answerDeviceEnergy state_;

	void showMessageDialog(const std::string & title, const std::string & message);
public:
	void setCurrentEnergySupply(unsigned long long value); /**< Sets and displays current energy supply. */
	void setMaximumEnergySupply(unsigned long long value);	/**< Sets and displays maximal energy supply. */
	void setCurrentEnergyAccumulator(unsigned long long value); /**< Sets and displays current energy in accumulator. */
	void setMaximumEnergyAccumulator(unsigned long long value);	/**< Sets and displays maximal energy in accumulator. */
private slots:
	void devicesSubmitedSlot(bool);
	void devicesResetedSlot(bool);
	void submitMessageDialogSlot();
	void settingsChangedSlot();
signals:
	void submitDevicesSignal(requestDeviceEnergy & state);
	void resetDevicesSignal();
public:
	AllDevicesWidget(QWidget * parent);
	~AllDevicesWidget();

	void initWidget();	


	requestDeviceEnergy getState() {
		requestDeviceEnergy state;

		state.ANDROIDS = devices_[ANDROIDS]->getState();
		state.HR_SENSORS = devices_[HR_SENSORS]->getState();
		state.LIFE_SUPPORT = devices_[LIFE_SUPPORT]->getState();
		state.LR_SENSORS = devices_[LR_SENSORS]->getState();
		state.MAINFRAME = devices_[MAINFRAME]->getState();
		state.MASKING = devices_[MASKING]->getState();
		state.SHIELDS = devices_[SHIELDS]->getState();
		state.TRANSPORT = devices_[TRANSPORT]->getState();
		state.WEAPONS = devices_[WEAPONS]->getState();

		return state;
	} /**< Returns energy states of all supported devices. */

	void setState(const answerDeviceEnergy & state); /**< Sets energy states of all supported devices. */
	void setStateERROR(unsigned long shipId, const std::string & reason);

	void notifyUpdate();
};

#endif