#include "LogsSorterConfig.h"
#include <QFile>
#include <QDataStream>

CLogsSorterConfig::CLogsSorterConfig()
{
    mIsConfigLoaded = false;
}


/*
FILTRE="ZT1"
TYPE="DÉPLACER"
DESCRIPTION="Zone Test 1 Terminus Angrignon"
PREFIXE="ANG"
DESTINATION="D:\Main\Fichiers\JOB\LogsDump\ZT Angrignon"
*/


int CLogsSorterConfig::LoadLogsSorterConfig(QString ConfigFileName)
{
    QFile *ConfigFile = new QFile(ConfigFileName);
    if (!ConfigFile->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        delete ConfigFile;
        return 1;
    }

    mLogsSorterConfig.mLogsFiltersList.clear();
    mLogsSorterConfig.mLogsSourceDirectory.clear();

    QDataStream * OutputStream = new QDataStream(ConfigFile);

    quint32 MagicNbr;
    quint32 FileVersion;

    *OutputStream >> MagicNbr;
    *OutputStream >> FileVersion;
    *OutputStream >> mLogsSorterConfig;

    ConfigFile->close();


    ConfigFile->close();
    delete ConfigFile;
    delete OutputStream;

    return 0;


    //    bool finished = false;
    //    QString string;
    //    int pos;
    //    while(!finished)
    //    {

    //        string.clear();
    //        string = ConfigFile->readLine(1024);
    //        if(string.isEmpty())
    //        {
    //            finished = true;
    //            break;
    //        }

    //        if(string.at(0) != '#' && string.at(0) != '\n')
    //        {
    //            string.remove("\n");    //remove trailing "\n" characters inserted by QFile.readline
    // //           string.remove(' ');     //remove spaces
    //            pos = string.indexOf('=');
    //            if(pos != -1)
    //            {
    //                pos += 1;
    ////              NewFilter.mLogIdentificator = string.mid(pos);
    //                //Recherche d'une définition de filtre
    //                if(string.contains("FILTRE"))
    //                {
    //                    string.remove(' ');     //remove spaces
    //                    QStringList Param = string.split('\"',Qt::SkipEmptyParts);
    //                    if(Param.size() >= 2)
    //                    {
    //                        CLogsFilterData NewFilter;
    //                        NewFilter.mLogIdentificator = Param.at(1);

    //                        QString delimiter = ConfigFile->readLine(1024); //doit être '{'
    //                        if(delimiter.isEmpty())
    //                        {
    //                            //fin du fichier!!
    //                            finished = true;
    //                            break;
    //                        }
    //                        else
    //                        {
    ////                            delimiter.
    ////                            if(delimiter == "{")
    ////                            {

    ////                            }
    //                        }
    //                    }

    //                }
    //            }


    //        }
    //    }
}


int CLogsSorterConfig::SetNewConfig(CLogsSorterConfigData NewConfig)
{
    mLogsSorterConfig = NewConfig;
    SaveConfigToFile(".\\Config.bin");

    return 0;
}

int CLogsSorterConfig::SaveConfigToFile(QString ConfigFileName)
{
    QFile *ConfigFile = new QFile(ConfigFileName);
    if (!ConfigFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered))
    {
        delete ConfigFile;
        return 1;
    }

    QDataStream * OutputStream = new QDataStream(ConfigFile);

    quint32 MagicNbr = 0xBAADCAFE;
    quint32 FileVersion = 1;

    *OutputStream << MagicNbr;
    *OutputStream << FileVersion;
    *OutputStream << mLogsSorterConfig;

    ConfigFile->close();
    delete ConfigFile;
    delete OutputStream;


}

