#ifndef LOGIN_WIDGET_H
#define LOGIN_WIDGET_H

#include <QtGui>
#include <QFile>
#include <map>
#include <cassert>

#include "ui_loginWidget.h"
#include "softProj\clientServer\structs.h"
#include "softProj\Main\ConfirmDialog.h"

/// Widget for loging in to the game and management of connections.
/** Widget for loging in to the game and management of connections. */
class LoginWidget : public QWidget {
Q_OBJECT
signals:
	/** Signals that user clicked submit button and application should connect
	@param details Login details information 
	*/
	void submitLoginSignal(const LoginDetails & details);

	/** Signals that user clicked submit registration button and application should register player
	@param details Registration details information 
	*/
	void submitRegistrationSignal(const RegistrationData & details);
public slots:
	/** Slot for clicking submit button */
	void submitButtonClickedSlot(bool checked);

	/** Slot for clicking save connection button */
	void saveButtonClickedSlot(bool checked);

	/** Slot for clicking add new connection button */
	void addButtonClickedSlot(bool checked);

	/** Slot for remove connection button */
	void removeButtonClickedSlot(bool checked);
private slots:
	/** Slot for double clicking cell in a logins table
	@param row Clicked row number
	@param column Clicked column number 
	*/
	void cellDoubleClickedSlot(int row, int column);

	/** Slot for clicking cell in a logins table
	@param row Clicked row number
	@param column Clicked column number 
	*/
	void cellClickedSlot(int row, int column);

	/** Slot for changing edited connection settings */
	void settingsChangedSlot();

	void registerButtonClickedSlot();
public:
	/** Creates new instance of LoginWidget
	@param parent Parent widget
	*/
	LoginWidget(QWidget * parent);

	/** Sets widget enabled or disabled 
	@param enabled If widget should be enabled
	*/
	virtual void setEnabled(bool enabled);

	/** Loads connections settings from file from given path
	@param filePath Path to file with connection settings
	*/
	void loadConnections(const QString & filePath);

	/** Saves connections to a file with given file path
	@param filePath Path where to save connections 
	*/
	void saveConnections(const QString & filePath);

	/** Saves connections to a file from where it loaded */
	void saveConnections();

	/** Loads connections from the file and adds given connection 
	@param details Login information to add
	*/
	void addConnection(const LoginDetails & details);

	/** Destructor */
	~LoginWidget();
private:
	/** Widget ui from designer */
	Ui::LoginWidget ui_;

	/** Collections with all connection details that are available */
	std::map<int, LoginDetails> allLoginDetails_;

	/** Number of selected connection */
	int selectedIndex_;

	/** File path to file from where connections were load */
	QString filePath_;

	ConfirmDialog * confirmDialog_;

	/** Creates and sets new login details according to values from form
	@return Login details set by values from form
	*/
	LoginDetails getLoginDetailsFromForm();

	/** Clears form input values */
	void clearForm();

	/** Finds index in a table that corresponds to connection settings with given index */
	int findRowByIndex(int index);

	/** Inserts to table new connection settings row 
	@param login Username 
	@param password Password
	@param server Server address
	@param port Port number
	*/
	void insertConnectionItem(QString login, QString password, QString server, QString port);

	/** Finds login details in login table in given row and column.
	This should be called only after cellClicked events, to ensure that there
	is valid QTableWidgetItem in a table with given row and column.
	@param row Row where to find login details
	@param column Column where to find login details
	@return Login details found in a table
	*/
	std::pair<int, LoginDetails> getLoginDetailsFromTable(int row, int column);
	
	/** Shows confirmation dialog */
	int showConfirmDialog(const std::wstring & title, const std::wstring & text);

	/** Shows confirmation dialog for adding new connection */
	int showConfirmAddDialog();

	/** Shows confirmation dialog for saving current connection */
	int showConfirmSaveDialog();

	/** Shows confirmation dialog for removing current connection
	@return Result value of the dialog
	*/
	int showConfirmRemoveDialog();
};

#endif