// TextureHintView.cpp : implementation file
//

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

#include "DlgCreateMegatexture.h"
#include "DlgAddTextures.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTextureHintView

IMPLEMENT_DYNCREATE(CTextureHintView, CView)

CTextureHintView::CTextureHintView()
{
}

CTextureHintView::~CTextureHintView()
{
}


BEGIN_MESSAGE_MAP(CTextureHintView, CView)
	//{{AFX_MSG_MAP(CTextureHintView)
	ON_WM_RBUTTONUP()
	ON_WM_SIZE()
	ON_COMMAND(ID_NEW_MEGATEXTURE, OnNewMegatexture)
	ON_COMMAND(ID_NEW_TEXTURE, OnNewTexture)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTextureHintView drawing

void CTextureHintView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
}

/////////////////////////////////////////////////////////////////////////////
// CTextureHintView diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CTextureHintView message handlers

void CTextureHintView::OnInitialUpdate() 
{
	CRect rc;
	GetClientRect(&rc);
	m_wndListHints.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | LBS_NOTIFY | LBS_EXTENDEDSEL | LBS_NOINTEGRALHEIGHT,rc,this,NULL);
	m_wndListHints.ShowWindow(SW_SHOW);
	CView::OnInitialUpdate();	
}

void CTextureHintView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	switch (lHint)
	{
	case UPDATE_BUILDFILES:
		{
			m_wndListHints.ResetContent();
			break;
		}
	case UPDATE_HINTFILES:
		{
			if (pHint)
			{
				int count = 0;
				m_wndListHints.ResetContent();
				CPtrList *pList = (CPtrList *)pHint;
				POSITION pos = pList->GetHeadPosition();
				while (pos)
				{
					Stuff::NotationFile *pFile = (Stuff::NotationFile *)pList->GetNext(pos);
					if (pFile)
					{
						Check_Object(pFile);
						
						Stuff::NotationFile::PageIterator *pages = pFile->MakePageIterator();
						Stuff::Page *page;
						while (page = pages->ReadAndNext())
						{
							CString pagename = page->GetName();
							
							int index = m_wndListHints.AddString(pagename);
							m_wndListHints.SetItemData(index,(DWORD)page);
						}
						delete pages;
					}
				}
			}
			break;
		}
	default:
		{
		}
	}
}

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

	ClientToScreen(&point);

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

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

BOOL CTextureHintView::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	switch (HIWORD(wParam))
	{
	case LBN_SELCHANGE:
		{
			int nCount = m_wndListHints.GetSelCount();
			int *items = new int[nCount];
			int err = m_wndListHints.GetSelItems(nCount,items);
			CPtrList list;
			for (int x=0;x<nCount;x++)
			{
				DWORD data = m_wndListHints.GetItemData(items[x]);
				list.AddTail((LPVOID)data);
			}
			delete items;

			CDocument *pDoc = GetDocument();
			pDoc->UpdateAllViews(this,UPDATE_TEXTUREHINT,&list);
			break;
		}
	}
	return CView::OnCommand(wParam, lParam);
}

void CTextureHintView::OnNewMegatexture() 
{
	// Create a new Mega Texture here

	CPtrList list;
	CPtrList poslist;
	for (int x=0;x<m_wndListHints.GetCount();x++)
	{
		DWORD data = m_wndListHints.GetItemData(x);
		if (data)
		{
			POSITION pos = list.AddTail((LPVOID)data);
			if (m_wndListHints.GetSel(x))
			{
				poslist.AddTail((LPVOID)pos);
			}
		}
	}
	list.AddHead((LPVOID)&poslist);
	CDlgCreateMegatexture dlg;
	dlg.m_pList = &list;

	Stuff::NotationFile notefile;
	dlg.m_pNoteFile = &notefile;

	if (IDOK == dlg.DoModal())
	{
		CPtrList list;
		Stuff::NotationFile::PageIterator *pages = notefile.MakePageIterator();
		Stuff::Page *page;
		while (page = pages->ReadAndNext())
		{
			list.AddTail((LPVOID)page);
		}
		delete pages;
		CDocument *pDoc = GetDocument();
		pDoc->UpdateAllViews(this,UPDATE_ADD_TEXTUREHINT,&list);
	}
	notefile.IgnoreChanges();
}

void CTextureHintView::OnNewTexture() 
{
	CDlgAddTextures	dlg;
	CStringList list;
	int count = m_wndListHints.GetCount();
	for (int x=0;x<count;x++)
	{
		CString str;
		m_wndListHints.GetText(x,str);
		DWORD dw = m_wndListHints.GetItemData(x);
		Stuff::Page *page = (Stuff::Page *)dw;
		Check_Object(page);

		str.MakeLower();
		list.AddTail(str);

		const char *entry;
		if (page->GetEntry("alias",&entry))
		{
			str = entry;
			str.MakeLower();
			list.AddTail(str);
		}
	}

	dlg.m_pList = &list;
	if (IDOK == dlg.DoModal())
	{
		Stuff::NotationFile notefile;
		POSITION pos = list.GetHeadPosition();
		CPtrList ptrlist;
		while (pos)
		{
			CString str = list.GetNext(pos);
			Stuff::Page *page = notefile.AddPage(str);
			ptrlist.AddTail((LPVOID)page);
		}
		CDocument *pDoc = GetDocument();
		pDoc->UpdateAllViews(this,UPDATE_ADD_TEXTUREHINT,&ptrlist);

		notefile.IgnoreChanges();
	}
}
