/******************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. 
** All rights reserved.
**
** File   : CTypeDefinition.cpp
**
** Author : David Phillips
**
** Description:
**    This file provides implementation for the CTypeDefinition
**    class.
**
** History:
**    24-Nov-2001 : David Phillips
**        Created file.
******************************************************************/


#include "CGEngine\CTypeDefinition.h"
#include "CGEngine\CTypeHierarchy.h"
#include "ClipsInterface\Class.h"

// Class CTypeDefinition 

CTypeDefinition::CTypeDefinition(std::string name)
  : m_name(name), m_bPrimitive(false)
{
	std::cout << "Creating Type Definition => " << m_name << std::endl;
}

CTypeDefinition::CTypeDefinition(const CTypeDefinition &right)
{
}


CTypeDefinition::~CTypeDefinition()
{
}


CTypeDefinition & CTypeDefinition::operator=(const CTypeDefinition &right)
{
	throw ACConstruct::Not_Implemented_Yet();
}


int CTypeDefinition::operator==(const CTypeDefinition &right) const
{
	throw ACConstruct::Not_Implemented_Yet();
}

int CTypeDefinition::operator!=(const CTypeDefinition &right) const
{
	throw ACConstruct::Not_Implemented_Yet();
}



//## Other Operations (implementation)
void CTypeDefinition::AddLambdaElement (std::string String)
{
	throw ACConstruct::Not_Implemented_Yet();
}

//## Other Operations (implementation)
void CTypeDefinition::AddSuperclass (CTypeDefinition *pSuperclass)
{
	m_superclass_vector.push_back(pSuperclass);
	pSuperclass->m_subclass_vector.push_back(this);
}

void  CTypeDefinition::Commit ()
{

	std::cout << "Processing commit for " << m_name << std::endl;
    
	m_clipClass = new CCLPClass;
	
	// Send current class to CLIPS

	m_clipClass->SetName("CG_CONCEPT_" + m_name);

	if (m_name.compare("Entity") == 0)
	{
        m_clipClass->Define("USER");
	}
	else
	{
		TypeDefVector::iterator iterSuperclass;
		iterSuperclass = m_superclass_vector.begin();

		std::string sSuperclass;

		if (iterSuperclass == m_superclass_vector.end())
		{
			const CTypeHierarchy* th = dynamic_cast<const CTypeHierarchy*>(GetParent());
			CTypeDefinition* pEntity = th->FindByName("Entity");
			AddSuperclass(pEntity);
			m_bPrimitive = TRUE;
			sSuperclass = "Entity";
		}
		else
		{
			sSuperclass = (*iterSuperclass)->m_name;
		}

		// NOTE:  Need to be able to install multiple superclasses
		m_clipClass->Define("CG_CONCEPT_" + sSuperclass);
	}

    // Commit Subclasses
	TypeDefVector::iterator iterSubclass;
	iterSubclass = m_subclass_vector.begin();

	while (iterSubclass != m_subclass_vector.end())
	{
		std::cout << "Calling commit for " << (*iterSubclass)->GetTypeName().c_str() << std::endl;
        (*iterSubclass)->Commit();
		iterSubclass++;
	}

}

void CTypeDefinition::Browse()
{
	m_clipClass->BrowseClass();
}
