#include "MakeDarkCurrentTable.h"
MakeDCT::MakeDCT(QObject* parent /*= nullptr*/)
{
	m_qstrDarkCurrentTable = "/home/data/Data/DCTable.txt";
}

MakeDCT::~MakeDCT()
{

}

void MakeDCT::SetRunParams(OneFSContext struFSRunParams)
{
	m_struFSRunParams = struFSRunParams;
}


int MakeDCT::Initialize()
{
	m_pctrlFS = new CAbsFSController;

	m_pctrlFS->SetContext(m_struFSRunParams);

	m_pctrlFS->Initialize();
	while (!m_pctrlFS->m_iFlagInit)
	{
		Delay_MSec(1000);
	}
	m_pctrlFS->GetDeviceAttr(m_struDeviceAttr);
	m_struFSRunParams.usPixels = m_struDeviceAttr.iPixels;

	return 0;
}

int MakeDCT::Initialize_Part()
{
	if (m_struFSRunParams.ucDeviceModel== DeviceModel::IS1)
	{
		m_struFSRunParams.usPixels = 512;
	}

	if (m_struFSRunParams.ucDeviceModel == DeviceModel::FLAME)
	{
		m_struFSRunParams.usPixels = 2048;
	}
	return 0;
}

int MakeDCT::MakeTable()
{
	DataFrame struSingleFrame;
	long long llAverage[4096] = {0};

	m_vecDataFrame.clear();

	for (int i = 0; i < 10; i++)
	{ 
		memset(llAverage,0,4096*sizeof(long long));
		int iExposureTime = i + 1;
		m_pctrlFS->SetExposureTime(iExposureTime);//1-10

		qDebug() << "Smapling Time:" << iExposureTime;

		for (int i = 0; i < 10; i++)
		{
			m_pctrlFS->SingleShot(struSingleFrame);

			for (int j = 0; j < m_struFSRunParams.usPixels; j++)
			{
				llAverage[j] += struSingleFrame.lData[j];
			}
		}

		for (int j = 0; j < m_struFSRunParams.usPixels; j++)
		{
			struSingleFrame.lData[j] = (long)((double)llAverage[j] / (double)10);
		}
		
		m_vecDataFrame.push_back(struSingleFrame);

	}

	for (int i = 0; i < 9; i++)
	{
		memset(llAverage, 0, 4096 * sizeof(long long));
		int iExposureTime = (i+2) *10;//20 - 100
		m_pctrlFS->SetExposureTime(iExposureTime);

		qDebug() << "Smapling Time:" << iExposureTime;

		for (int i = 0; i < 10; i++)
		{
			m_pctrlFS->SingleShot(struSingleFrame);

			for (int j = 0; j < m_struFSRunParams.usPixels; j++)
			{
				llAverage[j] += struSingleFrame.lData[j];
			}
		}

		for (int j = 0; j < m_struFSRunParams.usPixels; j++)
		{
			struSingleFrame.lData[j] = (long)((double)llAverage[j] / (double)10);
		}

		m_vecDataFrame.push_back(struSingleFrame);
	}

	for (int i = 0; i < 9; i++)
	{
		memset(llAverage, 0, 4096 * sizeof(long long));
		int iExposureTime = (i + 2) * 100;//200-1000
		m_pctrlFS->SetExposureTime(iExposureTime);

		qDebug() << "Smapling Time:" << iExposureTime;


		for (int i = 0; i < 10; i++)
		{
			m_pctrlFS->SingleShot(struSingleFrame);

			for (int j = 0; j < m_struFSRunParams.usPixels; j++)
			{
				llAverage[j] += struSingleFrame.lData[j];
			}
		}

		for (int j = 0; j < m_struFSRunParams.usPixels; j++)
		{
			struSingleFrame.lData[j] = (long)((double)llAverage[j] / (double)10);
		}

		m_vecDataFrame.push_back(struSingleFrame);
	}

	QFile qfData(m_qstrDarkCurrentTable);
	bool bRes = qfData.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate);
	if (!bRes)
	{
		//qDebug() << "WriteData open Failed.";
		printf("WriteData open Failed.");
		return 0;
	}
	QString qstrTemp;

	for (int i=0;i<m_vecDataFrame.size();i++)
	{
		if (i<=9)
		{
			qstrTemp = QString::number(i+1);
			qfData.write(qstrTemp.toLatin1());
		}
		else if (i>=10&&i<=18)
		{
			qstrTemp = QString::number((i-8)*10);
			qfData.write(qstrTemp.toLatin1());
		}
		else
		{
			qstrTemp = QString::number((i-17) * 100);
			qfData.write(qstrTemp.toLatin1());
		}
		

		for (int j=0;j<m_struFSRunParams.usPixels;j++)
		{
			qfData.write(",");
			qstrTemp = QString::number(m_vecDataFrame[i].lData[j]);
			qfData.write(qstrTemp.toLatin1());
		}
		qfData.write("\n");
	}
	qfData.flush();
	qfData.close();

	return 0;
	//m_pctrlFS->SingleShot();
}

int MakeDCT::LoadTable()
{
	m_vecDataFrame.clear();
	DataFrame dfTemp;

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

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

	return 0;
	
}

void MakeDCT::GetDCTable(std::vector<DataFrame>& vecDataFrame)
{
	vecDataFrame = m_vecDataFrame;
}

void MakeDCT::Delay_MSec(ZZ_U16 usMS)
{
	QEventLoop qeLoop;
	QTimer::singleShot(usMS, &qeLoop, SLOT(quit()));
	qeLoop.exec();
}
