#include "SystemConfigger.h"


 ZZ_SysConfigger::ZZ_SysConfigger(QObject *parent /*= nullptr*/)
 {
#ifdef _DEBUG
	 m_qstrDeviceConfigFilePath =        "E:/WorkSpace/TowerOptoSifAndSpectral/config/DeviceSettings.ini";
	 m_qstrJSONConfigFilePath = "E:/WorkSpace/TowerOptoSifAndSpectral/config/config.json";
#else
	 m_qstrJSONConfigFilePath = ("/home/data/Setting/config.json");
#endif // DEBUG

	 m_qsDeviceConfig = NULL;
 }
 
 ZZ_SysConfigger::~ZZ_SysConfigger()
 {
	 if (qfJConfig.isOpen())
	 {
		 qfJConfig.close();
	 }
 }
 
 int ZZ_SysConfigger::Initialize()
 {
	 //////////////////////////////////////////////////////////////////////////load device settings
	 using namespace ZZ_MISCDEF;
	 QMetaEnum enumDeviceModel = QMetaEnum::fromType<ConfiggerDeviceModel>();

	 if (m_qsDeviceConfig!=NULL)
	 {
		delete m_qsDeviceConfig;
	 }
	 m_qsDeviceConfig = new QSettings(m_qstrDeviceConfigFilePath, QSettings::IniFormat);
	 m_struDeviceContext.ucDeviceNumber = m_qsDeviceConfig->value(QString("DEVICE INFO/TotalSpectrometer"),-1).toInt();
	 if (m_struDeviceContext.ucDeviceNumber == -1)
	 {
		 qDebug() << "DEVICE INFO  TotalSpectrometer Value Err.";
		 return 3;
	 }
	 for (ZZ_U8 i=0;i< m_struDeviceContext.ucDeviceNumber;i++)
	 {
		 QString qstrTemp= m_qsDeviceConfig->value(QString("FS%1/Model").arg(i+1), "Null").toString();
		 m_struDeviceContext.ucDeviceModel[i]= enumDeviceModel.keysToValue(qstrTemp.toLatin1().data());

		 int iInterface= m_qsDeviceConfig->value(QString("FS%1/Port").arg(i + 1), -2).toInt();
		 m_struDeviceContext.iInterface[i] = iInterface;

		 qstrTemp = m_qsDeviceConfig->value(QString("FS%1/UID").arg(i + 1), "Null").toString();
		 m_struDeviceContext.strSN[i] = qstrTemp.toStdString();
	 }

	 m_struAcqPosition.iTotalPosition = m_qsDeviceConfig->value(QString("LINEAR SHUTTER/TotalPosition"), -1).toInt();
	 if (m_struAcqPosition.iTotalPosition == -1)
	 {
		 qDebug() << "LINEAR SHUTTER  TotalPosition Value Err.";
		 return 4;
	 }

	 for (ZZ_U8 i = 0; i < m_struAcqPosition.iTotalPosition; i++)
	 {
		 int iPosition = m_qsDeviceConfig->value(QString("LINEAR SHUTTER/Position%1").arg(i), -2).toInt();
		 m_struAcqPosition.iPosition[i] = iPosition;
	 }



	 //////////////////////////////////////////////////////////////////////////load json settings
	 qfJConfig.setFileName(m_qstrJSONConfigFilePath);
	 bool bRes = qfJConfig.open(QIODevice::ReadOnly);
	 if (!bRes)
	 {
		 qDebug() << "QFile open config file Err.";
		 return 1;
	 }

	 m_qbaJData = qfJConfig.readAll();
	 m_qjdJDoc= QJsonDocument::fromJson(m_qbaJData, &m_qjpeJErr);
	 if (m_qjpeJErr.error != QJsonParseError::NoError)
	 {
		 qDebug() << m_qjpeJErr.errorString();
		 return 2;
	 }
	 m_qjoJObj = m_qjdJDoc.object();
	 return 0;
 }

 int ZZ_SysConfigger::GetGrabberRunParams(RunTimeGrabberParams &struGrabberRTParams)
 {
	 struGrabberRTParams.atsParams = m_struAcqTime;
	 struGrabberRTParams.fscParams = m_struDeviceContext;
	 return 0;
 }

 int ZZ_SysConfigger::LoadSettingsFromFile_IS1()
 {
	 m_struIS1Info.qstrCalFile_U0 = m_qjoJObj.value("IS1UpCalFile").toString();
	 m_struIS1Info.qstrCalFile_D1 = m_qjoJObj.value("IS1DownCalFile1").toString();
	 m_struIS1Info.qstrCalFile_D2 = m_qjoJObj.value("IS1DownCalFile2").toString();
	 m_struIS1Info.qstrCalFile_D3 = m_qjoJObj.value("IS1DownCalFile3").toString();

	
 	return 0;
 }
 
 int ZZ_SysConfigger::LoadSettingsFromFile_iSIF()
 {
	 m_struISIFInfo.qstrCalFile_U0 = m_qjoJObj.value("SIFUpCalFile").toString();
	 m_struISIFInfo.qstrCalFile_D1 = m_qjoJObj.value("SIFDownCalFile1").toString();
	 m_struISIFInfo.qstrCalFile_D2 = m_qjoJObj.value("SIFDownCalFile2").toString();
	 m_struISIFInfo.qstrCalFile_D3 = m_qjoJObj.value("SIFDownCalFile3").toString();
 	 return 0;
 }
 
 int ZZ_SysConfigger::LoadSettingsFromFile_System()
 {
	// QString qstr = m_qjoJObj.value("IntervalTime").toString();
	 m_struAcqTime.qtStartTime=QTime::fromString(m_qjoJObj.value("BeginTime").toString(), "hh:mm");
	 if (!m_struAcqTime.qtStartTime.isValid())
	 {
		 qDebug() << "Invalid BeginTime";
		 return 1;
	 }
	 
#ifdef _DEBUG
	 m_struAcqTime.qtInterval = QTime::fromString(m_qjoJObj.value("IntervalTime").toString(), "ss");
	 if (!m_struAcqTime.qtInterval.isValid())
	 {
		 qDebug() << "Invalid IntervalTime";
		 return 1;
	 }
#else
	 m_struAcqTime.qtInterval = QTime::fromString(m_qjoJObj.value("IntervalTime").toString(), "mm");
	 if (!m_struAcqTime.qtInterval.isValid())
	 {
		 qDebug() << "Invalid IntervalTime";
		 return 1;
	 }
#endif

	 m_struAcqTime.qtStopTime = QTime::fromString(m_qjoJObj.value("EndTime").toString(), "hh:mm");
	 if (!m_struAcqTime.qtStopTime.isValid())
	 {
		 qDebug() << "Invalid EndTime";
		 return 1;
	 }

 	return 0;
 }

 int ZZ_SysConfigger::LoadSettingsFromFile_MISC()
 {
	 m_struEC.qstrLocation = m_qjoJObj.value("Location").toString();
	 m_struEC.qstrGPS_Altitude = m_qjoJObj.value("GPS_Altitude").toString();
	 m_struEC.qstrGPS_Latitude = m_qjoJObj.value("GPS_Latitude").toString();
	 m_struEC.qstrGPS_Longtitude = m_qjoJObj.value("GPS_Longtitude").toString();
	 m_struEC.qstrGPS_North = m_qjoJObj.value("GPS_North").toString();

	 m_struMEC.qstrInstallationTime              = m_qjoJObj.value("InstallationTime").toString();
	 m_struMEC.qstrISIFCalibrationTime           = m_qjoJObj.value("ISIFCalibrationTime").toString();
	 m_struMEC.qstrIS1CalibrationTime            = m_qjoJObj.value("IS1CalibrationTime").toString();
	 m_struMEC.qstrNameOfMaintenanceStaff        = m_qjoJObj.value("NameOfMaintenanceStaff").toString();
	 m_struMEC.qstrPhoneNumberOfMaintenanceStaff = m_qjoJObj.value("PhoneNumberOfMaintenanceStaff").toString();
	 m_struMEC.qstrDownloadUserID                = m_qjoJObj.value("DownloadUserID").toString();
	 m_struMEC.qstrDownlaodAddress               = m_qjoJObj.value("DownlaodAddress").toString();

	

	 return 0; 
 }

 int ZZ_SysConfigger::LoadSettings_Test()
 {
	 QFile qfTest(m_qstrJSONConfigFilePath);

	 qfTest.open(QFile::ReadOnly);

	 QByteArray qbJData = qfTest.readAll();

	 QJsonParseError jerr;
	 QJsonDocument jdoc(QJsonDocument::fromJson(qbJData, &jerr));
	 if (jerr.error!= QJsonParseError::NoError)
	 {
		 qDebug() << jerr.errorString();
	 }

	 QJsonObject rootobj = jdoc.object();
	 QStringList key = rootobj.keys();
	 QString qtest1 = rootobj.value(rootobj.keys().at(15)).toString();
	 return 0;
 }
