/******************************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. All rights 
** reserved.
**
** File   : DrawableConcept.cpp
**
** Author : Satish Hota
**
** Description:
**    This file provides implementation for CDrawableConcept class.
**
** History:
** 27-Nov-01 Satish Hota
**     File Created. 
** 01-Dec-01 Satish Hota
**     Overrode the GetText method and implemented the same.
** 02-Dec-01 Satish Hota
**     Modified syntax of string in GetText method.
** 02-Dec-01 Satish Hota
**     Again, modified syntax of string in GetText method.
** 02-Dec-01 Edwin Chan, Satish Hota
**     Added and implemented OnEditProperties, DoJoin and DoDetach methods.
******************************************************************************/

#include "stdafx.h"
#include "ConGraT.h"
#include "DrawableConcept.h"
#include "CGEngine/CSheetOfAssertion.h"
#include "CGEngine/CKnowledgeBase.h"
#include "DrawableCGFactory.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDrawableConcept::CDrawableConcept()
{

}

CDrawableConcept::~CDrawableConcept()
{

}

long CDrawableConcept::GetText(std::string &rstrText)
{
    std::string strTypeName, strName, strLabel, strMarker;

    // Get type name
    strTypeName = GetTypeDef()->GetTypeName();
    
    // If the concept has a label, append it to the text.
    strName = GetName();
    strLabel = GetLabel();
    strMarker = GetIndividualMarker();

	rstrText = strTypeName;
	if(!strName.empty() || !strMarker.empty() || !strLabel.empty())
	{
		rstrText += " : ";
	}
    if(!strName.empty())
    {
		rstrText += strName;
	}
    if(!strMarker.empty())
    {
		rstrText += strMarker;
	}
    if(!strLabel.empty())
    {
        rstrText += "*" + strLabel;
    }

    return CG_S_OK;
}

void CDrawableConcept::OnEditProperties(CmdType eCmd)
{
	CTypeDefinition *pTypeDefinition;
	std::string strName, strTypeName;

	strName = GetName();
    
	pTypeDefinition = GetTypeDef();
	strTypeName = pTypeDefinition->GetTypeName();

	ACConstruct *pConstruct = GetParent();

	CSheetOfAssertion *pSOA = dynamic_cast<CSheetOfAssertion*> (pConstruct);

	if(!pSOA)
	{
		ASSERT(0);
		return;
	}

	pConstruct = pSOA->GetParent();

	CKnowledgeBase *pKB = dynamic_cast<CKnowledgeBase*> (pConstruct);

	if(!pKB)
	{
		ASSERT(0);
		return;
	}

	CTypeHierarchy *pTH;
	pTH = pKB->GetTypes();

	const CTypeHierarchy::TypeDefMap *pTypesMap;
	CTypeHierarchy::TypeDefMap::const_iterator iterTypes;
	pTypesMap = pTH->GetTypes();

	std::vector<std::string> vectorStrings;
	std::string tempStr;
	int nCursor = 0;
	bool bFound = false;
	for(iterTypes = pTypesMap->begin();iterTypes != pTypesMap->end(); iterTypes++)
	{
		tempStr = (*iterTypes).second->GetTypeName();
		vectorStrings.push_back(tempStr);
		if(tempStr == strTypeName)
		{
			bFound = true;
		}
		if(!bFound)
		{
			nCursor++;
		}
	}

    CPropSheet ps(_T("Properties Sheet"));
    ps.m_PropPage.m_strRef = strName.c_str();
	ps.m_PropPage.m_strSelected = strTypeName;
	ps.m_PropPage.SetTypes(vectorStrings, nCursor);


   
    if(eCmd==CmdType_Restrict)
        ps.Construct( _T("Restrict") );
    else
        ps.Construct( _T("Unrestrict") );

	std::string strSelectedType;
	if (ps.DoModal() == IDOK)
    {
        strName = ps.m_PropPage.m_strRef;
        SetName(strName);
		strSelectedType = ps.m_PropPage.m_strSelected;
		pTypeDefinition = pTH->FindByName(strSelectedType);
		SetTypeDef(pTypeDefinition);
    }
    else
        return;

///////////////////////////////////////////////////

}

void CDrawableConcept::DoJoin(CDrawableElement *pInElement)
{
	CConcept *pInConcept = dynamic_cast<CConcept*> (pInElement);
	if(!pInElement)
		return;
	Join(pInConcept);
}

void CDrawableConcept::DoDetach()
{
	CDrawableCGFactory oFactory;
	ACCGFactory *pFactory = dynamic_cast<ACCGFactory*> (&oFactory);

	Detach(pFactory);
}
