// CCppTreeNodeInfoGenerator.cpp
//
#include "stdafx.h"
#include "CppTreeNodeInfoGenerator.h"
#include "XmlProjectSettings.h"
#include "XmlClassInfo.h"
#include "XmlMemberInfo.h"

CCppTreeNodeInfoGenerator::CCppTreeNodeInfoGenerator()
{}

CCppTreeNodeInfoGenerator::~CCppTreeNodeInfoGenerator()
{}


void CCppTreeNodeInfoGenerator::Generate()
{
	// generate header file
	//
	GenerateHeader();
	
	// generate implementation file
	//
	GenerateImplementation();
}
	
void CCppTreeNodeInfoGenerator::GenerateHeader()
{
	cmString fileName;
	cmString pathName;

	Head_generateHeader();
	Head_generateIfDefBegin();
	Head_generateIncludeSection();
	Head_generateForwardDecl();
	Head_generateClassBegin();
	Head_generateConstructor();
	Head_generateDestructor();
	Head_generateMethods();
	Head_generateClassEnd();
	Head_generateIfDefEnd();

	pathName = BuildGeneratedSourcePath() + _T("include\\");
	fileName = GetClassName() +  _T(".h");
	SaveFile(pathName + fileName, headCode_);

	hGeneratedFiles_[fileName] = pathName + fileName;
}

void CCppTreeNodeInfoGenerator::Head_generateHeader()
{
	headCode_ += _T("// ") + GetClassName() + _T(".h") + xCrLf;
	headCode_ += _T("// ") + xCrLf; 
	headCode_ += GetGenerationTimeStamp();
}

void CCppTreeNodeInfoGenerator::Head_generateIfDefBegin()
{
	headCode_ += _T("#if !defined(") + DefineOf(GetClassName()) + _T(")") + xCrLf;
	headCode_ += _T("#define ") + DefineOf(GetClassName()) + xCrLf;
	headCode_ += xCrLf;
}

void CCppTreeNodeInfoGenerator::Head_generateIncludeSection()
{
	
	headCode_ += _T("#include \"stdafx.h\"") + xCrLf;
	headCode_ += _T("#include \"cmString.h\"") + xCrLf;
	headCode_ += _T("#include \"XmlSerializable.h\"") + xCrLf;
	headCode_ += xCrLf;
}

void CCppTreeNodeInfoGenerator::Head_generateForwardDecl()
{
}

void CCppTreeNodeInfoGenerator::Head_generateClassBegin()
{
	headCode_ += _T("class ") + GetClassName() + xCrLf;
	headCode_ += _T("{") + xCrLf;
}

void CCppTreeNodeInfoGenerator::Head_generateConstructor()
{
	headCode_ += _T("public:") + xCrLf;
	headCode_ += xTab + GetClassName() + _T("();") + xCrLf;
	headCode_ += xTab + GetClassName() + _T("(const ") + GetClassName() + _T("& copy);") + xCrLf;
}

void CCppTreeNodeInfoGenerator::Head_generateDestructor()
{
	headCode_ += xTab + _T("virtual ~") + GetClassName() + _T("();") + xCrLf + xCrLf;
}

void CCppTreeNodeInfoGenerator::Head_generateMethods()
{
	headCode_ += xTab + _T("// Get/Set methods") + xCrLf;
	headCode_ += xTab + _T("//") + xCrLf;
	headCode_ += _T("public:") + xCrLf + xCrLf;

	headCode_ += xTab + _T("void SetTreeItem(HTREEITEM item);") + xCrLf;
	headCode_ += xTab + _T("HTREEITEM GetTreeItem();") + xCrLf;
	headCode_ += xCrLf;
	
	headCode_ += xTab + _T("void SetClassId(DWORD classId);") + xCrLf;
	headCode_ += xTab + _T("DWORD GetClassId();") + xCrLf;
	headCode_ += xCrLf;

	headCode_ += xTab + _T("void SetArrayIndex(int index);") + xCrLf;
	headCode_ += xTab + _T("int GetArrayIndex();") + xCrLf;
	headCode_ += xCrLf;

	headCode_ += xTab + _T("void SetMapKey(const cmString& key);") + xCrLf;
	headCode_ += xTab + _T("cmString GetMapKey();") + xCrLf;
	headCode_ += xCrLf;

	headCode_ += xTab + _T("void SetIconExpandedSelected(int iconIndex);") + xCrLf;
	headCode_ += xTab + _T("int GetIconExpandedSelected() const;") + xCrLf;
	headCode_ += xCrLf;

	headCode_ += xTab + _T("void SetIconExpandedNotSelected(int iconIndex);") + xCrLf;
	headCode_ += xTab + _T("int GetIconExpandedNotSelected() const;") + xCrLf;
	headCode_ += xCrLf;

	headCode_ += xTab + _T("void SetIconCollapsedSelected(int iconIndex);") + xCrLf;
	headCode_ += xTab + _T("int GetIconCollapsedSelected() const;") + xCrLf;
	headCode_ += xCrLf;

	headCode_ += xTab + _T("void SetIconCollapsedNotSelected(int iconIndex);") + xCrLf;
	headCode_ += xTab + _T("int GetIconCollapsedNotSelected() const;") + xCrLf;
	headCode_ += xCrLf;


	headCode_ += _T("protected:") + xCrLf;
	headCode_ += xTab + _T("void Copy(const ") + GetClassName() + _T("& nodeInfo);") + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + _T("HTREEITEM treeItem_;") + xCrLf;
	headCode_ += xTab + _T("DWORD classId_;") + xCrLf;
	headCode_ += xTab + _T("int arrayIndex_;") + xCrLf;
	headCode_ += xTab + _T("cmString mapKey_;") + xCrLf;
	headCode_ += xTab + _T("int iconExpandedSelected_;") + xCrLf;
	headCode_ += xTab + _T("int iconExpandedNotSelected_;") + xCrLf;
	headCode_ += xTab + _T("int iconCollapsedSelected_;") + xCrLf;
	headCode_ += xTab + _T("int iconCollapsedNotSelected_;") + xCrLf;
	headCode_ += xCrLf;
}

void CCppTreeNodeInfoGenerator::Head_generateClassEnd()
{
	headCode_ += xCrLf;
	headCode_ += _T("};") + xCrLf + xCrLf;
}

void CCppTreeNodeInfoGenerator::Head_generateIfDefEnd()
{
	headCode_ += _T("#endif //!defined(") + DefineOf(GetClassName()) + _T(")") + xCrLf + xCrLf;
}

void CCppTreeNodeInfoGenerator::GenerateImplementation()
{
	cmString fileName;
	cmString pathName;

	Impl_generateHeader();
	Impl_generateIncludeSection();
	
	Impl_generateConstructor();
	Impl_generateDestructor();
	Impl_generateMethods();
	
	pathName = BuildGeneratedSourcePath() + _T("src\\");
	fileName = GetClassName() +  _T(".cpp");
	SaveFile(pathName + fileName, implCode_);

	cppGeneratedFiles_[fileName] = pathName + fileName;
}

void CCppTreeNodeInfoGenerator::Impl_generateHeader()
{
	implCode_ += _T("// ") + GetClassName() + _T(".cpp") + xCrLf;
	implCode_ += _T("// ") + xCrLf; 
	implCode_ += GetGenerationTimeStamp();
}

void CCppTreeNodeInfoGenerator::Impl_generateIncludeSection()
{
	implCode_ += _T("#include \"stdafx.h\"") + xCrLf;
	implCode_ += _T("#include \"") + GetClassName() + _T(".h\"") + xCrLf;
	implCode_ +=  xCrLf;
}

void CCppTreeNodeInfoGenerator::Impl_generateConstructor()
{
	// default constructor
	//
	implCode_ += GetClassName() + _T("::") + GetClassName() + _T("():") + xCrLf;
	implCode_ += xTab + _T("treeItem_(NULL),") + xCrLf;
	implCode_ += xTab + _T("classId_(0),") + xCrLf;
	implCode_ += xTab + _T("arrayIndex_(-1),") + xCrLf;
	implCode_ += xTab + _T("iconExpandedSelected_(-1),") + xCrLf;
	implCode_ += xTab + _T("iconExpandedNotSelected_(-1),") + xCrLf;
	implCode_ += xTab + _T("iconCollapsedSelected_(-1),") + xCrLf;
	implCode_ += xTab + _T("iconCollapsedNotSelected_(-1)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// copy constructor
	//
	implCode_ += GetClassName() + _T("::") + GetClassName() + _T("(const ") + GetClassName() + _T("& ") + GetCamelCase(GetClassName()) + _T(")") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("Copy(") + GetCamelCase(GetClassName()) + _T(");") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}

void CCppTreeNodeInfoGenerator::Impl_generateDestructor()
{
	implCode_ += GetClassName() + _T("::~") + GetClassName() + _T("()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
}

void CCppTreeNodeInfoGenerator::Impl_generateMethods()
{
	// Get/Set treeItem
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetTreeItem(HTREEITEM item)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("treeItem_ = item;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("HTREEITEM ") + GetClassName() + _T("::GetTreeItem()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return treeItem_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;
	
	// Get/Set classId
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetClassId(DWORD classId)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("classId_ = classId;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("DWORD ") + GetClassName() + _T("::GetClassId()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return classId_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// Get/Set ArrayIndex
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetArrayIndex(int index)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("arrayIndex_ = index;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("int ") + GetClassName() + _T("::GetArrayIndex()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return arrayIndex_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// Get/Set MapKey
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetMapKey(const cmString& key)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("mapKey_ = key;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("cmString ") + GetClassName() + _T("::GetMapKey()") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return mapKey_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += xCrLf;

	implCode_ += _T("void ") + GetClassName() + _T("::Copy(const ") + GetClassName() + _T("& nodeInfo)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("treeItem_ = nodeInfo.treeItem_;") + xCrLf;
	implCode_ += xTab + _T("classId_ = nodeInfo.classId_;") + xCrLf;
	implCode_ += xTab + _T("arrayIndex_ = nodeInfo.arrayIndex_;") + xCrLf;
	implCode_ += xTab + _T("mapKey_ = nodeInfo.mapKey_;") + xCrLf;
	implCode_ += xTab + _T("iconExpandedSelected_ = nodeInfo.iconExpandedSelected_;") + xCrLf;
	implCode_ += xTab + _T("iconExpandedNotSelected_ = nodeInfo.iconExpandedNotSelected_;") + xCrLf;
	implCode_ += xTab + _T("iconCollapsedSelected_ = nodeInfo.iconCollapsedSelected_;") + xCrLf;
	implCode_ += xTab + _T("iconCollapsedNotSelected_ = nodeInfo.iconCollapsedNotSelected_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// icon expanded + selected
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetIconExpandedSelected(int iconIndex)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("iconExpandedSelected_ = iconIndex;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("int ") + GetClassName() + _T("::GetIconExpandedSelected() const") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return iconExpandedSelected_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// icon expanded + not selected
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetIconExpandedNotSelected(int iconIndex)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("iconExpandedNotSelected_ = iconIndex;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("int ") + GetClassName() + _T("::GetIconExpandedNotSelected() const") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return iconExpandedNotSelected_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// icon collapsed + selected
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetIconCollapsedSelected(int iconIndex)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("iconCollapsedSelected_ = iconIndex;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("int ") + GetClassName() + _T("::GetIconCollapsedSelected() const") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return iconCollapsedSelected_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	// icon collapsed + not selected
	//
	implCode_ += _T("void ") + GetClassName() + _T("::SetIconCollapsedNotSelected(int iconIndex)") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("iconCollapsedNotSelected_ = iconIndex;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

	implCode_ += _T("int ") + GetClassName() + _T("::GetIconCollapsedNotSelected() const") + xCrLf;
	implCode_ += _T("{") + xCrLf;
	implCode_ += xTab + _T("return iconCollapsedNotSelected_;") + xCrLf;
	implCode_ += _T("}") + xCrLf;
	implCode_ += xCrLf;

}

cmString CCppTreeNodeInfoGenerator::GetClassName()
{
	return cppSettings_->GetClassPrefix() + _T("TreeNodeInfo");
}