#ifndef UPDATECONTROLLER_H
#define UPDATECONTROLLER_H


#include <QString>
#include <QObject>
#include <QCryptographicHash>
#include <QDirIterator>
#include <QDebug>
#include <QDataStream>
#include <QMutex>

#include <Systems/Parsers/dataparser.h>
#include <Data/typesDataServerClient.h>
#include <Data/StreamingVersionData.h>

class TCPServer;
class SendSystem;
class DataParser;
class ClientHandler;
class AssetsManager;
class ServerLMSWidget;
class CommonClientHandler;
class Logger;

class UpdateController : public QObject
{
    Q_OBJECT

public:

    explicit UpdateController(QObject *parent = 0);
    void initialize(CommonClientHandler* commonClientHandler,DataParser *dataParser,AssetsManager *assetManager,Logger *logger);
    void compareFiles(ClientHandler* handler, QByteArray array);
    void showHash();
    void calculateFullHash();
    void calculateSharedHash();
    void sendNewVersionList();
    void setCurrentStreamingPath(QString path);
    bool checkDuplicate(QString versionName);
    QList<FileData>* calculateHash(QString path);
    QByteArray getLocalHash();

    QString getCurrentVersionName();
    QList<FileData> getFileSendList();
    QList<FileData> getClientDataList() const;
    QList<FileData> getFileDeleteList() const;
    QString getCurrentStreamingPath() const;

    ~UpdateController();

    DataInfo *getCurrentDataInfo();
    void clearCurrentDataInfo();
    void xmlFileDataParse(QByteArray array);

    QString getPathAdditionalFile(QString name);

    StreamingVersionData *getCurrentVersion();
    void setDataInfo(DataInfo *value);

public slots:
    void changeAssetVersion(QString versionName);
    void createCopyVersion(QString versionName,QString newVersionName,QString author);
    void deleteAssetVersion(QString versionName);
    void setUpCurrentServerHash();

signals:
    void sigLogMessage(QString message);

private:
    QList<FileData> clientDataList;
    QList<FileData> serverDataList;

    QList<FileData> fileSendList;
    QList<FileData> fileDeleteList;

    DataInfo *dataInfo;

    QString buildPath;
    QString currentStreamingPath;
    QString sharedDataPath;
    CommonClientHandler *commonClientHandler;
    DataParser *dataParser;
    AssetsManager *assetManager;
    quint64 sizeToSend;
    QMutex *mutex;

    QString getCommands();
    void printFileList(QList<FileData> fileData);

    bool checkNeedUpdate(ClientHandler* handler);
    void setLocalFileData(QList<FileData> dataList);
    void calculateSize();
    void saveHash(QString fileName,QList<FileData> *fileList);
    void loadHash();
    void CalculateSizeToSend(QList<FileData> diffList);
};

#endif // UPDATECONTROLLER_H
