#ifndef BUILD_WIDGET_H
#define BUILD_WIDGET_H

#include <QtGui>
#include "ui_buildWidget.h"
#include "softProj\WeaponsWidget\fightWidgetRow.h"
#include "softProj\WeaponsWidget\fightWidgetRowWidget.h"
#include "softProj\clientServer\structs.h"
#include "softProj\Main\MessageDialog.h"
#include "softProj\Main\ConfirmDialog.h"
#include <deque>
#include <map>
#include <set>

class BuildWidget;

enum EFortressBuildTableType {
	FORTRESS_BUILD_TABLE_OWNED, FORTRESS_BUILD_TABLE_UNCONTROLLED, FORTRESS_BUILD_TABLE_CLASSES,
};

class BuildWidgetTable : public QWidget {
    Q_OBJECT

public:
    BuildWidgetTable(BuildWidget* parent, EFortressBuildTableType tableType);

    void setTitle(QPushButton *titleLabel); /**< Sets title name of the trade widget. */
    void setTable(QTableWidget *tableWidget);
    void setSearchButton(QPushButton *searchButton); //TODO, FIXME, COMMENTME
    void setShowAllButton(QPushButton *showAllButton); //TODO, FIXME, COMMENTME
    void setSearchEdit(QLineEdit *searchEdit); //TODO, FIXME, COMMENTME

    void clearFilteredContent();
    void clearAllContent();
    void insertItem(FightWidgetRow *row);       
    void connectSignals();

	int findRowOfItem(unsigned long id);
	FightWidgetRow * findItemRow(int row, int col);
	FightWidgetRow * findItemById(unsigned long id);

	void removeRow(int row);
	void removeRowAndItem(FightWidgetRow * item, int row);		
	void setRowAsClicked(int row);
	void setNoRowAsClicked();
protected:            	
    virtual void cellClicked(FightWidgetRow * item, int row);

    QPushButton *titleLabel_;
    QTableWidget *tableWidget_;
    QPushButton *searchButton_;
    QPushButton *showAllButton_;
    QLineEdit *searchEdit_;

    BuildWidget * tradeWidget_;

    typedef std::map<QString, FightWidgetRow *> ItemsMap;
    typedef std::vector<FightWidgetRow *> ItemsVector;

    int insertItemIntoTable(FightWidgetRow * item);

    std::map<long, FightWidgetRow *> rows_;
	
	FightWidgetRowWidget * clickedFortressRow_;
private slots:
    void cellClickedSlot(int row, int col);
	void itemClickedSlot(id_t itemId);	
private slots:
    void searchClickedSlot();
    void showAllClickedSlot();
private:	
	EFortressBuildTableType tableType_;
};

class BuildWidget : public QWidget {
	Q_OBJECT
public:
	BuildWidget(QWidget *parent);
	~BuildWidget();

	/** Clears client state of the widget */
	void clearState();
private:
	Ui::BuildWidget ui_;

	BuildWidgetTable * ownedFortressesTable_;
	BuildWidgetTable * uncontrolledTable_;
	BuildWidgetTable * classesTable_;

	int selectedRow_;

	/* FORTRESSES */
	void clearAllOwnedFortresses();
	void clearAllUncontrollableFortresses();
	void clearAllFortressClasses();

	void insertOwnedFortress(FightWidgetRow * item);
	void insertUncontrollableFortress(FightWidgetRow * item);	
	void insertFortressClass(FightWidgetRow * item);

	void setSelfPartner(FightWidgetRow * item);
	void setEnemyPartner(FightWidgetRow * item);
	void setNoSelfPartner();
	void setNoEnemyPartner();

	void stepSelectOwnedFortress(bool reloadData);

	bool isSynchronizing_;

	std::map<id_t, fortress_build_information> buildInformation_;

	QString normalStyle_;
	QString normalBigStyle_;
	QString yesStyle_;
	QString yesBigStyle_;
	QString noStyle_;
	QString noBigStyle_;

	QIcon yesIcon_;
	QIcon noIcon_;
	QIcon hiddenIcon_;
public:	
	void ownedFortressClicked(FightWidgetRow * item, int row);
	void uncontrolledFortressClicked(FightWidgetRow * item, int row);
	void fortressClassClicked(FightWidgetRow * item, int row);

	void setOwnedFortresses(const std::list<fortress_build_information> & ships, const std::list<fortress_build_information> & bases);
	void setUncontrolledFortresses(id_t fortressId, const std::vector<FortressInRangeData> & ships, const std::vector<FortressInRangeData> & bases);
	void setFortressClasses(const std::list<fortress_class_simple_data> & fortressClasses);

	void answerBuildNewBaseShell(id_t selfFortressId, BaseBuildingResult result, unsigned long long newTitan);
	void answerAvailableFortressClasses(id_t selfFortressId, const std::list<fortress_class_simple_data> & shipClasses, const std::list<fortress_class_simple_data> & baseClasses);
	void answerFortressClassData(const fortress_class_complex_data & data);
	void answerConquerFortress(const ConquerResultData & data);
private slots:
	void conquerButtonClickedSlot();
	void buildButtonClickedSlot();
	void tradeButtonClickedSlot();
signals:
	void ownedFortressesSignal();
	void fortressesInConquerRangeSignal(id_t fortressId);
	void fortressClassesSignal(id_t fortressId);
	void fortressClassDetailsSignal(id_t fortressClassId);
	void buildNewBaseShellSignal(id_t fortressId, id_t fortressClassId, unsigned long long titanium);
	void tradeSignal(id_t selfFortressId, id_t enemyFortressId);
	void conquerSignal(id_t selfFortressId, id_t enemyFortressId);
};

#endif