#include "stdafx.h"
#include "effectdata.hpp"
#include "contents.hpp"

void AddEffectDirectory (CTreeCtrl& EffectList,WIN32_FIND_DATA* dirdata,HTREEITEM parent);

void AddEffectList (CTreeCtrl& EffectList)
{
	WIN32_FIND_DATA data;
	HANDLE findhandle;
	CString name;

	findhandle = FindFirstFile ("content\\effects\\*.*",&data);
	if (findhandle != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (data.cFileName[0] == '.')
				continue;
			if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				HTREEITEM tree;
				tree = EffectList.InsertItem (data.cFileName);
				EffectList.SetItemData (tree,(DWORD) 0);
				AddEffectDirectory (EffectList,&data,tree);
				EffectList.Expand (tree,TVE_EXPAND);
			}
		}
		while (FindNextFile (findhandle,&data));
	}
}

void AddEffectDirectory (CTreeCtrl& EffectList,WIN32_FIND_DATA* dirdata,HTREEITEM parent)
{
	HANDLE findhandle;
	WIN32_FIND_DATA data;
	CString name,file,path;
	CEffectData *effdata;
	CContentData *contdata;

	name = "content\\effects\\";
	name += dirdata->cFileName;
	path = name;
	name += "\\*.data";
	findhandle = FindFirstFile (name,&data);
	if (findhandle != INVALID_HANDLE_VALUE)
	{
		do
		{
			HTREEITEM item;
			item = EffectList.InsertItem (data.cFileName,parent);
			file = path;
			effdata = new CEffectData (path);
			file += "\\";
			file += data.cFileName;
			effdata->ReadData (data.cFileName);
			EffectList.SetItemData (item,(DWORD) effdata);
		} while (FindNextFile (findhandle,&data)  );
	}
	
	name = "content\\effects\\";
	name += dirdata->cFileName;
	path = name;
	name += "\\*.contents";
	findhandle = FindFirstFile (name,&data);
	if (findhandle != INVALID_HANDLE_VALUE)
	{
		do
		{
			HTREEITEM item;
			item = EffectList.InsertItem (data.cFileName,parent);
			file = path;
			contdata = new CContentData (path);
			file += "\\";
			file += data.cFileName;
			contdata->ReadData (data.cFileName);
			EffectList.SetItemData (item,(DWORD) contdata);
		} while (FindNextFile (findhandle,&data)  );
	}

}

void KillEffectListHelper (CTreeCtrl &EffectList,HTREEITEM root)
{
	CDataBase *data;
	CEffectData *effdata;
	CContentData *contdata;
	HTREEITEM item;

	item = EffectList.GetChildItem (root);
	if (item == NULL)
		return;
	do
	{
		if (EffectList.ItemHasChildren (item))
		{
			KillEffectListHelper (EffectList,item);
		}
		data = (CDataBase *) EffectList.GetItemData (item);
		effdata = dynamic_cast<CEffectData *> (data);
		contdata = dynamic_cast<CContentData *> (data);
		if (effdata)
			effdata->WriteData ();
		else if (contdata)
			contdata->WriteData ();
		delete data;
		EffectList.SetItemData (item,0);
		item = EffectList.GetNextSiblingItem (item);
	}
	while (item);
}

void KillEffectList (CTreeCtrl &EffectList)
{
	HTREEITEM root;
	CDataBase *data;
	CEffectData *effdata;
	CContentData *contdata;

	root = EffectList.GetRootItem ();
	if (root == NULL)
		return;

	do
	{
		if (EffectList.ItemHasChildren (root))
		{
			KillEffectListHelper (EffectList,root);
		}
		data = (CDataBase *) EffectList.GetItemData (root);
		effdata = dynamic_cast<CEffectData *> (data);
		contdata = dynamic_cast<CContentData *> (data);
		if (effdata)
			effdata->WriteData ();
		else if (contdata)
			contdata->WriteData ();
		EffectList.SetItemData (root,0);
		root = EffectList.GetNextSiblingItem (root);
	}
	while (root);
}

HTREEITEM InsertEffectFolder (CString& foldername,CTreeCtrl& EffectList)
{
	HTREEITEM root;

	foldername.MakeLower ();
	root = EffectList.GetRootItem ();
	if (root == NULL)
		return NULL;

	do
	{
		CString text;

		text = EffectList.GetItemText (root);
		text.MakeLower ();
		if (text == foldername)
		{
			return root;
		}
		root = EffectList.GetNextSiblingItem (root);
	}
	while (root);
	root = EffectList.InsertItem (foldername);
	return root;
}

HTREEITEM InsertEffectItem (CString& itemname,CTreeCtrl& EffectList,HTREEITEM root)
{
	HTREEITEM item;
	item = EffectList.GetChildItem (root);
	if (item != NULL)
	{
		do
		{
			if (EffectList.GetItemText (item) == itemname)
				return item;
			item = EffectList.GetNextSiblingItem (item);
		}
		while (item);
	}
	item = EffectList.InsertItem (itemname,root);
	return item;
}

bool CheckEffectItem (CString& itemname,CTreeCtrl& EffectList,HTREEITEM root)
{
	HTREEITEM item;
	itemname.MakeLower ();
	item = EffectList.GetChildItem (root);
	if (item != NULL)
	{
		do
		{
			CString text;
			text = EffectList.GetItemText (item);
			text.MakeLower ();
			if (text == itemname)
				return true;
			item = EffectList.GetNextSiblingItem (item);
		}
		while (item);
	}
	return false;
}

