#include "MainDataGrabber.h"

CMainDataGrabber::CMainDataGrabber(QObject* parent /*= nullptr*/)
{
	m_piFlagCaptureThreadStatus = NULL;
}

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!=NULL)
	{
		delete m_piFlagCaptureThreadStatus;
	}
}

int CMainDataGrabber::SetGrabberParams(RunTimeGrabberParams struGrabberRTParams)
{
	m_struAcqTime = struGrabberRTParams.atsParams;
	m_struDeviceContext = struGrabberRTParams.fscParams;
	m_iTotalThreads = m_struDeviceContext.ucDeviceNumber;
	return 0;
}

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

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

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

		CAbstractFSController* m_pWorkThread = new CAbstractFSController;
		m_pWorkThread->SetRunParas(i, fsInfo);
		//m_pWorkThread->InitializeFSControl();
		m_pControlThread.push_back(m_pWorkThread);
	}
	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, m_pControlThread[i], &CAbstractFSController::StartAcquisition);
		connect(m_pControlThread[i], &CAbstractFSController::SignalAcqFinished, this, &CMainDataGrabber::HandleThreadEvent);
	}

	connect(this, &CMainDataGrabber::SignalGrabOnceFinished, this, &CMainDataGrabber::GrabFinished);
	
	return 0;
}

int CMainDataGrabber::HandleThreadEvent(int iThreadID, int iFlagStatus)
{
	m_piFlagCaptureThreadStatus[iThreadID] = iFlagStatus;

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

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

int CMainDataGrabber::StartGrab()
{
	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;
	return 0;
}

int CMainDataGrabber::GrabFinished()
{
	m_iFlagIsCapturing = false;
	return 0;
}

