/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Utilisée pour la configuration non volatile de certains paramètres comme
	les fonction de détection hors service (TK HS).  Les paramètres sont sauvegardés
	dans un fichier binaire qui est ouvert au démarrage du programme.
*/

/* ************************************************************************** */
/* Revision:
### 20121219 JFM
	Verision d'origine.

### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

/* ************************************************************************** */

#include "Settings.h"
#include <QTextStream>
#include <QFile>

CSettingsFileMgr::CSettingsFileMgr()
{
}

unsigned int CSettingsFileMgr::LoadSettings(CSettingsData *SettingsData)
{
    QFile* SettingsFile = new QFile("./Configuration/Settings.ozs");
    if(SettingsFile)
    {
        if(SettingsFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered) == false)
        {
            LoadDefaultSettings(SettingsData);
            SaveSettings(SettingsData);
            return RET_ERROR;
        }
    }
    else
    {
        LoadDefaultSettings(SettingsData);
         return RET_ERROR;
    }

    QDataStream * InputStream = new QDataStream(SettingsFile);
    InputStream->setVersion(QDataStream::Qt_4_8);

    quint32 MagicNbr;// = 0xBAADCAFE;

    *InputStream >> MagicNbr;

    if(MagicNbr != 0xBAADCAFE)
    {
        LoadDefaultSettings(SettingsData);
        return RET_ERROR;
    }

    int NbNetworkEntries = 0;

    *InputStream >> SettingsData->mDataSourceDirectoryPath;

    *InputStream >> NbNetworkEntries;

    for(int i = 0; i < NbNetworkEntries; i++)
    {
        CZTNetworkConfig NewCfg;
        *InputStream >> NewCfg;
        SettingsData->mNetworkCfgList.append(NewCfg);
    }

    SettingsFile->close();
    delete SettingsFile;
    delete InputStream;

    return RET_OK;
}

unsigned int CSettingsFileMgr::SaveSettings(CSettingsData *SettingsData)
{
    QFile* SettingsFile = new QFile("./Configuration/Settings.ozs");
    if(SettingsFile)
    {
        if(SettingsFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered) == false)
        {
            return RET_ERROR;
        }
    }
    else
    {
       return RET_ERROR;
    }

    QDataStream * OutputStream = new QDataStream(SettingsFile);
    OutputStream->setVersion(QDataStream::Qt_4_8);

    quint32 MagicNbr = 0xBAADCAFE;

    *OutputStream << MagicNbr;


    *OutputStream << SettingsData->mDataSourceDirectoryPath;
    *OutputStream << SettingsData->mNetworkCfgList.size();

    for(int i = 0; i < SettingsData->mNetworkCfgList.size(); i++)
    {
        *OutputStream << SettingsData->mNetworkCfgList.at(i);
    }


    SettingsFile->flush();
    SettingsFile->close();

    delete SettingsFile;
    delete OutputStream;

    return RET_OK;
}

void CSettingsFileMgr::LoadDefaultSettings(CSettingsData *SettingsData)
{
    SettingsData->mDataSourceDirectoryPath = "./Trains/";

    SettingsData->mNetworkCfgList.clear();
    QString BaseIP = "192.168.0.11";
    for(int i = 0; i < NB_ZT_ID; i++)
    {
        CZTNetworkConfig ZTNetCfg;
        ZTNetCfg.mZTID = i;
        ZTNetCfg.mIPAdress = BaseIP + QString::number(i);
        SettingsData->mNetworkCfgList.append(ZTNetCfg);
    }
}
