// FindView.cpp : implementation file
//

#include "stdafx.h"
#include "contenttrack.h"
#include "mainfrm.h"

#include "FindView.h"
#include "finddlg.h"
#include "NodeDataValueFileDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFindView

IMPLEMENT_DYNCREATE(CFindView, CListView)

CFindView::CFindView()
{
}

CFindView::~CFindView()
{
}


BEGIN_MESSAGE_MAP(CFindView, CListView)
	//{{AFX_MSG_MAP(CFindView)
	ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFindView drawing

void CFindView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CFindView diagnostics

#ifdef _DEBUG
void CFindView::AssertValid() const
{
	CListView::AssertValid();
}

void CFindView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFindView message handlers

void CFindView::OnInitialUpdate() 
{
	CFindDlg dlg;
	if (IDOK == dlg.DoModal())
	{
		m_listKeyType.AddHead(&dlg.m_listKeyType);
	}	

	CListCtrl *pCtrl = &GetListCtrl();
	
	pCtrl->InsertColumn(0,"Find Items...",LVCFMT_LEFT,300);

	CListView::OnInitialUpdate();
}

BOOL CFindView::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.style |= LVS_REPORT | LVS_SHOWSELALWAYS | LVS_SINGLESEL;	
	return CListView::PreCreateWindow(cs);
}

void CFindView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (pHint)
		// For now... something was wrong, and this is updating itself
		return;
	CListCtrl *pCtrl = &GetListCtrl();
	pCtrl->DeleteAllItems();
	if (GetDocument()->IsKindOf(RUNTIME_CLASS(CNodeDataValueFileDoc)))
	{
		CNodeDataValueFileDoc *pDoc = (CNodeDataValueFileDoc *)GetDocument();
		if (pDoc->m_pActiveChild)
		{
			CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd();
			pFrame->SetMessageText("Searching...");
			CNodeObject *pNode = pDoc->m_pActiveChild;
			FindNodes(pNode);
		}
	}	
}

int CFindView::FindNodes(CNodeObject *pNode)
{
	int count = 0;
	if (pNode->IsDataValue())
	{
		// Check if it matches the search
		CNodeDataValue *pData = (CNodeDataValue *)pNode;
		int value = pData->GetKeyType();
		bool bMatchClass = this->m_listKeyType.Find((void *)value,NULL);
		if (bMatchClass)
		{
			CListCtrl *pCtrl = &GetListCtrl();
			CString str;
			CNodeObject *pChild = pNode;

			pChild->GetName(str);
			//str = pChild->GetName();
/*			while (pChild)
			{
				str = pChild->GetName()+":"+str;
				pChild = pChild->GetParentNode();
			}*/
			int index = pCtrl->InsertItem(pCtrl->GetItemCount(),str);
			pCtrl->SetItemData(index,(DWORD)pNode);
		}
	}
	POSITION pos = pNode->GetFirstChild();
	while (pos)
	{
		CNodeObject *pChild = pNode->GetNextChild(pos);
		count += FindNodes(pChild);
	}
	return 0;
}

void CFindView::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	
	if (pNMListView->uNewState & LVIS_SELECTED)
	{
		CListCtrl *pCtrl = &GetListCtrl();
		CNodeObject *pNode = (CNodeObject *)pCtrl->GetItemData(pNMListView->iItem);
		if (pNode)
			GetDocument()->UpdateAllViews(this,(DWORD)pNode,NULL);
	}
	*pResult = 0;
}
