// HintFileView.cpp : implementation file
//

#include "stdafx.h"
#include "Megatron.h"
#include "HintFileView.h"
#include "MegatronDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHintFileView

IMPLEMENT_DYNCREATE(CHintFileView, CView)

CHintFileView::CHintFileView()
{
}

CHintFileView::~CHintFileView()
{
}


BEGIN_MESSAGE_MAP(CHintFileView, CView)
	//{{AFX_MSG_MAP(CHintFileView)
	ON_WM_RBUTTONUP()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHintFileView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CHintFileView diagnostics

#ifdef _DEBUG
void CHintFileView::AssertValid() const
{
	CView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CHintFileView message handlers

void CHintFileView::OnInitialUpdate() 
{
	CRect rc;
	GetClientRect(&rc);
	m_wndListFiles.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | LBS_NOTIFY | LBS_EXTENDEDSEL | LBS_NOINTEGRALHEIGHT,rc,this,NULL);
	m_wndListFiles.AddString("No Hint Files");
	m_wndListFiles.ShowWindow(SW_SHOW);
	CView::OnInitialUpdate();
}

void CHintFileView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{	
	switch (lHint)
	{
	case UPDATE_BUILDFILES:
		{
			if (pHint)
			{
				CDocument *pDoc = GetDocument();

				int count = 0;
				m_wndListFiles.ResetContent();
				CPtrList *pList = (CPtrList *)pHint;
				POSITION pos = pList->GetHeadPosition();
				while (pos)
				{
					Stuff::NotationFile *pFile = (Stuff::NotationFile *)pList->GetNext(pos);
					Check_Object(pFile);
					
					Stuff::NotationFile::PageIterator *pages = pFile->MakePageIterator();
					Stuff::Page *page;
					while (page = pages->ReadAndNext())
					{
						CString pagename = page->GetName();
						
						const char *entry;
						if (page->GetEntry("texturepool",&entry))
						{
							count++;
							int index = m_wndListFiles.AddString(entry);
							CString filename("content\\");
							filename+=entry;

							CMegatronDoc *pMegaDoc = (CMegatronDoc *)pDoc;
							Stuff::NotationFile *pHintFile = pMegaDoc->GetHintFile(filename);
							m_wndListFiles.SetItemData(index,(DWORD)pHintFile);
						}
					}
					delete pages;
				}
				if (0==count)
					m_wndListFiles.AddString("No Hint Files");
			}
			break;
		}
	case UPDATE_ADD_TEXTUREHINT:
		{
			if (pHint)
			{
				CPtrList *pList = (CPtrList *)pHint;
				POSITION pos = pList->GetHeadPosition();
				while (pos)
				{
					Stuff::Page *page= (Stuff::Page *)pList->GetNext(pos);
					Check_Object(page);


					// Copy this page into each of the selected files in this view
					int nCount = m_wndListFiles.GetSelCount();
					int *items = new int[nCount];
					int err = m_wndListFiles.GetSelItems(nCount,items);

					for (int x=0;x<nCount;x++)
					{
						DWORD data = m_wndListFiles.GetItemData(items[x]);
						Stuff::NotationFile *pFile = (Stuff::NotationFile *)data;
						Check_Object(pFile);

						Stuff::Page *newpage = pFile->AddPage(page->GetName());
						Stuff::Note *note;
						Stuff::Page::NoteIterator *notes = page->MakeNoteIterator();
						while (note = notes->ReadAndNext())
						{
							const char *entry;
							note->GetEntry(&entry);
							newpage->AppendEntry(note->GetName(),entry);
						}
						delete notes;
					}
					delete items;		
				}
				OnSelChange();
			}
			break;
		}
	default:
		{
		}
	}
}

BOOL CHintFileView::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	switch (HIWORD(wParam))
	{
	case LBN_SELCHANGE:
		{
			OnSelChange();
			break;
		}
	}
	return CView::OnCommand(wParam, lParam);
}

void CHintFileView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	CMenu* pMenu = new CMenu();
	pMenu->LoadMenu(IDR_POPUP_MENUS);
	CMenu *pSubMenu = pMenu->GetSubMenu(1);

	ClientToScreen(&point);

	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,point.x, point.y, this, NULL);
	delete pMenu;	
	CView::OnRButtonUp(nFlags, point);
}

void CHintFileView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	if (m_wndListFiles.GetSafeHwnd())
	{
		m_wndListFiles.MoveWindow(0,0,cx,cy);
	}
}

void CHintFileView::OnSelChange()
{
	int nCount = m_wndListFiles.GetSelCount();
	int *items = new int[nCount];
	int err = m_wndListFiles.GetSelItems(nCount,items);
	CPtrList list;
	for (int x=0;x<nCount;x++)
	{
		DWORD data = m_wndListFiles.GetItemData(items[x]);
		list.AddHead((LPVOID)data);
	}
	delete items;

	CDocument *pDoc = GetDocument();
	pDoc->UpdateAllViews(this,UPDATE_HINTFILES,&list);
}
