#pragma once
#include"pch.h"
#include <vector>
#include "AbstractFSController.h"
#include "DataFileProcessor.h"
#include "Scheduler.h"
#include "VSMD12XControl.h"
#include "MainDataUploader.h"

using namespace std;
using namespace ZZ_MISCDEF::ZZ_RUNPARAMS;

//Q_DECLARE_METATYPE(vector<vector<DataFrame>>)

class CMainDataGrabber :public QObject
{
	Q_OBJECT
public:
	CMainDataGrabber(QObject* parent = nullptr);
	~CMainDataGrabber();	

private:
	vector<CAbstractFSController *> m_pControlThread;
	vector<QThread* >               m_pqThreadHolder;

	int *m_piFlagCaptureThreadStatus_Signal;
	int *m_piFlagCaptureThreadStatus_Dark;

	int m_iTotalThreads;

	int m_iFlagIsCapturing;
	int m_iFlagIsCapturing_Signal;
	int m_iFlagIsCapturing_Dark;

	DataFileProcessor * m_pdfpSaver;
	Scheduler         * m_psScheduler;
	CMainDataUploader * m_pmduUploader;

	CVSMD12XControl m_ctrlLS;

	FSContext            m_struDeviceContext;
	LSContext            m_struLinearShutterContext;
	AcqPosSettings       m_struAcqPosSetting;
	AcqTimeSettings      m_struAcqTime;
	RunTimeGrabberParams m_struRTGP;
	
	//20220422 added for dual-shutter system  
	vector<DSStatus> m_vecDSS;

	//QTimer *m_GrabTimer;
private:
	void Delay_MSec(ZZ_U16 usMS);
	void SubDataFrame(DataFrame& dfLeft, DataFrame const dfRight,int iCount);

	//Call this first
	int SetGrabberParams(RunTimeGrabberParams struGrabberRTParams);
	int SetGrabberFileProcessor(DataFileProcessor &dfpSaver);
	int SetTimer(Scheduler &sScheduler);
	int SetUploader(CMainDataUploader &mduUploader/*, RunTimeGrabberParams struGrabberRTParams*/);

	int InitThreadStatus();
	int ResetThreadStatus();
	int InitializeWorkers();
	int StartWorkers();
	int SetupMsgPipelines();
	int SetupMsgPipelines_RunTime();
	int StartGrab();

	
public:
	int SetContext(RunTimeGrabberParams struGrabberRTParams, DataFileProcessor &dfpSaver, Scheduler &sScheduler);
	int SetContext(RunTimeGrabberParams struGrabberRTParams, DataFileProcessor &dfpSaver, Scheduler &sScheduler,CMainDataUploader &mduUploader);
	void Init_Normal();
	void Init_Self();

public slots:
	//int InitLS_Self(/*RunTimeGrabberParams struGrabberRTParams, DataFileProcessor &dfpSaver, Scheduler &sScheduler*/);
	int InitLS();
	int StartGrabTimer();
	int OnTimeCounter();
	int OnZeroHoldCurrent();

	int HandleThreadEvent_Signal(int iThreadID, int iFlagStatus);
	int HandleThreadEvent_Dark(int iThreadID, int iFlagStatus);
	
	int GrabOnceFinished_Signal();
	int GrabOnceFinished_Dark();
	int GrabOnceFinished();
	
signals:
	void SignalStartGrabOnce_Signal();
	void SignalStartGrabOnce_Dark();

	void SignalGrabOnceFinished_Signal();
	void SignalGrabOnceFinished_Dark();
	void SignalGrabOnceFinished();


	void SignalStopGrab();
	void SignalStartGrab();

	void SignalLSInit();
	void SignalPushOneDataFrame();
	//void SignalPushOneDataFrame(std::vector<std::vector<DataFrame>> vecData);
};