#include "StdAfx.h"
#include "FuncAvailCtrl.h"
#include "FuncList.h"
#include "resource.h"

#define COL_Function		0
#define MIN_WIDTH_Function	213
#define STATE_NotSelected	0
#define STATE_Selected		1

#define SZ_SELECTALL	L"Click here to toggle all"

CFuncAvailCtrl::CFuncAvailCtrl()
{
	m_pctrCmdsAvail = NULL;
}

CFuncAvailCtrl::~CFuncAvailCtrl(void)
{
}

BOOL CFuncAvailCtrl::Initialize(CTreeCtrl* pGuiCtrl, CFuncList* pFuncList)
{
	TBD_FUNCTION(CFuncAvailCtrl::Initialize);

	if (!pGuiCtrl || !pFuncList)
	{
		TBD_OUTPUT(TBDCT_INFO, TBDOL_ERROR, L"Invalid param");
		return FALSE;
	}

	// cache pointer to GUI control
	m_pctrCmdsAvail = pGuiCtrl;

	return LoadFunctions(pFuncList);
}

BOOL CFuncAvailCtrl::LoadFunctions(CFuncList* pFuncList)
{
	TBD_FUNCTION(CFuncAvailCtrl::LoadFunctions);

	HTREEITEM htreeParent = TVI_ROOT;
	CString szCachedType = L"";
	CString szType = L"";
	CString szFunctionName = L"";

	for (int iIndex=0; iIndex<pFuncList->GetCount(); iIndex++)
	{
		szFunctionName = pFuncList->GetFunctionName(iIndex, &szType);

		if (szFunctionName.GetLength())
		{
			if (0 != szType.Compare(szCachedType))
			{
				// type service type, create new parent node
				htreeParent = m_pctrCmdsAvail->InsertItem(szType);
				szCachedType = szType;
			}

			// insert child item
			m_pctrCmdsAvail->InsertItem(szFunctionName, htreeParent);
		}
	}

	return TRUE;
}

BOOL CFuncAvailCtrl::IsAnyFuncSelected()
{
	HTREEITEM pParent = m_pctrCmdsAvail->GetRootItem();
	HTREEITEM pChild = m_pctrCmdsAvail->GetChildItem(pParent);

	while (pParent)
	{
		while(pChild)
		{
			if (m_pctrCmdsAvail->GetCheck(pChild))
				return TRUE;

			pChild = m_pctrCmdsAvail->GetNextSiblingItem(pChild);
		}

		pParent = m_pctrCmdsAvail->GetNextSiblingItem(pParent);
		pChild = m_pctrCmdsAvail->GetChildItem(pParent);
	}
	
	return FALSE;
}

BOOL CFuncAvailCtrl::GetSelectedFunctions(CFuncList* pFuncList)
{
	TBD_FUNCTION(CFuncAvailCtrl::GetSelectedFunctions);

	// clear list
	pFuncList->RemoveAll();
	HTREEITEM pParentItem = m_pctrCmdsAvail->GetRootItem();
	HTREEITEM pChildItem = m_pctrCmdsAvail->GetChildItem(pParentItem);

	// cycle through list
	while (pParentItem)
	{
		while (pChildItem)
		{
			// only add items where checkbox is selected
			if (m_pctrCmdsAvail->GetCheck(pChildItem))
			{
				CString szFunc(m_pctrCmdsAvail->GetItemText(pChildItem));
				pFuncList->Add(L"", szFunc);
				TBD_OUTPUT(TBDCT_INFO, TBDOL_INFORMATION, szFunc);
			}

			pChildItem = m_pctrCmdsAvail->GetNextItem(pChildItem, TVGN_NEXT);
		}
		

		pParentItem = m_pctrCmdsAvail->GetNextItem(pParentItem, TVGN_NEXT);
		pChildItem = m_pctrCmdsAvail->GetChildItem(pParentItem);
	}

	return TRUE;
}

BOOL CFuncAvailCtrl::SelectAllChildren(HTREEITEM hItem, BOOL bAll /* = TRUE */)
{
	HTREEITEM pParentItem = hItem;
	HTREEITEM pChildItem = m_pctrCmdsAvail->GetChildItem(pParentItem);

	while (pChildItem)
	{
		// only add items where checkbox is selected
		m_pctrCmdsAvail->SetCheck(pChildItem, bAll);
		pChildItem = m_pctrCmdsAvail->GetNextItem(pChildItem, TVGN_NEXT);
	}

	return TRUE;
}