#ifndef SH_PLUGINS_WEB_H
#define SH_PLUGINS_WEB_H

#include <QObject>
#include <pivector.h>
#include "SH_plugin_manifest.h"

class QNetworkAccessManager;
class QNetworkReply;

class SHPluginsWeb: public QObject {
	Q_OBJECT
public:
	SHPluginsWeb();
	~SHPluginsWeb();

	void setBaseURL(const QString & url);
	void requestPluginsList();
	bool waitPluginsList();

	bool downloadPlugin(const SHPluginManifest & manifest, QString dir_to);

	const PIVector<SHPluginManifest> & manifests() const {return manifests_list;}
	
private:
	static bool writeFile(const QString & path, const QByteArray & data);
	QNetworkReply * sendGet(const QString & path, std::function<void(QNetworkReply*)> on_success, std::function<void(QNetworkReply*)> on_error = nullptr);
	void listReaded(const QByteArray & ba);
	void manifestReaded(const QString & path, const QByteArray & ba);
	void requestNextFromList();
	void queueNextFromList();

	PIVector<SHPluginManifest> manifests_list;
	
	QNetworkAccessManager * nam = nullptr;
	QString base_url;
	QStringList list_to_get;
	int list_to_get_src_size = 1;
	bool list_done = false;

public slots:
	
signals:
	void downloadStarted();
	void downloadProgress(int percent);
	void downloadFinished();
	
};

#endif
