#include "LogsHandler.h"
#include "LogsSorterConfigData.h"
#include "LogsSorter.h"
#include <QDir>
#include <QFileInfo>

CLogsHandler::CLogsHandler() : QObject()
{
    mSortingConfigData = 0;
    mProgramPtr = 0;

    mLogsWatcher = new QFileSystemWatcher();
    mHandlerRunning = false;

    connect(mLogsWatcher,&QFileSystemWatcher::directoryChanged,this,&CLogsHandler::WatchedDirectoryChanged);

}

CLogsHandler::~CLogsHandler()
{
    mLogsWatcher->removePaths(mLogsWatcher->directories());
    delete mLogsWatcher;
}

int CLogsHandler::SetSortingConfigData(CLogsSorterConfigData *ConfigData)
{
    mSortingConfigData = ConfigData;
    mLogsWatcher->removePaths(mLogsWatcher->directories());

    if(QDir(ConfigData->mLogsSourceDirectory).exists())
    {
        mLogsWatcher->addPath(ConfigData->mLogsSourceDirectory);
        QString msg = QString("Le répertoire source %1 sous surveillance").arg(ConfigData->mLogsSourceDirectory);
        qDebug(qPrintable(msg));
    }
    else
    {
        QString msg = QString("Erreur, le répertoire source %1 n'existe pas").arg(ConfigData->mLogsSourceDirectory);
    }
    return 0;
}

int CLogsHandler::StartLogHandling()
{
    mHandlerRunning = true;
    return 0;
}

int CLogsHandler::StopLogHandling()
{
    mHandlerRunning = false;
    return 0;
}

void CLogsHandler::WatchedDirectoryChanged(QString Path)
{
    ParseWatchedDirectory(Path);
}

int CLogsHandler::ParseWatchedDirectory(QString Path)
{
    QDir SourceDir(Path);
    QFileInfoList LogFiles = SourceDir.entryInfoList();
    const CLogsFilterData *Filter;

    for(int i = 0; i < LogFiles.size(); i++)
    {
        for(int j = 0; j < mSortingConfigData->mLogsFiltersList.size(); j++)
        {
            Filter = &mSortingConfigData->mLogsFiltersList[j];

            if(LogFiles.at(i).fileName().contains(Filter->mLogIdentificator) &&  //Le fichier contient le différentiateur comme ZT1, ZT2, BooleanData, etc.
               LogFiles.at(i).fileName().contains(Filter->mLogPrefix))            //ET le fichier contient le bon préfixe (identification de la station).
            {
                if(Filter->mFilterOperation == CLogsFilterData::LOG_FILTER_MOVE_OPERATION_TYPE)
                {
                    QString msg= QString("Filtre trouvé pour le fichier %1").arg(LogFiles.at(i).fileName());
                    qDebug(qPrintable(msg));

                    QString FullDestinationPath = Filter->mLogDestination;
                    QString DestFilename = LogFiles.at(i).fileName();
                    FullDestinationPath.append("\\");

                    DestFilename.remove(Filter->mLogPrefix);
                    FullDestinationPath.append(DestFilename);

                    if(!QDir(Filter->mLogDestination).exists()) //Créer le répertoire cible s'il n'exite pas déjà
                    {
                        QDir().mkdir(Filter->mLogDestination);
                    }

                    if(QFile::copy(LogFiles.at(i).absoluteFilePath(),FullDestinationPath))
                    {
                        qDebug("Fichier %s copié vers %s",qPrintable(LogFiles.at(i).absoluteFilePath()),qPrintable(FullDestinationPath));
                    }
                    else
                    {
                        qDebug("Impossible de copier %s vers %s",qPrintable(LogFiles.at(i).absoluteFilePath()),qPrintable(FullDestinationPath));
                    }

                    if(QFile::remove(LogFiles.at(i).absoluteFilePath()))
                    {
                         qDebug("Fichier %s effacé",qPrintable(LogFiles.at(i).absoluteFilePath()));
                    }
                    else
                    {
                         qDebug("Impossible d'effacer %s",qPrintable(LogFiles.at(i).absoluteFilePath()));
                    }

                }
                else if(Filter->mFilterOperation == CLogsFilterData::LOG_FILTER_DELETE_OPERATION_TYPE)
                {
                    if(QFile::remove(LogFiles.at(i).absoluteFilePath()))
                    {
                         qDebug("Fichier %s effacé",qPrintable(LogFiles.at(i).absoluteFilePath()));
                    }
                    else
                    {
                         qDebug("Impossible d'effacer %s",qPrintable(LogFiles.at(i).absoluteFilePath()));
                    }
                }
            }
        }

    }



    return 0;
}
