#ifndef SERVERSCRIPTEDIT_H
#define SERVERSCRIPTEDIT_H

#include <QListWidget>
#include <QMetaMethod>
#include <qcodeedit.h>
#include "ui_serverscriptedit.h"
#include "script_engine.h"
#include "serverdesigner_core.h"


class ServerScriptEdit: public QWidget, public Ui::ServerScriptEdit {
	Q_OBJECT
public:
	explicit ServerScriptEdit(QWidget * parent = 0);
	~ServerScriptEdit();

	void loadFiles(const QStringList & path);
	void saveCurrentScriptTo(QString path);
	void closeAll();
	QString currentFile();
	QString currentTitle();
	void setProject(const SHS::ServerProject & proj);

	void clearForm();
	void scriptError(int line, QString file, QString message);
	void addSignal(QMetaMethod mm, QString signal_src);
	void insertText(const QString & t);
	void insertEvent(const QString & func_name);
	void insertFunction(const QString & func_name);
	void insertInclude(const QString & inc_name);

private:
	virtual bool event(QEvent * e);

	bool saveFile(QTextDocument * doc);
	bool saveAsFile(QTextDocument * doc);
	//QString loadFilter() {return "UI or Script(*.ui *.qs *.dat);;UI files(*.ui);;Script files(*.qs);;Database files(*.dat);;Variable files(*.var)";}
	void setDocument(QTextDocument * doc);
	QTextDocument * currentDocument();
	void addDocTab(QTextDocument * doc, const QString & title, const QString & path = QString());
	void _setIconSize();
	void applyExtraSelection();
	void prepareUnusedDocs();
	QTextCursor findInDocs(const QString & s, QTextDocument *& doc);
	void setTextCursor(QTextCursor tc);
	void showOpened();
	void moveToOpened();
	void changeOpened(bool next);
	void gotoLine(QString file, int line);

	QTabBar * tab_bar;
	QListWidget * list_opened;
	QTextEdit::ExtraSelection es_error, es_syntax;
	QMap<QString, QTextDocument*> uu_docs;
	QVector<QTextDocument*> last_opened;
	QIcon icon_main, icon_proj;
	SHS::ServerProject project;

public slots:
	void loadFile(const QString & path);
	void newScript();
	void openScript();
	void saveCurrentScript();
	void saveAllScripts();
	void saveAsCurrentScript();
	void closeCurrentScript();
	void clearError();

private slots:
	void on_codeEdit_textChanged();
	void on_codeEdit_requestDocByPath(QString path, QTextDocument ** ret);
	void on_codeEdit_gotoDoc(QString path, int pos);
	void on_buttonNewTab_clicked() {newScript();}
	void on_labelSyntax_linkActivated(QString link);
	void tabBar_currentChanged(int index);
	void tabBar_tabCloseRequested(int index);
	void markTabs();
	void projectChanged();

signals:
	void titleChangeRequest(QString title);
	void currentScriptChanged(QString path);
	void documentsChanged(bool);

};


#endif // SERVERSCRIPTEDIT_H
