#include "AbstractFSController.h"
CAbstractFSController::CAbstractFSController(QObject* parent /*= nullptr*/)
{
	m_pFSCtrl = NULL;
	m_iThreadID = -1;
}

CAbstractFSController::~CAbstractFSController()
{
	if (m_pFSCtrl!= 0 )
	{
		delete m_pFSCtrl;
	}
}

int CAbstractFSController::SetRunParas(int iThreadID, FSInfo fsInfo)
{
	m_iThreadID = iThreadID;
	m_fsInfo = fsInfo;
	return 0;
}



int CAbstractFSController::InitializeFSControl()
{
	using namespace ZZ_MISCDEF::IRIS;
	if (m_iThreadID == -1/*|| m_iDeviceType == -1*/)
	{
		qDebug() << "Params Err. Call SetRunParas first";
		return 1;
	}
	switch (m_fsInfo.ucDeviceModel)
	{
	case DeviceModel::OSIFAlpha:  
		m_pFSCtrl = new OceanOptics_lib;
		if (m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, m_fsInfo.strSN) != 0)
		{
			qDebug() << "OSIFAlpha Not Opened";
			return 2;
		}
		break;
	case DeviceModel::OSIFBeta:
		m_pFSCtrl = new OceanOptics_lib;
// 		if (m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, m_fsInfo.strSN) !=0)
// 		{
// 			qDebug() << "OSIFBeta Not Opened";
// 			return 2;
// 		}
		break;
	case DeviceModel::ISIF: 
		m_pFSCtrl = new ZZ_ATPControl_Serial_Qt;
		m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, NULL);
		if (m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, m_fsInfo.strSN) != 0)
		{
			qDebug() << "ISIF Not Opened";
			return 3;
		}
		break;
	case DeviceModel::IS1:
		m_pFSCtrl = new ZZ_ATPControl_Serial_Qt;
		m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, NULL);
		if (m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, m_fsInfo.strSN) != 0)
		{
			qDebug() << "IS1 Not Opened";
			return 3;
		}
		break;
	case DeviceModel::IS2:
		m_pFSCtrl = new ZZ_ATPControl_Serial_Qt;
		m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, NULL);
		if (m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, m_fsInfo.strSN) != 0)
		{
			qDebug() << "IS2 Not Opened";
			return 3;
		}
		break;
	default: 
		break;
	}
	return 0;
}

int CAbstractFSController::PerformAutoExposure()
{
	qDebug() << "Starting PerformAutoExposure" << " Thread ID:" << m_iThreadID;
	QThread::msleep(5000);
	qDebug() << "Stop PerformAutoExposure" << " Thread ID:" << m_iThreadID;
	//emit SignalAcqFinished(m_iThreadID, 1);
	return 0;
}

int CAbstractFSController::TakeDarkFrame()
{
	qDebug() << "Starting TakeDarkFrame" << " Thread ID:" << m_iThreadID;
	QThread::msleep(1000);
	qDebug() << "Stop TakeDarkFrame" << " Thread ID:" << m_iThreadID;
	//emit SignalAcqFinished(m_iThreadID, 1);
	return 0;
}

int CAbstractFSController::TakeSignal()
{
	qDebug() << "Starting TakeSignal" << " Thread ID:" << m_iThreadID;
	QThread::msleep(5000);
	qDebug() << "Stop TakeSignal" << " Thread ID:" << m_iThreadID;
	//emit SignalAcqFinished(m_iThreadID, 1);
	return 0;
}

int CAbstractFSController::SaveDataFile()
{
	return 0;
}

int CAbstractFSController::StartAcquisitionSignal()
{
	//
	qDebug() << "Starting acq Signal" << " Thread ID:" << m_iThreadID;
// 	DataFrame struDF;
// 	int iii;
// 	m_pFSCtrl->SetExposureTime(10000000);
// 	m_pFSCtrl->GetExposureTime(iii);
// 	m_pFSCtrl->SingleShot(struDF);
	PerformAutoExposure();
	TakeSignal();

	qDebug() << "Stop acq Signal" << " Thread ID:" << m_iThreadID;
	emit SignalAcqFinished_Signal(m_iThreadID, 1);
	return 0;
}

int CAbstractFSController::StartAcquisitionDark()
{
	qDebug() << "Starting acq Dark" << " Thread ID:" << m_iThreadID;
	TakeDarkFrame();
	qDebug() << "Stop acq Dark"<< " Thread ID:" << m_iThreadID;
	emit SignalAcqFinished_Dark(m_iThreadID, 1);

	return 0;
}

int CAbstractFSController::StopAcquisition()
{
	return 0;
}



