#include <crtdbg.h>
#include <atlstr.h>
#include "GroupProperty.h"
#include "EnumProperty.h"
#include "SIProperty.h"
#include "EventProperty.h"

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace NDTS::Managed::SCO;

GroupProperty::GroupProperty():Property()
{
}
GroupProperty::~GroupProperty(){};

int GroupProperty::getNumberOfProperties() 
{ 
	return dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getNumberOfProperties(); 
}

Property ^GroupProperty::getPropertyByNumber(int index) 
{ 
	NDTS::SCO::Property *pCurrentProp = dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getPropertyByNumber(index);
	return getPropertyAsChildObject(pCurrentProp);
}

Property ^GroupProperty::getPropertyByName(String ^s) 
{ 
	// Convert the .net string to a char * via an MFC style CString that is from a template rather than MFC
    CStringA cstr(s);
    char* ptr = (char*)(const char*)cstr;    
	NDTS::SCO::Property *pCurrentProp = dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getPropertyByName(ptr);

	return getPropertyAsChildObject(pCurrentProp);
}


// --------------- Gang added Int, Enum, SI, Event Properties 29/08/08 --------------------------
int GroupProperty::getFlatNumberOfIntProperties()
{ 	 
	return dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getFlatNumberOfIntProperties();
}

IntProperty ^GroupProperty::getFlatIntPropertyByNumber(int index) 
{
	NDTS::SCO::IntProperty *pCurrentProp = dynamic_cast<NDTS::SCO::GroupProperty *>(pProp)->getFlatPropertyLocal<NDTS::SCO::IntProperty>(index);
	return dynamic_cast<IntProperty^>(getPropertyAsChildObject(pCurrentProp));
}

int GroupProperty::getFlatNumberOfEnumProperties()
{ 	 
	return dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getFlatNumberOfEnumProperties();
}

EnumProperty ^GroupProperty::getFlatEnumPropertyByNumber(int index) 
{
	NDTS::SCO::EnumProperty *pCurrentProp = dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getFlatPropertyLocal<NDTS::SCO::EnumProperty>(index);
	return dynamic_cast<EnumProperty^>(getPropertyAsChildObject(pCurrentProp));
}

int GroupProperty::getFlatNumberOfSIProperties()
{ 	 
	return dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getFlatNumberOfSIProperties();
}

SIProperty ^GroupProperty::getFlatSIPropertyByNumber(int index) 
{
	NDTS::SCO::SIProperty *pCurrentProp = dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getFlatPropertyLocal<NDTS::SCO::SIProperty>(index);
	return dynamic_cast<SIProperty^>(getPropertyAsChildObject(pCurrentProp));
}

int GroupProperty::getFlatNumberOfEventProperties()
{ 	 
	return dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getFlatNumberOfEventProperties();
}

EventProperty ^GroupProperty::getFlatEventPropertyByNumber(int index)
{
	NDTS::SCO::EventProperty *pCurrentProp = dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getFlatPropertyLocal<NDTS::SCO::EventProperty>(index);
	return dynamic_cast<EventProperty^>(getPropertyAsChildObject(pCurrentProp));
}
// --------------- Gang added Int, Enum, SI, Event Properties 29/08/08 --------------------------


Property ^GroupProperty::getPropertyAsChildObject(NDTS::SCO::Property *pCurrentProp) 
{
	// Is there a neater way than this to convert the object to a .net one?
	NDTS::SCO::GroupProperty *pCurrentAsGroup = dynamic_cast<NDTS::SCO::GroupProperty*>(pCurrentProp);
	if (pCurrentAsGroup)
	{
		GroupProperty ^retVal = gcnew GroupProperty;
		retVal->setProp(pCurrentProp); 
		return retVal;
	}
	NDTS::SCO::EnumProperty *pCurrentAsEnum = dynamic_cast<NDTS::SCO::EnumProperty*>(pCurrentProp);
	if (pCurrentAsEnum)
	{
		EnumProperty ^retVal = gcnew EnumProperty;
		retVal->setProp(pCurrentProp); 
		return retVal;
	}		
	NDTS::SCO::IntProperty *pCurrentAsInt = dynamic_cast<NDTS::SCO::IntProperty*>(pCurrentProp);
	if (pCurrentAsInt)
	{
		IntProperty ^retVal = gcnew IntProperty;
		retVal->setProp(pCurrentProp); 
		return retVal;
	}
	NDTS::SCO::EventProperty *pCurrentAsEvent = dynamic_cast<NDTS::SCO::EventProperty*>(pCurrentProp);
	if (pCurrentAsEvent)
	{
		EventProperty ^retVal = gcnew EventProperty;
		retVal->setProp(pCurrentProp); 
		return retVal;
	}
	NDTS::SCO::SIProperty *pCurrentAsSI = dynamic_cast<NDTS::SCO::SIProperty*>(pCurrentProp);
	if (pCurrentAsSI)
	{
		SIProperty ^retVal = gcnew SIProperty;
		retVal->setProp(pCurrentProp); 
		return retVal;
	}
	else
	{
		Property ^retVal = gcnew Property;
		retVal->setProp(pCurrentProp); 
		return retVal;
	}
}

void GroupProperty::beginGroupChange()
{
	dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->beginGroupChange(); 
}

void GroupProperty::commitGroupChange()
{
	dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->commitGroupChange(); 
}


bool GroupProperty::getIsArray()
{
	return dynamic_cast<NDTS::SCO::GroupProperty*>(pProp)->getIsArray(); 
}

