#include "RQThreadInternal.h"
#include "RQProtected.h"
#include "RQTErrorInternal.h"
#include <crtdbg.h>

#include "RQRawScanProtected.h"
 
RQ_API NDTS_Boolean RQT_CollectInitialise(unsigned long cardIndex)
{
//	unsigned long bank;
	RQT_State *pThreadState; 

	if (!RQT_Initialise(cardIndex)) 
		return NDTS_BFalse;

	pThreadState = RQT_ReturnThreadStatePointer(cardIndex) ;
	if (!pThreadState) 
		return NDTS_BFalse; 

	RQ_Lock(cardIndex);

	pThreadState->collectState.readBank = 0;
	pThreadState->collectState.rawScansInitialised = 0;
	//for(bank = 0; bank < NUMBER_OF_SNAPSHOT_BANKS; bank++)
	//	RQ_InitialiseRawScan(&(pThreadState->collectState.rawScans[bank]));

	RQ_Unlock(cardIndex);

	return RQT_SetThreadState(cardIndex, RQT_SMSCollect);
}

RQ_API NDTS_Boolean RQT_CollectUninitialise(unsigned long cardIndex)
{
//	unsigned long bank;
	RQT_State *pThreadState = RQT_ReturnThreadStatePointer(cardIndex) ;

	if (!pThreadState) 
		return NDTS_BFalse; 

	// Shut the thread down so it isn't accessing pRawScans below.
	RQT_SetThreadState(cardIndex, RQT_SMSShutdown) ;

	Sleep(100);
	if(pThreadState->collectState.rawScansInitialised == 1)
	{
		RQ_FreeRawScanData(&(pThreadState->collectState.rawScans[0]), RQ_FRSOAll) ;
		RQ_UninitialiseRawScan(&(pThreadState->collectState.rawScans[0]));
		pThreadState->collectState.rawScansInitialised = 0 ;
	}

	if (!RQT_Uninitialise(cardIndex)) 
		return NDTS_BFalse;

	return NDTS_BTrue;
}

RQ_API NDTS_Boolean RQT_CollectReset(unsigned long cardIndex)
{
	RQT_State *pThreadState = RQT_ReturnThreadStatePointer(cardIndex) ;
	if (!pThreadState) 
		return NDTS_BFalse; 

	if(pThreadState->collectState.rawScansInitialised == 1)
	{
		RQ_FreeRawScanData(&(pThreadState->collectState.rawScans[0]), RQ_FRSOAll) ;
		RQ_UninitialiseRawScan(&(pThreadState->collectState.rawScans[0]));
		RQ_ClearRawScan(&(pThreadState->collectState.rawScans[0])) ;
		pThreadState->collectState.rawScansInitialised = 0 ;
	}

	return NDTS_BTrue;
}

RQ_API NDTS_Boolean RQT_GetRawScan(unsigned long cardIndex, RQ_RawScan *pRawScan) 
{
	RQT_State *pThreadState = RQT_ReturnThreadStatePointer(cardIndex) ;

	if (!pThreadState) 
		return NDTS_BFalse; 

	_ASSERTE(pRawScan) ;

	// Must be called outside RQ_Lock so thread can run
	if (!RQT_BankSwapAndUpdate(cardIndex))	
		return NDTS_BFalse;

	RQ_Lock(cardIndex);

	if(pThreadState->collectState.rawScans[0].numberOfBScans == 0)
//	if(pThreadState->collectState.pRawScans[pThreadState->collectState.readBank].numberOfBScans == 0)
	{
		// The raw scan in the read bank has not been filled. 
		RQT_HandleError(RQT_GetRawScan_NoDataAvailableOrBuffered);
		RQ_Unlock(cardIndex);
		return NDTS_BFalse ;
	}

	// Copy the raw scan from the read bank.
	RQ_CopyRawScan(pRawScan, &pThreadState->collectState.rawScans[0]) ;
//	RQ_CopyRawScan(pRawScan, &pThreadState->collectState.pRawScans[pThreadState->collectState.readBank]) ;

	_ASSERTE(pThreadState->collectState.rawScans[0].pInternal) ;
	// Clear the raw scan as it as it's data now 'belongs' to pRawScan.
	RQ_ClearRawScan(&pThreadState->collectState.rawScans[0]) ;
//	RQ_ClearRawScan(&pThreadState->collectState.pRawScans[pThreadState->collectState.readBank]) ;

	// Flip banks.
//	pThreadState->collectState.readBank = pThreadState->collectState.readBank ? 0 : 1;

	RQ_Unlock(cardIndex);

	return NDTS_BTrue;
}
