#ifndef DETAILS_WIDGET_H
#define DETAILS_WIDGET_H

#include <QtGui>
#include <string>
#include <vector>
#include "ui_DetailsWidget.h"
#include "softProj\DetailsWidget\ComplexItem.h"
#include "softProj\DetailsWidget\SimpleItem.h"
#include "softProj\commonTypes.h"
#include "softProj\Main\INotifyUpdate.h"

typedef std::vector<SimpleItem> GameItemsVector;

class QShipButton : public QPushButton
{
	Q_OBJECT
private:
	EShipSectionState shipData_;
	bool isShip_;
signals:
	void clicked(EShipSectionState data);
	
public slots:
	void mouseClicked(bool checked)
	{
		emit clicked(shipData_);
	}
public:
	QShipButton(QString & message, QWidget * parent, EShipSectionState data)
		: QPushButton(message, parent)
	{
		this->shipData_ = data;

		QObject::connect(
			this, SIGNAL(clicked(bool)),
			this, SLOT(mouseClicked(bool))
			);
	}
};

class QBaseButton : public QPushButton
{
	Q_OBJECT
private:
	EBaseSectionState shipData_;
	bool isShip_;
signals:
	void clicked(EBaseSectionState data);
	
public slots:
	void mouseClicked(bool checked)
	{
		emit clicked(shipData_);
	}
public:
	QBaseButton(QString & message, QWidget * parent, EBaseSectionState data)
		: QPushButton(message, parent)
	{
		this->shipData_ = data;

		QObject::connect(
			this, SIGNAL(clicked(bool)),
			this, SLOT(mouseClicked(bool))
			);
	}
};

///Manages set of buttons. 
class DetailsWidget : public NotifyWidget
{
	Q_OBJECT

public:
	DetailsWidget(QWidget * parent);
	~DetailsWidget();
	
	void showShipsTable(GameItemsVector & items); /**< Shows ItemsPanel widget. ItemsPanel will be filled with ship items given through input param. */
	void showBasesTable(GameItemsVector & items); /**< Shows ItemsPanel widget. ItemsPanel will be filled with baseline items given through input param. */

	void changeSelectedFortress( SimpleItem& selectedItem); /**< Select given ship or baseline. */

	void showNavigation();	/**< Shows NavigationWidget. */
	void showHangar();		/**< Shows HangarUI widget. */
	void showDevices();		/**< Shows AllDevicesWidget. */
	void showWeapons();		/**< Shows AllWeaponsWidget. */
	void showRender();		/**< Shows GLwidget2 render widget. */
	void showShields();		/**< Shows ShieldsWidget. */
	void showCentralInfo();	/**< Shows CentralInfoWidget. */
	void showImpulsDrive();	/**< Shows ImpulsDriveWidget. */
	void showSubspaceDrive();	/**< Shows SubspaceDriveWidget. */
	void showCrew();		/**< Shows CrewWidget. */
	void showMainframe();	/**< Shows MainframeWidget. */
	void showQuarrying();	/**< Shows MiningWidget. */
	void showAccommodations(); /**< Shows AccommodationsWidget. */
	void showOtherEquipment(); /**< Shows OtherEquipmentWidget. */
	void showFactories(); /**< Shows FactoriesWidget. */
	void showWarehouses(); /**< Shows WarehousesWidget. */
	void showAndroids(); /**< Shows AndroidsWidget. */
	void showCloaking(); /**< Shows CloakingWidget. */
	void showSensors(); /**< Shows SensorsWidget. */

	/** Clears client state of the widget */
	void clearState();

	SimpleItem * findFortressById(id_t fortressId);
public:
	Ui::detailsWidget ui_;
	void hideDetailsPanel();
	void notifyUpdate();
public slots:
	void changeSelectedFortressSlot(int row, int column);
	void showShipSectionSlot(EShipSectionState data);
	void showBaseSectionSlot(EBaseSectionState data);
signals:
	void changeSelectedFortressSignal(SimpleItem &);
	void changeShipSectionSignal(SimpleItem & item, EShipSectionState data);
	void changeBaseSectionSignal(SimpleItem & item, EBaseSectionState data);
private:
	SimpleItem * findItemRow(int row);	

	void setItemsTable(GameItemsVector & items);
	void setItemsTableRow(const SimpleItem & item);

	std::map<unsigned long, SimpleItem> rows_;
	EMainState detailsState_;
	SimpleItem selectedFortress_;

	QGridLayout * shipButtonsLayout_;
	QGridLayout * baseButtonsLayout_;

	QGridLayout * shipMoreButtonsLayout_;
	QGridLayout * baseMoreButtonsLayout_;

	void showDetailsPanel();

	void hideItemsPanel();
	void showItemsPanel();

	void setItemRowAsSelected(SimpleItem & item);

	QString createDetailsTitle(int id, const std::string & name);

	void setShipButton(QGridLayout * layout, QFrame * frame, ShipParam * param, int index, int itemsInRow);
	void setBaseButton(QGridLayout * layout, QFrame * frame, BaseParam * param, int index, int itemsInRow);

	void showPageWidget(QWidget * widgetPage, NotifyWidget * widget, bool resizeParentToFit = true);

	NotifyWidget * currentWidget_;
};

#endif
