// FileNode.cpp: implementation of the FileNode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ResourceBrowser.h"
#include "FileNode.h"
#include <Adept\Adept.hpp>
#include <Stuff\Stuff.hpp>

using namespace Stuff;
using namespace Adept;

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

FileNode::FileNode(const char *fname)
{
	FileName=fname;
	Parent=NULL;
	Children.SetLength(8);
	ChildTot=0;
	IsSorted=true;
}

void FileNode::AddChild(FileNode *new_child)
{
	if(new_child->GetParent()) new_child->GetParent()->RemoveChild(new_child);
	new_child->Parent=this;
	
	if(Children.GetLength()<=ChildTot) Children.SetLength(Children.GetLength()+Children.GetLength()/2);
	Children[ChildTot++]=new_child;
	IsSorted=false;
	
}

void FileNode::RemoveChild(int loc)
{
	Verify(loc>=0 && loc<ChildTot);
	Children[loc]->Parent=NULL;
	int i;
	for(i=loc;i<(ChildTot-1);i++) Children[i]=Children[i+1];

}

int FileNode::FindChild(FileNode *child)
{
	Verify(child->GetParent()==this);
	int i;
	for(i=0;i<ChildTot;i++) if(Children[i]==child) return i;
	
	return -1;
}

void FileNode::OrganizeTree()
{
int i;
for(i=0;i<ChildTot;i++)
if(Children[i]) Children[i]->OrganizeTree(this);

}


void FileNode::OrganizeTree(FileNode *root_node)
{
Resource res(FileName);

if(res.DoesResourceExist())
	{
	Stuff::FileDependencies deps;
	res.GetFileDependencies(&deps);
	deps.m_fileNameStream.Rewind();

	while(deps.m_fileNameStream.GetBytesRemaining())
	{
		char *ptr=(char *)deps.m_fileNameStream.GetPointer();
		deps.m_fileNameStream.AdvancePointer(strlen(ptr)+1);
		
		Resource childres(ptr);
		if(childres.DoesResourceExist())
		{
		//Remove From MasterList
		
		//Add To This
		STOP(("Unimplemented"));
		}		
		else
		{
		AddChild(new FileNode(ptr));
		}
	}

	}

}

FileNode::~FileNode()
{
	
}
