#include "DataFileProcessor.h"

DataFileProcessor::DataFileProcessor(QObject* parent /*= nullptr*/)
{
#ifdef _DEBUG
	m_qstrFilePath = "E:/WorkSpace/TowerOptoSifAndSpectral/Data";
#else
	m_qstrFilePath = "/home/data/Data";
#endif // DEBUG
	
}

DataFileProcessor::~DataFileProcessor()
{
}

void DataFileProcessor::SetEnvironmentContex(EContext struEC)
{
	m_struEC = struEC;
}

void DataFileProcessor::SetManmadeEnviromentalContext(MEContext struMEC)
{
	m_struMEC = struMEC;
}

void DataFileProcessor::SetDeviceInfo(FSContext struDeviceContext)
{
	m_struDeviceContext = struDeviceContext;
}

void DataFileProcessor::SetData(std::vector<std::vector<DataFrame>> vecData)
{
	m_vecData.clear();
	m_vecData = vecData;
}

bool DataFileProcessor::WriteDataFile()
{
	GenerateFilePath();
	WriteEnvironmentInfo();
	WriteDeviceInfo();
	WriteData();
	return 1;
}

void DataFileProcessor::GenerateFilePath()
{
    m_qdtTime = QDateTime::currentDateTime();
	QString qstrAddYMD = m_qdtTime.toString("/yyyy_MM_dd");
	QString qstrAddHMS = m_qdtTime.toString("_hh_mm_ss");

	m_struEC.qstrUTCDateTime = m_qdtTime.toUTC().toString("yyyy_MM_dd hh:mm:ss");

	m_qstrFullFileName = m_qstrFilePath + qstrAddYMD;
	QString qstrTemp= m_qstrFullFileName;
	if (m_struEC.qstrLocation=="")
	{
		m_struEC.qstrLocation = "Unknown";
	}
	m_qstrFullFileName= m_qstrFullFileName+"/"+m_struEC.qstrLocation + qstrAddHMS+".csv";

	QDir qdirPath(qstrTemp);
	if (!qdirPath.exists())
	{
		bool bRes = qdirPath.mkdir(qstrTemp);
		if (!bRes)
		{
			qDebug() << "DataFileProcessor mkdir Failed.";
		}
	}
}

bool DataFileProcessor::WriteEnvironmentInfo()
{
	bool bRes = true;
	QFile qfData(m_qstrFullFileName);
	bRes = qfData.open(QFile::WriteOnly|QFile::Text|QFile::Truncate);
	if (!bRes)
	{
		qDebug() << "WriteEnvironmentInfo open Failed.";
		return bRes;
	}
	qDebug() << m_qstrFullFileName;
	//EC
	qfData.write("EnvironmentalContext,");
	qfData.write("DEV_SN,");
	qfData.write(m_struEC.qstrDEV_SN.toLatin1());
	qfData.write(",");
	qfData.write("CaseHumidity,");
	qfData.write(m_struEC.qstrCaseHumidity.toLatin1());
	qfData.write(",");
	qfData.write("CaseTemperature,");
	qfData.write(m_struEC.qstrCaseTemperature.toLatin1());
	qfData.write(",");
	qfData.write("GPS_Altitude,");
	qfData.write(m_struEC.qstrGPS_Altitude.toLatin1());
	qfData.write(",");
	qfData.write("GPS_Latitude,");
	qfData.write(m_struEC.qstrGPS_Latitude.toLatin1());
	qfData.write(",");
	qfData.write("GPS_Longtitude,");
	qfData.write(m_struEC.qstrGPS_Longtitude.toLatin1());
	qfData.write(",");
	qfData.write("GPS_North,");
	qfData.write(m_struEC.qstrGPS_North.toLatin1());
	qfData.write(",");
	qfData.write("Location,");
	qfData.write(m_struEC.qstrLocation.toLatin1());
	qfData.write(",");
	qfData.write("UTCDateTime,");
	qfData.write(m_struEC.qstrUTCDateTime.toLatin1());
	qfData.write(",");
	//qfData.write("\n");

	//MEC
	//qfData.write("ManmadeEnvironmentalContext\n");
	qfData.write("DownlaodAddress,");
	qfData.write(m_struMEC.qstrDownlaodAddress.toLatin1());
	qfData.write(",");
	qfData.write("DownloadUserID,");
	qfData.write(m_struMEC.qstrDownloadUserID.toLatin1());
	qfData.write(",");
	qfData.write("HTTPServer,");
	qfData.write(m_struMEC.qstrHTTPServer.toLatin1());
	qfData.write(",");
	qfData.write("InstallationTime,");
	qfData.write(m_struMEC.qstrInstallationTime.toLatin1());
	qfData.write(",");
	qfData.write("NameOfMaintenanceStaff,");
	qfData.write(m_struMEC.qstrNameOfMaintenanceStaff.toLatin1());
	qfData.write(",");
	qfData.write("PhoneNumberOfMaintenanceStaff,");
	qfData.write(m_struMEC.qstrPhoneNumberOfMaintenanceStaff.toLatin1());
	//qfData.write(",");

	qfData.close();
	return bRes;
}

void DataFileProcessor::WriteDeviceInfo()
{
	QFile qfData(m_qstrFullFileName);
	bool bRes = qfData.open(QFile::WriteOnly | QFile::Text | QIODevice::Append);
	if (!bRes)
	{
		qDebug() << "WriteDeviceInfo open Failed.";
		return;
	}
	QString qstrTemp;

	qfData.write("\n");
	qfData.write("TotalSpectrometer,");
	qstrTemp = QString::number(m_struDeviceContext.ucDeviceNumber);
	qfData.write(qstrTemp.toLatin1());
	qfData.write(",");
	qfData.write("TotalScanPoints,");
	qstrTemp = QString::number(m_vecData[0].size());
	qfData.write(qstrTemp.toLatin1());
	qfData.write("\n");

	for (int i=0;i< m_struDeviceContext.ucDeviceNumber;i++)
	{
		qstrTemp = QString("FS%1_Info").arg(i + 1);
		qfData.write(qstrTemp.toLatin1());
		qfData.write(",");

		qfData.write("Model,");
		using namespace ZZ_MISCDEF::IRIS;
		qstrTemp = QString::fromStdString(GetDeviceModelName(m_struDeviceContext.ucDeviceModel[i]));
		qfData.write(qstrTemp.toLatin1());
		qfData.write(",");

		qfData.write("SN,");
		qstrTemp = QString::fromStdString(m_struDeviceContext.strSN[i]);
		qfData.write(qstrTemp.toLatin1());
		qfData.write(",");

		qfData.write("Pixels,");
		qstrTemp = QString::number(m_struDeviceContext.usPixels[i]);
		qfData.write(qstrTemp.toLatin1());
		qfData.write(",");

		qfData.write("Depth,");
		qstrTemp = QString::number(m_struDeviceContext.lDepth[i]);
		qfData.write(qstrTemp.toLatin1());
		qfData.write(",");

		qfData.write("TEC Temperature,");
		qstrTemp = QString::number(m_vecData[i][0].fTemperature);
		qfData.write(qstrTemp.toLatin1());

		qfData.write("\n");

		qfData.write("Wavelength,");
		for (unsigned short j = 0; j < m_struDeviceContext.usPixels[i]-1; j++)
		{
			qstrTemp = QString::number(m_struDeviceContext.fWavelength[i][j]);
			qfData.write(qstrTemp.toLatin1());
			qfData.write(",");
		}

		qstrTemp = QString::number(m_struDeviceContext.fWavelength[i][m_struDeviceContext.usPixels[i] - 1]);
		qfData.write(qstrTemp.toLatin1());

		qfData.write("\n");
	}
	qfData.close();
}

bool DataFileProcessor::WriteData()
{
	QFile qfData(m_qstrFullFileName);
	bool bRes = qfData.open(QFile::WriteOnly | QFile::Text | QIODevice::Append);
	if (!bRes)
	{
		qDebug() << "WriteData open Failed.";
		return 0;
	}
	QString qstrTemp;
	qfData.write("Data Section");
	qfData.write("\n");

	using namespace ZZ_MISCDEF::IRIS;
	for (int i=0;i<m_struDeviceContext.ucDeviceNumber;i++)
	{
		for (int j=0;j<m_vecData[i].size();j++)
		{
			qstrTemp = QString::fromStdString(GetDeviceModelName(m_struDeviceContext.ucDeviceModel[i]));
			qfData.write(qstrTemp.toLatin1());
			qstrTemp = QString("_P%1").arg(j + 1);
			qfData.write(qstrTemp.toLatin1());
			qfData.write(",");
			if ((m_vecData[i][j].fTemperature < 5) /*&& (m_vecData[i][j].fTemperature> -5)*/)
			{
				qfData.write("valid");
			}
			else
			{
				qfData.write("invalid");
			}
			
			qfData.write(",");
			qstrTemp = QString::number(m_vecData[i][j].usExposureTimeInMS);
			qfData.write(qstrTemp.toLatin1());
			for (int k=0;k< m_struDeviceContext.usPixels[i];k++)
			{
				qfData.write(",");
				qstrTemp = QString::number(m_vecData[i][j].lData[k]);
				qfData.write(qstrTemp.toLatin1());
			}
			qfData.write("\n");
		}
	
	}
	bool res = true;
	return res;
}


