// DlgSelectMegatextures.cpp : implementation file
//

#include "stdafx.h"
#include "animationsuite.h"
#include "DlgSelectMegatextures.h"
#include "Proxies\ProxyHeaders.hpp"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgSelectMegatextures dialog


CDlgSelectMegatextures::CDlgSelectMegatextures(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSelectMegatextures::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSelectMegatextures)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgSelectMegatextures::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSelectMegatextures)
	DDX_Control(pDX, IDC_LIST_PAGES, m_listPages);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSelectMegatextures, CDialog)
	//{{AFX_MSG_MAP(CDlgSelectMegatextures)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSelectMegatextures message handlers

BOOL CDlgSelectMegatextures::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CStringList namelist;
	MStringChainIterator megatextures(Proxies::CoalesceTexturesProcess::MakeMegatextureChain(m_strMegaTextures));
	PlugOf<MString> *string;
	const char* mega_name = NULL;
	while ((string = megatextures.ReadAndNext()) != NULL)
	{
		mega_name = string->GetItem();
		Check_Pointer(mega_name);
		namelist.AddTail(mega_name);
	}
	
	if (m_strHintFile.GetLength())
	{
		bool bDone = false;
		CString filename = m_strHintFile;

		while (!bDone)
		{
			Stuff::NotationFile *pFile = new Stuff::NotationFile(filename);
			Stuff::NotationFile::PageIterator *pages = pFile->MakePageIterator();
			Stuff::Page *page;
			while (page = pages->ReadAndNext())
			{
				CString pagename = page->GetName();
				const char *entry;
				if (page->GetEntry("PageSize",&entry))
				{
					int index = m_listPages.AddString(pagename);
					if (namelist.Find(pagename))
					{
						m_listPages.SetSel(index);
					}
				}
			}
			delete pages;
			bDone = true;
			page = pFile->FindPage("");
			if (page)
			{
				const char *entry;
				if (page->GetEntry("Parent",&entry))
				{
					filename = entry;
				}
				else
				{
					bDone = false;
				}
			}
			delete pFile;
		}
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSelectMegatextures::OnOK() 
{
	int count;
	count = m_listPages.GetSelCount();
	int *items = new int[count];
	m_listPages.GetSelItems(count,items);
	m_strMegaTextures = "";
	for (int x=0;x<count;x++)
	{
		CString str;
		m_listPages.GetText(items[x],str);
		if (x>0)
		{
			m_strMegaTextures += ",";
		}
		m_strMegaTextures += str;
	}
	m_strMegaTextures.TrimRight();
	CDialog::OnOK();
}
