#include "DataFileProcessor.h"

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

DataFileProcessor::~DataFileProcessor()
{
}


int DataFileProcessor::WriteWavelengthInfo(float *pfWaveLength, int iLength)
{
	QFileInfo qfFileInfo(m_qstrWavelengthInfoFileName);
	if (qfFileInfo.isFile())
	{
		return 0;
	}
	else
	{
		bool bRes = true;
		QFile qfData(m_qstrWavelengthInfoFileName);
		bRes = qfData.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
		if (!bRes)
		{
			//qDebug() << "WriteEnvironmentInfo open Failed.";
			printf("WriteWavelengthInfo QFile open Failed");
			return 1;
		}

		QString qstrTemp = "wavelength = ";
		qfData.write(qstrTemp.toLatin1());
		qfData.write("{ ");
		for (int i=0;i< iLength;i++)
		{
			qstrTemp =QString::number(pfWaveLength[i], 'f', 2);
			if (i== (iLength-1))
			{
				qfData.write(qstrTemp.toLatin1());
			}
			else
			{
				qfData.write(qstrTemp.toLatin1());
				qfData.write(", ");
			}
		}
		qfData.write("}");
		qfData.close();

	}
	return 0;
}

int DataFileProcessor::WriteData(DataFrame dfDataFrame)
{
	QFile qfData(m_qstrFullFileName);
	qDebug() << m_qstrFullFileName;
	bool bRes = qfData.open(QFile::WriteOnly| QIODevice::Append);
	if (!bRes)
	{
		printf("WriteData QFile open Failed");
		return 0;
	}
	qfData.write((char*)&dfDataFrame, sizeof(DataFrame));
	qfData.flush();
	return 0;
}

void DataFileProcessor::LoadWaveLengthFile(QString qstrFileName)
{
	//E:\WavelengthInfo.txt
	QFile qfWavelength(qstrFileName);
	//QFile qfWavelength();

	bool bRes = qfWavelength.open(QFile::ReadOnly);
	if (!bRes)
	{
		printf("LoadWaveLengthFile open Failed.");
		return;
	}

	QString qsData = qfWavelength.readAll();
	int iTemp = qsData.size();
	qsData.remove(iTemp - 1, 1);
	qsData.remove(0, 14);
	QStringList qsList = qsData.split(',');
	for (int i=0;i<qsList.size();i++)
	{
		qDebug() << qsList[i];
	}
	//iTemp = qsData.size();
	return;
}

void DataFileProcessor::LoadSingleDataFile(QString qstrFileName)
{
	QFile qfDataFile(qstrFileName);
	DataFrame dfTemp;

	bool bRes = qfDataFile.open(QFile::ReadOnly);
	if (!bRes)
	{
		printf("LoadWaveLengthFile open Failed.");
		return;
	}
	int iExpoTime=0;
	std::vector<int> vecExpotime;
	std::vector<double> vecDTime;
	long long int llCount = (qfDataFile.size()) / (sizeof(DataFrame));
	for (int i=0;i<llCount;i++)
	{
		QByteArray qbDataFrame = qfDataFile.read(sizeof(DataFrame));
		memcpy(&dfTemp, qbDataFrame, sizeof(DataFrame));
		vecExpotime.push_back(dfTemp.usExposureTimeInMS);
		vecDTime.push_back(dfTemp.dTimes);
	}
	return;
}



void DataFileProcessor::RecoverBackground(QString qstrDataFileName, QString qstrBackgroundFileName)
{
	int iExpoTime = 0;
	std::vector<int> vecExpotime;
	std::vector<double> vecDTime;
	DataFrame dfDataTemp;
	DataFrame m_dfBackground;

	QString qstrFullFileName(qstrDataFileName+"recv");
	QFile qfDataRec(qstrFullFileName);
	bool bRes = qfDataRec.open(QFile::WriteOnly | QIODevice::Append);
	if (!bRes)
	{
		printf("WriteData QFile open Failed");
		return;
	}

	QFile qfDataFile(qstrDataFileName);
	bRes = qfDataFile.open(QFile::ReadOnly);
	if (!bRes)
	{
		printf("qstrDataFileName open Failed.");
		return;
	}
	///
	std::vector<DataFrame> m_vecDataFrame;
	m_vecDataFrame.clear();
	DataFrame dfDarkTemp;

	QFile qfData(qstrBackgroundFileName);
	bRes = qfData.open(QFile::ReadOnly);
	if (!bRes)
	{
		//qDebug() << "LoadTable open Failed.";
		printf("LoadTable open Failed.");
		return;
	}
	QString qstrTemp;
	QStringList qsList;
	while (!qfData.atEnd())
	{
		QString data;
		data = qfData.readLine();
		qsList = data.split(',');

		dfDarkTemp.usExposureTimeInMS = qsList[0].toInt();
		for (int i = 0; i < qsList.size() - 1; i++)
		{
			dfDarkTemp.lData[i] = qsList[i + 1].toInt();
			qDebug() << qsList[i] << ";";
		}
		m_vecDataFrame.push_back(dfDarkTemp);
		qDebug() << qsList.size() << endl;
	}
	///


	long long int llCount = (qfDataFile.size()) / (sizeof(DataFrame));
	for (int i = 0; i < llCount; i++)
	{
		QByteArray qbDataFrame = qfDataFile.read(sizeof(DataFrame));
		memcpy(&dfDataTemp, qbDataFrame, sizeof(DataFrame));
		vecExpotime.push_back(dfDataTemp.usExposureTimeInMS);
		vecDTime.push_back(dfDataTemp.dTimes);

		if (dfDataTemp.usExposureTimeInMS<10)
		{
			int iB = dfDataTemp.usExposureTimeInMS % 10;
			m_dfBackground = m_vecDataFrame[iB];
		}
		else if (dfDataTemp.usExposureTimeInMS >=10&& dfDataTemp.usExposureTimeInMS<=99)
		{
			int iA = dfDataTemp.usExposureTimeInMS / 10;
			m_dfBackground = m_vecDataFrame[8 + iA];
		}
	  

		for (int i = 0; i < 2048 ; i++)
		{
			dfDataTemp.lData[i] = dfDataTemp.lData[i] + m_dfBackground.lData[i];
		}

		double dBackground = 0;
		int iCountBg = 0;
		for (int i=6;i<21;i++)
		{
			dBackground = dBackground +dfDataTemp.lData[i];
			iCountBg++;
		}
	 
		dBackground = dBackground / iCountBg;

		for (int i = 0; i < 2048; i++)
		{
			dfDataTemp.lData[i] = dfDataTemp.lData[i] - dBackground;
		}
	
		qfDataRec.write((char*)&dfDataTemp, sizeof(DataFrame));
		qfDataRec.flush();
	
	}

	

	return;
}

void DataFileProcessor::GenerateFilePath()
{
    m_qdtTime = QDateTime::currentDateTime();
// 	QTimeZone curZone;
// 	m_qdtTime.toTimeZone();
	QString qstrAddYMD = m_qdtTime.toString("/yyyy_MM_dd");
	m_qdtTime = m_qdtTime.addSecs(3600 * 8);
	QString qstrAddHMS = m_qdtTime.toString("hh_mm_ss");


	m_qstrFullFileName = m_qstrFilePath + qstrAddYMD;
	QString qstrTemp= m_qstrFullFileName;
	
	m_qstrFullFileName= m_qstrFullFileName+"/"+ qstrAddHMS+".dat";

	QDir qdirPathTemp(m_qstrFilePath);
	if (!qdirPathTemp.exists())
	{
		bool bRes = qdirPathTemp.mkdir(m_qstrFilePath);
		if (!bRes)
		{
			//qDebug() << "DataFileProcessor mkdir Failed.";
			printf("DataFileProcessor mkdir Failed");
		}
	}

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


