/*******************************************************************************
*                                                                              *
* 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 "ZTSettings.h"
#include "EngLog.h"
#include <QTextStream>
#include "ZTLog.h"
#include "ZTVersion.h"

CZTSettingsFileMgr::CZTSettingsFileMgr()
{
    mStationName = "Inconnue";
}

unsigned int CZTSettingsFileMgr::LoadSettings(CZTSettingsData *SettingsData)
{
    QFile* SettingsFile = new QFile("./Configuration/ZTSettings.ztc");
    if(SettingsFile)
    {
        if(SettingsFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered) == false)
        {
            CEngLog::instance()->AddLogString("Impossible d'ouvrir (open()) le fichier de settings.  Chargement des paramètres par défaut",1);
            LoadDefaultSettings(SettingsData);
            CEngLog::instance()->AddLogString("Tentative de créer un fichier de settings par défaut.",1);
            SaveSettings(SettingsData);
            return RET_ERROR;
        }
    }
    else
    {
        CEngLog::instance()->AddLogString("Impossible d'ouvrir (new Qfile()) le fichier de settings.  Chargement des paramètres par défaut",1);
        LoadDefaultSettings(SettingsData);
        CEngLog::instance()->AddLogString("Tentative de créer un fichier de settings par défaut.",1);
        return RET_ERROR;
    }

    QDataStream * InputStream = new QDataStream(SettingsFile);

    quint32 MagicNbr;// = 0xBAADCAFE;

    *InputStream >> MagicNbr;

    if(MagicNbr != 0xBAADCAFE + 3)
    {
        CEngLog::instance()->AddLogString("Fichier de settings invalide (mauvais magic number).  Chargement des paramètres par défaut");
        LoadDefaultSettings(SettingsData);
        return RET_ERROR;
    }

    *InputStream >> *(SettingsData->mDetectionFunctionSettings);
	*InputStream >> SettingsData->mAutoExportZT1CSV;
	*InputStream >> SettingsData->mAutoExportZT2CSV;
	*InputStream >> SettingsData->mPGTreshold;
    *InputStream >> SettingsData->mMaxLogCount;
    *InputStream >> SettingsData->mKeepMPM10Logs;
    *InputStream >> SettingsData->mKeepAllZT1Logs;
    *InputStream >> SettingsData->mKeepAllZT2Logs;
    *InputStream >> SettingsData->mUseNetworkTime;
    *InputStream >> SettingsData->mUseModbusCC;
    *InputStream >> SettingsData->mModbusCCHostAddress;
    *InputStream >> SettingsData->mSEIModbusHostAddress;

    //JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_ZT1].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_ZT2].AnalysisActive = true;



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

    LogSettings(SettingsData);

    return RET_OK;
}

unsigned int CZTSettingsFileMgr::SaveSettings(CZTSettingsData *SettingsData)
{
    QFile* SettingsFile = new QFile("./Configuration/ZTSettings.ztc");
    if(SettingsFile)
    {
        if(SettingsFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered) == false)
        {
            CEngLog::instance()->AddLogString("Impossible de créer le fichier de settings (open()).");
            return RET_ERROR;
        }
    }
    else
    {
        CEngLog::instance()->AddLogString("Impossible de créer le fichier de settings (new QFile())");
        return RET_ERROR;
    }

    QDataStream * OutputStream = new QDataStream(SettingsFile);

    quint32 MagicNbr = 0xBAADCAFE + 3;

    *OutputStream << MagicNbr;


    *OutputStream << *(SettingsData->mDetectionFunctionSettings);
	*OutputStream << SettingsData->mAutoExportZT1CSV;
	*OutputStream << SettingsData->mAutoExportZT2CSV;
	*OutputStream << SettingsData->mPGTreshold;
    *OutputStream << SettingsData->mMaxLogCount;
    *OutputStream << SettingsData->mKeepMPM10Logs;
    *OutputStream << SettingsData->mKeepAllZT1Logs;
    *OutputStream << SettingsData->mKeepAllZT2Logs;
    *OutputStream << SettingsData->mUseNetworkTime;
    *OutputStream << SettingsData->mUseModbusCC;
    *OutputStream << SettingsData->mModbusCCHostAddress;
    *OutputStream << SettingsData->mSEIModbusHostAddress;

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

    delete SettingsFile;
    delete OutputStream;

    CZTLog::instance()->AddLogString("Changement de settings",true);
    LogSettings(SettingsData);

    return RET_OK;
}

void CZTSettingsFileMgr::LoadDefaultSettings(CZTSettingsData *SettingsData)
{
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_ZT1].AnalysisActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_ZT2].AnalysisActive = true;

    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_FN].TKActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PG].TKActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP].TKActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP2].TKActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive = true;
    SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive = true;

	SettingsData->mAutoExportZT1CSV = false;
    SettingsData->mAutoExportZT2CSV = false;

    SettingsData->mPGTreshold = 125;
    SettingsData->mMaxLogCount = 100;
    SettingsData->mKeepMPM10Logs = false;
    SettingsData->mKeepAllZT1Logs = false;
    SettingsData->mKeepAllZT2Logs = false;

    SettingsData->mUseNetworkTime = false;

    SettingsData->mUseModbusCC = false;
    SettingsData->mModbusCCHostAddress.setAddress("10.158.224.27");
    SettingsData->mSEIModbusHostAddress.setAddress("10.158.224.28");

}
void CZTSettingsFileMgr::SetStationName(QString StationName)
{
    mStationName = StationName;
}

void CZTSettingsFileMgr::LogSettings(CZTSettingsData *SettingsData, bool LogStationName)
{
    QString temp;
    temp.clear();
    QTextStream stream(&temp);
    CZTLog::instance()->AddLogString(QString().sprintf("Version logiciel: %s",ZT_SOFT_VERSION),true);
    if(LogStationName)
    {
        CZTLog::instance()->AddLogString(QString().sprintf("Station: %s",mStationName.toUtf8().data()));
    }
    CZTLog::instance()->AddLogString("Configuration de la ZT: ",true);


    stream << "     Analyse FN = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     Analyse PG = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream <<  "     Analyse PP ZT1 = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     Analyse PP ZT2 = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     TK FN = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_FN].TKActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     TK PG = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PG].TKActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     TK PP ZT1 = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP].TKActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     TK PP ZT2 = " << SettingsData->mDetectionFunctionSettings->mZTDetectionConfig[DETECTION_FCT_PP2].TKActive;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     Exportation automatique ZT1 CSV = " << SettingsData->mAutoExportZT1CSV;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     Exportation automatique ZT2 CSV = " << SettingsData->mAutoExportZT2CSV;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
	stream << "     Seuil PG = " << SettingsData->mPGTreshold;
    CZTLog::instance()->AddLogString(temp,true);
	temp.clear();
    stream << "     Nb fichiers de passage = " << SettingsData->mMaxLogCount;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     Conserver LOGS MPM10 = " << SettingsData->mKeepMPM10Logs;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     Conserver LOGS ZT1 = " << SettingsData->mKeepAllZT1Logs;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << "     Conserver LOGS ZT2 = " << SettingsData->mKeepAllZT2Logs;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << QString().fromUtf8("     Utilisation de l'heure réseau = ") << SettingsData->mUseNetworkTime;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << QString().fromUtf8("     Utilisation communication Modbus CC = ") << SettingsData->mUseModbusCC;
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << QString().fromUtf8("     Adresse IP CC = ") << SettingsData->mModbusCCHostAddress.toString();
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();
    stream << QString().fromUtf8("     Adresse IP SEI = ") << SettingsData->mSEIModbusHostAddress.toString();
    CZTLog::instance()->AddLogString(temp,true);
    temp.clear();

}

