#include <string.h>

#include "RQ.h"
#include "NDTSRS2AErrors.h"

// Provides a C Style interface to access the gates.
// This completely bypasses the gate Properties and so an application should
// only ever use Properties or this interface to set gates and never both.

#define RQ_MAXIMUM_NUMBER_OF_GATES_NON_SEQUENCED 8

RQ_Gate pGateProperties[RQ_MAXIMUM_NUMBER_OF_GATES_NON_SEQUENCED];

NDTS_Boolean checkGateIndex(int index)
{
	if (index < 0 || index >= RQ_MAXIMUM_NUMBER_OF_GATES_NON_SEQUENCED)
	{
		RS2A_HandleRS2AError(RS2A_InvalidGateIndex) ;
		return NDTS_BFalse;
	}
	return NDTS_BTrue;
}

RS2A_API void CALL_CONV RS2A_InitialiseLegacyGatesInterface()
{
	for (int i=0; i < RQ_MAXIMUM_NUMBER_OF_GATES_NON_SEQUENCED; i++)
	{
		// SDK starts with all gates disabled so it doesn't matter too much what the values are
		memset(&pGateProperties[i], 0, sizeof(RQ_Gate));
		pGateProperties[i].index = (RQ_GateIndex)i;
		pGateProperties[i].isEnabled = NDTS_BFalse;
	}

}

RS2A_API NDTS_Boolean CALL_CONV RS2A_SetGate(const RQ_Gate *pGate)
{
	if (!checkGateIndex(pGate->index))
		return NDTS_BFalse;

	if (memcmp(&pGateProperties[pGate->index], pGate, sizeof(RQ_Gate)) == 0)
		return NDTS_BTrue;

	if (!RQ_AddGate(0, pGate))
		return NDTS_BFalse;

	pGateProperties[pGate->index]= *pGate;
	return NDTS_BTrue;
}

RS2A_API NDTS_Boolean CALL_CONV RS2A_GetGate(int gateIndex, RQ_Gate *pGate)
{
	if (!checkGateIndex(gateIndex))
		return NDTS_BFalse;

	*pGate = pGateProperties[gateIndex];
	return NDTS_BTrue;
}

