#ifndef SH_BASE_H
#define SH_BASE_H

#include <QDataStream>
#include <QMetaEnum>
#include <QPolygonF>
#include <QFile>
#include <piqt.h>
#include "SH_version.h"
#include "SH_platform.h"

#if defined(ANDROID) || defined(BLACKBERRY)
#  define MOBILE_VIEW
#endif

#define THREADED_READ_TYPE const uchar *


const double iconSizeMul = 2.;

const char SHFileSuffix[] = "_v1";

typedef QPair<QString, QString> SSPair;
struct ServerAddress;

QString toCName(QString s);
bool    isCName(QString s);
QString findFile(QString p, QString ext, const QString & dir = QString());
QString findExecutable(QString p);
QString parsePath(QString p);
QString replaceStringToHex(QString cmd);
uint    generateID();
void    initAbout();
bool    isFullpathValid(const QString & p);
void    dirContent(const QString & path, QStringList & dirs, QStringList & files, const QString & prefix = QString());
bool    copyDir(const QString & from, const QString & to);
PIByteArray passwordHash(const PIString & p);
ServerAddress parseServerAddress(QString addr);
PIString getThisCloudName();
PropertyStorage mergeProperties(PropertyStorage static_props, PropertyStorage plugin_props);

template <typename T> QMetaEnum getMetaEnum(const char * name) {
	int i = T::staticMetaObject.indexOfEnumerator(name);
	if (i < 0) return QMetaEnum();
	return T::staticMetaObject.enumerator(i);
}


template <typename T> bool saveToFile(const QString & filename, const T & value, uint hdr = 1) {
	QFile f(filename);
	if (!f.open(QIODevice::ReadWrite)) return false;
	f.resize(0);
	QDataStream s(&f);
	s << hdr << value;
	return true;
}

template <typename T> T loadFromFile(const QString & filename, uint hdr = 1) {
	T ret;
	QFile f(filename);
	if (!f.open(QIODevice::ReadOnly)) return ret;
	QDataStream s(&f);
	uint h = 0; s >> h;
	if (h == hdr)
		s >> ret;
	return ret;
}

inline QByteArray readFile(const QString & filename) {
	QFile f(filename);
	if (!f.open(QIODevice::ReadOnly)) return QByteArray();
	return f.readAll();
}

inline QDebug operator <<(QDebug s, const PropertyStorage & p) {s << p.properties(); return s.space();}



class SHSScriptEngineBase {
public:
	virtual QVariant call(const QString & function, const QVariantList & args = QVariantList()) = 0;
};

class __SH_Base_Initializer__ {
public:
	__SH_Base_Initializer__();
	static bool inited;
};
static __SH_Base_Initializer__ __SH_Base_init__;


#endif // SH_BASE_H
