#ifndef SERVERDESIGNER_H
#define SERVERDESIGNER_H

#include <QMainWindow>
#include <QListWidget>
#include <QTreeWidget>
#include <QFileSystemWatcher>
#include "designer_mainwindow.h"
#include "ui_serverdesigner.h"
#include "session_manager.h"
#include "piqt_connection_edit.h"
#include "script_engine.h"
#include "SH_plugins.h"
#include "upload_dialog.h"
#include "serverscriptedit.h"
#include "serverdesigner_core.h"
#include "variable_duration_dialog.h"
#include "device_streams_dialog.h"
#include "user_roles_dialog.h"


class ServerDesigner: public DesignerMainWindow, protected Ui::ServerDesigner {
	Q_OBJECT
public:
	explicit ServerDesigner();
	~ServerDesigner();

	void loadFiles(const QStringList & path);

private:
	virtual bool event(QEvent * e);
	virtual bool eventFilter(QObject * o, QEvent * e);
	virtual void procArguments(const QStringList & args);

	void reset(bool full = false);
	bool load(const QString & path);
	bool save(const QString & path);
	//QString loadFilter() {return "UI or Script(*.ui *.qs *.dat);;UI files(*.ui);;Script files(*.qs);;Database files(*.dat);;Variable files(*.var)";}
	QString loadFilter() {return "Server Designer project(*.sdp)";}
	QString saveFilter() {return loadFilter();}
	void loadingSession(QPIConfig & conf);
	void savingSession(QPIConfig & conf);
	void loadDB(const QString & path);
	void loadCM(const QString & path);
	void loadVAR(const QString & path);
	void saveVAR(const QString & path);
	void addDevices();
	void addSnippet(QString title, QString content);
	QList<SHS::ScriptVariable> variables() const;
	QStringList variableNames() const;
	bool makeAssembly(const QString & f);

	ConnectionEdit conn_edit;
	VariableDurationDialog var_dialog;
	DeviceStreamsDialog device_stream_dialog;
	UserRolesDialog user_roles_dialog;
	QFileSystemWatcher watcher;
	SHSScriptEngine engine;
	SHAssembly ass;

public slots:

private slots:
	void saveProject();
	void loadFile(const QString & path);
	void stopTest();
	void requestBAF(QByteArray * content);
	void on_widgetScripts_titleChangeRequest(QString title);
	void on_actionAbout_triggered() {showAbout();}
	void on_actionNew_script_triggered();
	void on_actionSave_all_scripts_triggered();
	void on_actionEdit_with_Qt_Designer_triggered();
	void on_actionConnection_editor_triggered();
	void on_actionDevice_streams_triggered();
	void on_actionReload_triggered() {loadFile(RUNTIME->server_project.ui);}
	void on_actionTest_with_form_triggered();
	void on_actionLoad_assembly_triggered();
	void on_actionBuild_assembly_triggered();
	void on_actionExtract_assembly_triggered();
	void on_actionUpload_assembly_triggered();
	void on_actionDownload_assembly_triggered();
	void on_actionPreferences_triggered();
	void on_actionAdd_script_to_project_triggered();
	void on_actionUser_roles_triggered();
	void on_treeVariables_itemChanged(QTreeWidgetItem *item, int col) {variablesChanged();}
	void on_treeVariables_itemClicked(QTreeWidgetItem *item, int col);
	void on_treeVariables_itemDoubleClicked(QTreeWidgetItem *item, int col);
	void on_treeVariables_itemSelectionChanged();
	void on_buttonVarAdd_clicked();
	void on_buttonVarDel_clicked();
	void on_buttonVarClone_clicked();
	void on_widgetScripts_documentsChanged(bool yes) {setChanged(yes);}
	void on_objectTree_propertyDoubleClicked(QStringList path);
	void on_objectTree_commandDoubleClicked(SHS::Command cmd);
	void on_listSnippets_itemDoubleClicked(QListWidgetItem * item);
	void currentScriptChanged(const QString & path);
	void watchUI();
	void noWatchUI();
	void scriptError(int line, QString file, QString message, QString stack);
	void variablesChanged();

};


#endif // SERVERDESIGNER_H
