#ifndef PULTWINDOWBASE_H
#define PULTWINDOWBASE_H

#include <QLabel>
#include <QLayout>
#include <QTemporaryFile>
#include <QSystemTrayIcon>
#include <QTreeWidget>
#include <QSettings>
#include <QElapsedTimer>
#include <QTime>
#include <session_manager.h>
#include <piplatform.h>
#include "SH_network_types.h"
#include "SH_client_channel.h"
#include "stackedwidget.h"

class KeyForm;
class SHSUiLoader;
namespace Ui {
	class PultWindowBase;
}

class PultWindowBase: public QWidget {
	Q_OBJECT
public:
	PultWindowBase(QWidget * parent = 0);
	~PultWindowBase();
	
	void saveSession();
#ifndef MOBILE_VIEW
	QSystemTrayIcon * trayIcon() {return &tray;}
#endif
	void addToExternalSession(SessionManager & sm);
	bool askForDownloadNewPackage();

	void setChannel(SHClientChannel * ch);
	SHSUiLoader * uiLoader() {return loader;}

protected:
	virtual bool event(QEvent * e);
	virtual void timerEvent(QTimerEvent * e);
	virtual void showEvent(QShowEvent * e);
	virtual void closeEvent(QCloseEvent * e);
	virtual void setForm(QWidget * f);
	virtual void unsetForm();
	virtual void hideEvent(QHideEvent * e);
	virtual bool backTriggered() {return false;} // return true for ignore close
	void setStatusText(const QString & t);
	
	Ui::PultWindowBase * ui = nullptr;
	QWidget * form = nullptr;
	QMenu * pult_menu = nullptr, * role_menu = nullptr;
	QAction * copy_link_action = nullptr;
	const SHClientChannel * channel = nullptr;
	QFile rcc_file;
	QObjectList form_objects;
	PacketServerInfo server_info;
	SessionManager session;
	SHSUiLoader * loader = nullptr;
	QElapsedTimer active_time;
	SHSStackedWidget * main_stacked = nullptr;
	bool rcc_success = false, downloading;
	int timer_save, timer_download, user_role = 0;
#ifndef MOBILE_VIEW
	QSystemTrayIcon tray;
#endif
	
private slots:
	void on_checkAutoConnectLast_toggled(bool on);
	void on_stackedWidget_currentChanged(int index);
	void on_treeServers_serverSelected(PacketServerInfo info);
	void trayClicked(QSystemTrayIcon::ActivationReason r);
	void exitTriggered();
	void copyLinkTriggered();
	void disconnectTriggered();
	void aboutTriggered();

public slots:
	void connecting(QString addr);
	void connected(PacketServerInfo server);
	void fetching();
	void disconnected();
	void receivedRCC(QByteArray rcc);
	void receivedUI(QByteArray ba);
	void addToSession();
	void keyRequest(PacketServerInfo server);
	void keyProbed(bool ok);
	void syncVersionFailed(int rec_version);
	void textStatus(QString t);
	void packageUpdating();
	void packageNoSuchOnServer(bool forced);
	void packageExists(QString path);
	void packageDownloadStarted();
	void packageDownloadFinished();
	void packageReady(QString path, bool forced);
	void serverError(QString msg, bool critical = true);
	void init();
	void connectedGraphics();
	void changeRoleRequestFailed();
	void changeRoleRequestDone(int index);

signals:
	void formLoaded(QObject * , SHSUiLoader * );
	void keyProbe(QString key);
	void connectRequest(QString addr);
	void disconnectRequest();
	void hideMenuRequest();
	void changeStatus(QString);
	void showed();
	void exited();
	void dialogRequest(QString def);
	void addressEntered(QString addr);
	void saveExternalSession();
	void changeRoleRequest(int index);

};

#endif // PULTWINDOWBASE_H
