#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#define QT_USE_FAST_CONCATENATION
#define QT_USE_FAST_OPERATOR_PLUS
#include "ui_mainwindow.h"
#include "ui_configuration.h"
#include "ui_about.h"
#include <QMainWindow>
#include <QSettings>
#include <QMessageBox>
#include <QFileDialog>
#include <QProgressDialog>
#include <QProcess>
#include <QSystemTrayIcon>
#include <QTranslator>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

extern QMap<int, QString> rsyncErrorStrings;

class Connexion
{
	public:
	int bandwidthLimit = 0;
	int bandwidthLimitUnit;
	int contimeout = 20;
	QString server;
	QString service;
	QString path;
	QString savePath;
	QString user;
	QString password;
	QProcess * process = nullptr;
	int ipversion = 4;
	uint port = 873;
	bool quit = false;
	void clear();
	bool paused = false;
};

/*class Downloading
{
	public:
	QString server;
	QString service;
	QString path;
	QString savePath;
	QString user;
	QString password;
	int ipversion = 4;
	int port = 873;
	QProcess * process = nullptr;
	bool quit = false;
	bool paused = false;

	void clear();
};*/

class About
{
	public:
	QString version;
	QString author = "Daniel TARTAVEL-JEANNOT";
	QString licence = "GPL_V3";
	QString description;
	QString email = "dtux@free.fr";
	QString git = "https://git.labolyon.fr/dtux/RsyncUI/issues";
};

class MainWindow : public QMainWindow
{
	Q_OBJECT

	public:
		Ui::MainWindow *ui;
		MainWindow(QWidget *parent = nullptr);
		~MainWindow();
		// passwdManager;
		QProgressDialog *progress;
		Connexion connexion;
		Connexion downloading;
		QSettings settings;
		About about;
		QDialog Configuration;
		Ui::Configuration config;
		QDialog aboutDialog;
		Ui::windowAbout AboutW;
		std::vector <QString> serversList;
		QSystemTrayIcon * trayIcon;
		QString icon = "/usr/share/icons/RsyncUI.png";
		bool rescan = false;
		bool stopDlAsked = false;
		bool treeviewClicked = false;
		bool initialization = true;
		bool exiting =  false;

		QList<QString> UnitText
		{
			tr("B"),
			tr("KB"),
			tr("MB"),
			tr("GB"),
			tr("TB"),
			tr("PB")
		};

		QList<QChar> bwUnitChar
		{
			'K',
			'M',
			'T',
			'G',
			'P'
		};

		QVector<QString> downloadProcessErrorString =
		{
		tr("The process failed to start. Either the invoked program is missing, or you may have insufficient permissions or resources to invoke the program."),
		tr("The process crashed some time after starting successfully."),
		tr("The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again."),
		tr("An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel."),
		tr("An error occurred when attempting to read from the process. For example, the process may not be running."),
		tr("An unknown error occurred. This is the default return value of error().")
		};

		void displayTree();
		void populateTree();
		void populateList(QString server, uint port);
		void listServices();
		uint validateServer(QString * server);
		void addTreeItem(QString name, QString fileSize, QString fullsize, QString type, QString date, bool isDir, QString dirName, QTreeWidgetItem *parent);
		bool scanDir(Connexion * connexion, QTreeWidgetItem *parent = NULL, QString path = "" );
		void startDownloading();
		void loadSettings();
		void saveSettings();
		void closeEvent (QCloseEvent *event);
		void saveDownloadList();
		void loadDownloadList();
		void deleteDownloadList();
		void download();
		void initSystemTrayIcon();
		void hideWindow();
		void showWindow();
		void init();
		bool getUserPassword(Connexion *);
		void preparePopulateTree();
		bool testServerPresence(QString, bool=false);
		void passwordStore (QString account, QString password);
		QString passwordGet (QString account);

	private slots:

		void on_listWidget_clicked();

		void on_treeWidget_itemClicked(QTreeWidgetItem *item, bool downloadDir = false);

		void downloadFinished(int exitCode, QProcess::ExitStatus exitStatus);

		void readRsyncOutput();

		void stoppingDownload();

		void on_listDownload_itemClicked(QListWidgetItem *item);

		void on_actionAbout_triggered();

		void on_actionAbout_Qt_triggered();

		bool on_DefaultSaveFolder_triggered();

		void on_connectButton_clicked();

		void on_action_Settings_triggered();

		void on_actionDownload_triggered();

		void on_comboBox_currentIndexChanged(int index);

		void on_buttonBox_accepted();

		void cancelled(QProcess *);

		void on_trayIcon_clicked(QSystemTrayIcon::ActivationReason reason);

		void quitApp();

		void on_actionExit_triggered();

		void setDlSpeed(QString speed);

		void on_actionHiddenService_triggered();

		//void on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);

		void on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);

		void on_treeWidget_doubleClicked(const QModelIndex &index);

		void on_actionPause_downloads_triggered();

	signals:
		void stopDownloading(QProcess *);
		void progressSignal(int);
		void speed(QString);
		void finishedSignal(bool = true);
		void fileName(QString);
		void passwordReady();
};

#endif // MAINWINDOW_H
