#include <Mstn\MdlApi\MdlApi.h>
#include <Mstn\ISessionMgr.h>
#include <Mstn\MdlApi\mselems.h>

USING_NAMESPACE_BENTLEY_DGNPLATFORM;
USING_NAMESPACE_BENTLEY_MSTNPLATFORM;
USING_NAMESPACE_BENTLEY_MSTNPLATFORM_ELEMENT;

extern "C" DLLEXPORT WChar** GetDgnlibLevelNames(int& namesCnt)
{
	FileLevelCacheR fileLvlCac = ISessionMgr::GetManager().GetActiveDgnFile()->GetLevelCacheR();
	T_LevelCachePtrVector*   t_lvlCacPtrVec = fileLvlCac.GetLibraries();
	namesCnt = 0;
	for (FileLevelCachePtr* fileLvlCacPtr = t_lvlCacPtrVec->begin(); fileLvlCacPtr != t_lvlCacPtrVec->end();fileLvlCacPtr++)
	{
		namesCnt += (int)fileLvlCacPtr->GetCR()->GetLevelCount();
	}
	WChar** rtnNamePP = new WChar*[namesCnt];
	int i = 0;
	for (FileLevelCachePtr* fileLvlCacPtr = t_lvlCacPtrVec->begin(); fileLvlCacPtr != t_lvlCacPtrVec->end(); fileLvlCacPtr++)
	{
		for (LevelHandle lvlHan = fileLvlCacPtr->GetCR()->begin(); lvlHan != fileLvlCacPtr->GetCR()->end(); i++, ++lvlHan)
		{
			WString lvlName(lvlHan.GetName());
			rtnNamePP[i] = new WChar[lvlName.length()];
			size_t nameLen = lvlName.length();
			nameLen = 0;
			memcpy(rtnNamePP[i], lvlName.GetWCharCP(), (lvlName.length() + 1) * 2);
		}
	}
	return rtnNamePP;
}

extern "C" DLLEXPORT void ReleaseDgnlibLevelNames(WChar** namepp, int namesCnt)
{
	for (int i = 0; i < namesCnt; i++)
	{
		delete[] namepp[i];
	}
	delete[] namepp;
}

extern "C" DLLEXPORT  void MdlMain(int argc, WCharCP argv[])
{
    RscFileHandle   rscFileH;  
    if (mdlResource_openFile (&rscFileH, NULL, RSC_READ) != SUCCESS)
    {
        mdlSystem_exit( ERROR, 1);
	}
	return;
}

