#include <crtdbg.h>
#include "Gates.h"

#include "Collect.h"
#include "NDTSRS2A.h"
#include "NDTSRS2AInternal.h"
#include "NDTSRS2AErrors.h"
#include "PropertiesInterface.h"
#include "PropertyInternal.h"
#include "IntPropertyInternal.h"
#include "EnumPropertyInternal.h"
#include "SDKPropertiesCollection.h"
#include "PingPongTable.h"
#include "TCG.h"
#include "Rq.h"

#include "SDKChangeHook.h"

using namespace NDTS::SCO;
using namespace NDTS::RS2A;



GateObject::GateObject(int cardIndex, int gateIndex)
{
	this->cardIndex = cardIndex;
	this->gateIndex = gateIndex;
	memset(&rqGate, 0, sizeof(RQ_Gate));
	rqGate.index = (RQ_GateIndex) gateIndex;
	rqGate.isEnabled = NDTS_BFalse;
}

GateObject::~GateObject(void)
{

}

bool GateObject::setField(GateField field, int value)
{
	switch(field)
	{
		case GF_isEnabled:
			rqGate.isEnabled = (value) ? NDTS_BTrue : NDTS_BFalse;
			break;
		case GF_start:
			rqGate.startCount = (unsigned long)(value);
			break;
		case GF_stop:
			rqGate.stopCount = (unsigned long)(value);
			break;
		case GF_threshold:
			rqGate.threshold = (unsigned long)(value);
			break;
		case GF_triggerGate:
			rqGate.triggerGate = (RQ_GateTrigger)value;
			break;
		case GF_polarity:
			rqGate.thresholdPolarity = (RQ_GateThresholdPolarity)value;
			break;
		case GF_triggerMode:
			rqGate.triggerMode = (RQ_GateTriggerMode)value;
			break;
		case GF_distanceMetric:
			rqGate.distanceMetric = (RQ_GateDistanceMetric)value;
			break;
		case GF_amplitudePeakSelect:
			rqGate.amplitudePeakSelect = (RQ_GateAmplitudePeakSelect)value;
			break;
		case GF_distancePeakSelect:
			rqGate.distancePeakSelect = (RQ_GateDistancePeakSelect)value;
			break;
		default:
			_ASSERTE(0);
			return false;
	}
	NDTS_Boolean isOk;
	isOk = RQ_AddGate(cardIndex, &rqGate);
	return (isOk == NDTS_BTrue)? true : false;
}

int GateObject::getField(GateField field)
{
	switch(field)
	{
		case GF_isEnabled:
			return (int)rqGate.isEnabled;
			break;
		case GF_start:
			return (int)rqGate.startCount;
			break;
		case GF_stop:
			return (int)rqGate.stopCount;
			break;
		case GF_threshold:
			return (int)rqGate.threshold;
			break;
		case GF_triggerGate:
			return (int)rqGate.triggerGate;
			break;
		case GF_polarity:
			return (int)rqGate.thresholdPolarity;
			break;
		case GF_triggerMode:
			return (int)rqGate.triggerMode;
			break;
		case GF_distanceMetric:
			return (int)rqGate.distanceMetric;
			break;
		case GF_amplitudePeakSelect:
			return (int)rqGate.amplitudePeakSelect;
			break;
		case GF_distancePeakSelect:
			return (int)rqGate.distancePeakSelect;
			break;
		default:
			_ASSERTE(0);
			return false;
	}

}

// Represents a single field of a single gate (e.g. gate enable for gate 1 card 1)
GateElement::GateElement(GateObject *pGateObject, GateField field)
{
	this->pGateObject = pGateObject;
	this->field = field;
}
GateElement::~GateElement(void)
{
}

bool GateElement::setValue(int newValue)
{
	return pGateObject->setField(field, newValue);
}
int GateElement::getValue()
{
	return pGateObject->getField(field);
}
