// BlockListDoc.cpp : implementation file
//

#include "stdafx.h"
#include "xpeluc01.h"
#include "BlocksDoc.h"
#include "BlockDirectory.h"
#include "BlockItem.h"
#include "BlockItem_Virtual.h"
#include "BlockItem_Input.h"
#include "BlockItem_Output.h"
#include "BlockItem_Real.h"

// CBlockListDoc

//IMPLEMENT_DYNCREATE(CBlocksDoc, CDocument)

CBlocksDoc::CBlocksDoc()
{
}

BOOL CBlocksDoc::Load()
{
	m_pBlockDirRoot = (CBlockDirectory*)CBlockDirectory::CreateObject();

	CBlockItem *blkDef;

	blkDef = CBlockItem_Virtual::CreateInstance();//new CBlockItem_Virtual();
	m_pBlockDirRoot->InsertBlock(L"", blkDef);
	blkDef->Release();//delete blkDef;

	blkDef = CBlockItem_Input::CreateInstance(); //new CBlockItem_Input();
	m_pBlockDirRoot->InsertBlock(L"", blkDef);
	blkDef->Release();// delete blkDef;

	blkDef = CBlockItem_Output::CreateInstance();//new CBlockItem_Output();
	m_pBlockDirRoot->InsertBlock(L"", blkDef);
	blkDef->Release();//delete blkDef;

	WIN32_FIND_DATA findData;
	HANDLE hFind;
	if ( INVALID_HANDLE_VALUE != (hFind = FindFirstFile(L"b*.dll", &findData)) )
	{
		do
		{
			CBlockItem_Real *blkReal;
			blkReal = CBlockItem_Real::CreateInstance();//new CBlockItem_Real();
			if ( blkReal->LoadInterface(findData.cFileName) )
			{
				m_pBlockDirRoot->InsertBlock(blkReal->GetBlockPath(), blkReal);
			}
			blkReal->Release();// delete(blkReal);
		} while ( FindNextFile(hFind, &findData) );
	}

	return TRUE;
}

CBlockDirectory* CBlocksDoc::GetRootDirectory()
{
	return m_pBlockDirRoot;
}

CBlocksDoc::~CBlocksDoc()
{
	delete m_pBlockDirRoot;
}