#include "ThreadDataGrabber.h"

ZZ_DataGrabberThread::ZZ_DataGrabberThread(QObject* parent /*= nullptr*/)
{
	m_GrabTimer = new QTimer(this);
	m_bFlagIsCollecting = false;
	


	ZZ_ATPAbsCtrlThread* m_p1 = new ZZ_iSIFControlThread;
	m_pControlThread.push_back(m_p1);
	ZZ_ATPAbsCtrlThread* m_p2 = new ZZ_IS1ControlThread;
	m_pControlThread.push_back(m_p2);
	//m_iTotalThreads = 2;

	InitThreadStatus();
	SetupSignals();
	StartGrabberThread();
}

ZZ_DataGrabberThread::~ZZ_DataGrabberThread()
{
	if (m_piFlagCaptureThreadStatus!=NULL)
	{
		delete m_piFlagCaptureThreadStatus;
	}


}

void ZZ_DataGrabberThread::SetGrabTimer(AcqTimeSettings struAcqTimer)
{
	m_struAcqTime = struAcqTimer;
}

int ZZ_DataGrabberThread::SetupSignals()
{
	connect(m_GrabTimer, &QTimer::timeout, this, &ZZ_DataGrabberThread::OnTimeCounter);

	connect(this, &ZZ_DataGrabberThread::SignalStartGrab, this, &ZZ_DataGrabberThread::StartGrab);

	//connect(this, &ZZ_DataGrabberThread::SignalStartGrabOnce, &m_tcISIFCtrlThread, &ZZ_ATPAbsCtrlThread::StartTestAcquisition);
	//connect(this, &ZZ_DataGrabberThread::SignalStartGrabOnce, &m_tcIS1CtrlThread,  &ZZ_ATPAbsCtrlThread::StartTestAcquisition);

	connect(this, &ZZ_DataGrabberThread::SignalStartGrabOnce, m_pControlThread[0], &ZZ_ATPAbsCtrlThread::StartTestAcquisition);
	connect(this, &ZZ_DataGrabberThread::SignalStartGrabOnce, m_pControlThread[1], &ZZ_ATPAbsCtrlThread::StartTestAcquisition);

	//connect(this, &ZZ_DataGrabberThread::SignalStopGrab, &m_tcISIFCtrlThread, &ZZ_iSIFControlThread::StopTestAcquisition, Qt::DirectConnection);
    //connect(this, &ZZ_DataGrabberThread::SignalStopGrab, &m_tcIS1CtrlThread, &ZZ_IS1ControlThread::StopTestAcquisition, Qt::DirectConnection);

	//connect(&m_tcISIFCtrlThread, &ZZ_iSIFControlThread::SignalAcqFinished, this, &ZZ_DataGrabberThread::HandleThreadEvent);
	//connect(&m_tcIS1CtrlThread, &ZZ_IS1ControlThread::SignalAcqFinished, this, &ZZ_DataGrabberThread::HandleThreadEvent);

	connect(m_pControlThread[0], &ZZ_ATPAbsCtrlThread::SignalAcqFinished, this, &ZZ_DataGrabberThread::HandleThreadEvent);
	connect(m_pControlThread[1], &ZZ_ATPAbsCtrlThread::SignalAcqFinished, this, &ZZ_DataGrabberThread::HandleThreadEvent);

	connect(this, &ZZ_DataGrabberThread::SignalGrabOnceFinished, this, &ZZ_DataGrabberThread::TestGrabOnce);
	//m_tcISIFCtrlThread.SetWaitCondition(&m_wcWaitISIF,&m_mTestMutex);
	//m_tcIS1CtrlThread.SetWaitCondition(&m_wcWaitIS1, &m_mTestMutex);
	return 0;
}

int ZZ_DataGrabberThread::StartGrabCall()
{
	emit SignalStartGrab();
	return 0;
}

int ZZ_DataGrabberThread::StartGrab()
{
	m_GrabTimer->start(1000);
	return 0;
}

int ZZ_DataGrabberThread::StartGrabberThread()
{
// 	m_pqISIFThreadHolder = new QThread();
// 	m_tcISIFCtrlThread.moveToThread(m_pqISIFThreadHolder);
// 	m_pqISIFThreadHolder->start();
// 
// 	m_pqIS1ThreadHolder = new QThread();
// 	m_tcIS1CtrlThread.moveToThread(m_pqIS1ThreadHolder);
// 	m_pqIS1ThreadHolder->start();


	m_pqISIFThreadHolder = new QThread();
	m_pControlThread[0]->moveToThread(m_pqISIFThreadHolder);
	m_pqISIFThreadHolder->start();

	m_pqIS1ThreadHolder = new QThread();
	m_pControlThread[1]->moveToThread(m_pqIS1ThreadHolder);
	m_pqIS1ThreadHolder->start();
	//m_stCameraServiceThread.SetController(this);
	return 0;
}

int ZZ_DataGrabberThread::InitThreadStatus()
{
	m_piFlagCaptureThreadStatus = new int[m_iTotalThreads];
	for (int i=0;i< m_iTotalThreads;i++)
	{
		m_piFlagCaptureThreadStatus[i] = 0;
	}
	return 0;
}

int ZZ_DataGrabberThread::TestGrab()
{
	m_bFlagIsCollecting = true;
	while (m_bFlagIsCollecting)
	{
		
		emit SignalStartGrabOnce();
		//m_mTestMutex.lock();
		//m_wcWaitISIF.wait(&m_mTestMutex);
		//m_mTestMutex.lock();
		//m_wcWaitIS1.wait(&m_mTestMutex);
		//m_mTestMutex.unlock();
	}

	return 0;
}

int ZZ_DataGrabberThread::TestGrabOnce()
{
	InitThreadStatus();
	emit SignalStartGrabOnce();
	return 0;
}

int ZZ_DataGrabberThread::TestStopGrab()
{
	//emit SignalStopGrab();
	m_tcISIFCtrlThread.StopTestAcquisition();
	m_tcIS1CtrlThread.StopTestAcquisition();

	return 0;
}

int ZZ_DataGrabberThread::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 ZZ_DataGrabberThread::OnTimeCounter()
{
	qDebug() << "OnTimeCounter";
	return 0;
}
