#include "MainDataGrabber.h"

CMainDataGrabber::CMainDataGrabber(QObject* parent /*= nullptr*/)
{
	m_piFlagCaptureThreadStatus_Signal = NULL;
	m_piFlagCaptureThreadStatus_Dark = NULL;

	m_GrabTimer = new QTimer(this);

	m_iFlagIsCapturing = false;
	m_iFlagIsCapturing_Signal = false;
	m_iFlagIsCapturing_Dark = false;
}

CMainDataGrabber::~CMainDataGrabber()
{
	if (m_pControlThread.size() != 0)
	{
		for (int i=0;i< m_pControlThread.size();i++)
		{
			delete m_pControlThread[i];
		}
	}
	m_pControlThread.clear();

	if (m_piFlagCaptureThreadStatus_Signal!=NULL)
	{
		delete m_piFlagCaptureThreadStatus_Signal;
	}

	if (m_piFlagCaptureThreadStatus_Dark != NULL)
	{
		delete m_piFlagCaptureThreadStatus_Dark;
	}


	if (m_GrabTimer!=NULL)
	{
		m_GrabTimer->stop();
		delete m_GrabTimer;
	}
}

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

void CMainDataGrabber::SubDataFrame(DataFrame& dfLeft, DataFrame const dfRight, int iCount)
{
	for (int i = 0; i < iCount; i++)
	{
		dfLeft.lData[i] = dfLeft.lData[i]- dfRight.lData[i];
	}
}

int CMainDataGrabber::SetGrabberParams(RunTimeGrabberParams struGrabberRTParams)
{
	m_struAcqTime = struGrabberRTParams.atsParams;
	m_struDeviceContext = struGrabberRTParams.fscParams;
	m_struAcqPosSetting = struGrabberRTParams.apsParams;
	m_struLinearShutterContext = struGrabberRTParams.lscParam;

	m_iTotalThreads = m_struDeviceContext.ucDeviceNumber;
	return 0;
}

int CMainDataGrabber::SetGrabberFileProcessor(DataFileProcessor &dfpSaver)
{
	m_pdfpSaver = &dfpSaver;
	return 0;
}

int CMainDataGrabber::InitThreadStatus()
{
	if (m_piFlagCaptureThreadStatus_Signal!=NULL)
	{
		delete m_piFlagCaptureThreadStatus_Signal;
	}
	m_piFlagCaptureThreadStatus_Signal = new int[m_iTotalThreads];
	for (int i = 0; i < m_iTotalThreads; i++)
	{
		m_piFlagCaptureThreadStatus_Signal[i] = 0;
	}


	if (m_piFlagCaptureThreadStatus_Dark != NULL)
	{
		delete m_piFlagCaptureThreadStatus_Dark;
	}
	m_piFlagCaptureThreadStatus_Dark = new int[m_iTotalThreads];
	for (int i = 0; i < m_iTotalThreads; i++)
	{
		m_piFlagCaptureThreadStatus_Dark[i] = 0;
	}
	return 0;
}

int CMainDataGrabber::InitializeWorkers()
{
	for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++)
	{
		m_piFlagCaptureThreadStatus_Signal[i] = 0;
		m_piFlagCaptureThreadStatus_Dark[i] = 0;

		FSInfo fsInfo;
		fsInfo.iInterface = m_struDeviceContext.iInterface[i];
		fsInfo.ucDeviceModel = m_struDeviceContext.ucDeviceModel[i];
		fsInfo.strSN = m_struDeviceContext.strSN[i];
		fsInfo.fMaxFactor = m_struDeviceContext.fMaxFactor[i];
		fsInfo.fMinFactor = m_struDeviceContext.fMinFactor[i];
		fsInfo.lDepth = m_struDeviceContext.lDepth[i];

		CAbstractFSController* m_pWorkThread = new CAbstractFSController;
		m_pWorkThread->SetRunParas(i, fsInfo);
		m_pWorkThread->InitializeFSControl();

		DeviceAttribute daAttrTemp;
		m_pWorkThread->GetDeviceAttr(daAttrTemp);
		m_struDeviceContext.usPixels[i] = (ZZ_U16)daAttrTemp.iPixels;
		for (ZZ_U16 j=0;j< daAttrTemp.iPixels;j++)
		{
			m_struDeviceContext.fWavelength[i][j] = daAttrTemp.fWaveLengthInNM[j];
		}

		m_pControlThread.push_back(m_pWorkThread);
	}
	m_pdfpSaver->SetDeviceInfo(m_struDeviceContext);
	return 0;
}

int CMainDataGrabber::StartWorkers()
{

	for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++)
	{
		QThread *pWorkThreadHolder = new QThread();
		m_pControlThread[i]->moveToThread(pWorkThreadHolder);
		pWorkThreadHolder->start();
	}
	
	return 0;
}

int CMainDataGrabber::SetupMsgPipelines()
{
	//connect(this, &CMainDataGrabber::SignalStartGrabOnce, this, &CMainDataGrabber::StartGrab);

	for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++)
	{
		connect(this, &CMainDataGrabber::SignalStartGrabOnce_Signal, m_pControlThread[i], &CAbstractFSController::StartAcquisitionSignal);
		connect(this, &CMainDataGrabber::SignalStartGrabOnce_Dark, m_pControlThread[i], &CAbstractFSController::StartAcquisitionDark);
		connect(m_pControlThread[i], &CAbstractFSController::SignalAcqFinished_Signal, this, &CMainDataGrabber::HandleThreadEvent_Signal);
		connect(m_pControlThread[i], &CAbstractFSController::SignalAcqFinished_Dark, this, &CMainDataGrabber::HandleThreadEvent_Dark);
	}

	connect(this, &CMainDataGrabber::SignalGrabOnceFinished, this, &CMainDataGrabber::GrabOnceFinished);

	connect(m_GrabTimer, &QTimer::timeout, this, &CMainDataGrabber::OnTimeCounter);

	connect(this, &CMainDataGrabber::SignalStartGrab, this, &CMainDataGrabber::StartGrabTimer);

	connect(this, &CMainDataGrabber::SignalGrabOnceFinished_Signal, this, &CMainDataGrabber::GrabOnceFinished_Signal);
	connect(this, &CMainDataGrabber::SignalGrabOnceFinished_Dark, this, &CMainDataGrabber::GrabOnceFinished_Dark);
	
	return 0;
}

int CMainDataGrabber::StartGrab()
{
	emit SignalStartGrab();
	return 0;
}

int CMainDataGrabber::HandleThreadEvent_Signal(int iThreadID, int iFlagStatus)
{
	m_piFlagCaptureThreadStatus_Signal[iThreadID] = iFlagStatus;

	bool bFinished = true;
	for (int i = 0; i < m_iTotalThreads; i++)
	{
		if (m_piFlagCaptureThreadStatus_Signal[i] != 1)
		{
			bFinished = false;
			break;
		}
	}

	if (bFinished)
	{
		emit SignalGrabOnceFinished_Signal();
	}
	return 0;
}

int CMainDataGrabber::HandleThreadEvent_Dark(int iThreadID, int iFlagStatus)
{
	m_piFlagCaptureThreadStatus_Dark[iThreadID] = iFlagStatus;

	bool bFinished = true;
	for (int i = 0; i < m_iTotalThreads; i++)
	{
		if (m_piFlagCaptureThreadStatus_Dark[i] != 1)
		{
			bFinished = false;
			break;
		}
	}

	if (bFinished)
	{
		emit SignalGrabOnceFinished_Dark();
	}
	return 0;
}

int CMainDataGrabber::GrabOnceFinished_Signal()
{
	m_iFlagIsCapturing_Signal = false;
	return 0;
}

int CMainDataGrabber::GrabOnceFinished_Dark()
{
	m_iFlagIsCapturing_Dark = false;
	return 0;
}

int CMainDataGrabber::StartGrabTimer()
{


	//////////////////////////////////////////////////////////////////////////start
	////check start time
// 	bool bStopWait = false;
// 	while (!bStopWait)
// 	{
// 		QTime qtTime = QTime::currentTime();
// 		if (m_struAcqTime.qtStartTime >= qtTime && qtTime<m_struAcqTime.qtStopTime)
// 		{
// 			bStopWait = true;
// 		}
// 	}
	
 	//start
 	int iIntervalInMS =m_struAcqTime.qtInterval.hour()*3600*1000+ m_struAcqTime.qtInterval.minute()*60*1000+ m_struAcqTime.qtInterval.second()*1000;
 	m_GrabTimer->start(3000);

	//////////////////////////////////////////////////////////////////////////test
// 	m_iFlagIsCapturing = true;
// 	emit SignalStartGrabOnce();
//  	while (m_iFlagIsCapturing)
//  	{
//  		QThread::msleep(1000);
//  	}
// 	
// 	m_iFlagIsCapturing = true;
// 	InitThreadStatus();
// 	emit SignalStartGrabOnce();
// 	while (m_iFlagIsCapturing)
// 	{
// 		QThread::msleep(1000);
// 	}
// 	qDebug() << "Allgrab stopped" << " Thread ID:" <<2;


	////final test code eat my ass
// 	m_iFlagIsCapturing = 1;
// 	
// 	
// 	for (int i=0;i<5;i++)
// 	{
// 		m_iFlagIsCapturing_Signal = 1;
// 		emit SignalStartGrabOnce_Signal();
// 		while (m_iFlagIsCapturing_Signal)
// 		{
// 			Delay_MSec(200);
// 			qDebug() << "msleep" << 200;
// 		}
// 		
// 		m_iFlagIsCapturing_Dark = 1;
// 		emit SignalStartGrabOnce_Dark();
// 		while (m_iFlagIsCapturing_Dark)
// 		{
// 			Delay_MSec(200);
// 			qDebug() << "msleep" << 200;
// 		}
// 		
// 	}
// 	qDebug() << "for quit";
// 	
	return 0;
}

int CMainDataGrabber::GrabOnceFinished()
{
	m_iFlagIsCapturing = false;
// 	QTimer t;
// 	t.start();
// 	while (1)
// 	{
// 		QThread::msleep(1);
// 		QCoreApplication::processEvents();
// 	}
	return 0;
}

 int CMainDataGrabber::OnTimeCounter()
 {
	 if (m_iFlagIsCapturing)
	 {
		 qDebug() << "Fatal Thread Err.";
		 return 1000;
	 }
	 m_iFlagIsCapturing = 1;

	 m_struAcqPosSetting.iTotalPosition = 5;

	 //m_pdfpSaver->WriteDataFile();
	 for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++)
	 {
		 m_pControlThread[i]->ClearBuffer();
	 }

	 for (int i=0;i< m_struAcqPosSetting.iTotalPosition-1;i++)
	 {
		 ////move to
		 m_struAcqPosSetting.iPosition[i + 1];
		 m_iFlagIsCapturing_Signal = 1;
		 emit SignalStartGrabOnce_Signal();
		 while (m_iFlagIsCapturing_Signal)
		 {
			 Delay_MSec(1000);
		 }


		 ////move to
		 m_struAcqPosSetting.iPosition[0];
		 m_iFlagIsCapturing_Dark = 1;
		 emit SignalStartGrabOnce_Dark();
		 while (m_iFlagIsCapturing_Dark)
		 {
			 Delay_MSec(1000);
		 }

	 }

	 std::vector<std::vector<DataFrame>> vecData;
	
	 for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++)
	 {
		 std::vector<DataFrame> vecDark, vecSignal, vecResult;
		 m_pControlThread[i]->GetBuffer(vecDark, vecSignal);
		 for (size_t j=0;j< vecDark.size();j++)
		 {
			 SubDataFrame(vecSignal[j], vecDark[j], m_struDeviceContext.usPixels[i]);
		 }
		 vecData.push_back(vecSignal);
	 }
	 m_pdfpSaver->SetData(vecData);
	 m_pdfpSaver->WriteDataFile();
// 	 qDebug() << "-------------------------------------------busy" << QTime::currentTime().toString();
// 	 QThread::msleep(5000);
// 	 return 0;	 
//  	if (m_iFlagIsCapturing)
//  	{
//  		
//  	}
// 	m_iFlagIsCapturing = 1;
	//qDebug() << "-------------------------------------------return";
	//return 1;
// 	for (int i = 0; i < 5; i++)
// 	{
// 		m_iFlagIsCapturing_Signal = 1;
// 		emit SignalStartGrabOnce_Signal();
// 		while (m_iFlagIsCapturing_Signal)
// 		{
// 			//Delay_MSec(1000);
// 			QThread::msleep(100);
// 			QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
// 			//qDebug() << "msleep" << 1000;
// 		}
// 
// 		m_iFlagIsCapturing_Dark = 1;
// 		emit SignalStartGrabOnce_Dark();
// 		while (m_iFlagIsCapturing_Dark)
// 		{
// 			Delay_MSec(1000);
// 			QThread::msleep(100);
// 			QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
// 			//qDebug() << "msleep" << 1000;
// 		}
// 
// 	}
// 	qDebug() << "for quit";
// 
//  	
//  	emit SignalGrabOnceFinished();
 	return 0;
 }

