// WizPageProject.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CWizPageProject property page

IMPLEMENT_DYNCREATE(CWizPageProject, CPropertyPage)

CWizPageProject::CWizPageProject() : CPropertyPage(CWizPageProject::IDD)
{
	//{{AFX_DATA_INIT(CWizPageProject)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CWizPageProject::~CWizPageProject()
{
}

void CWizPageProject::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWizPageProject)
	DDX_Control(pDX, IDC_LIST_COPYDIR, m_ctrlListCopy);
	DDX_Control(pDX, IDC_LISTPROJECT, m_ctrlList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWizPageProject, CPropertyPage)
	//{{AFX_MSG_MAP(CWizPageProject)
	ON_LBN_SELCHANGE(IDC_LISTPROJECT, OnSelchangeListproject)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWizPageProject message handlers

BOOL CWizPageProject::OnSetActive() 
{
	CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
	pParent->SetWizardButtons(PSWIZB_BACK | PSWIZB_DISABLEDFINISH);

	// Remove all items
	while (m_ctrlList.GetCount())
		m_ctrlList.DeleteString(0);

	// Add all open projects except for this one
	CFrameWnd *pFrame = GetParentFrame();
	CMainFrame *pMainFrame = (CMainFrame *)pFrame;
	CDocument *pDoc = pMainFrame->MDIGetActive()->GetActiveDocument();

	CWinApp *pApp = AfxGetApp();
	POSITION pos = pApp->GetFirstDocTemplatePosition();
	while (pos)
	{
		CDocTemplate *pTemplate = pApp->GetNextDocTemplate(pos);
		POSITION posT = pTemplate->GetFirstDocPosition();
		while (posT)
		{
			CDocument *pDocT = pTemplate->GetNextDoc(posT);
			if (pParent->m_bRename || (pDocT != pDoc))
			{
				int index = m_ctrlList.AddString(pDocT->GetTitle());
				m_ctrlList.SetItemData(index,(DWORD)pDocT);
			}
		}	
	}

	while (this->m_ctrlListCopy.GetCount())
		m_ctrlListCopy.DeleteString(0);

	CNodeDisk *pDisk = pParent->m_pSrcDoc->GetRootNode();
	pos = pDisk->GetFirstChild();
	while (pos)
	{
		CNodeObject *pChild = pDisk->GetNextChild(pos);
		if (pChild->GetClassID() == CLASS_CNODEDISKDIRECTORY)
		{
			CString strName;
			pChild->GetName(strName);
			int index = m_ctrlListCopy.AddString(strName);
			m_ctrlListCopy.SetItemData(index,(DWORD)pChild);
			// By default, select these two directories
			if ((0 == strnicmp(strName,"maps",4)) || (0 == strnicmp(strName,"missions",8)))
			{
				m_ctrlListCopy.SetSel(index);
			}
		}
	}

	return CPropertyPage::OnSetActive();
}

LRESULT CWizPageProject::OnWizardNext() 
{
	int index = m_ctrlList.GetCurSel();
	CNodeDataValueFileDoc *pDoc = (CNodeDataValueFileDoc *)m_ctrlList.GetItemData(index);
	CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
	pParent->m_pDstDoc = pDoc;

	pParent->m_listCopyDir.RemoveAll();
	int count = m_ctrlListCopy.GetSelCount();
	int *items = new int(count);
	m_ctrlListCopy.GetSelItems(count,items);
	for (int x=0;x<count;x++)
	{
		CNodeObject *pObj = (CNodeObject *)m_ctrlListCopy.GetItemData(items[x]);
		pParent->m_listCopyDir.AddHead(pObj);
	}
	delete items;
	return CPropertyPage::OnWizardNext();
}

BOOL CWizPageProject::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CWizPageProject::OnSelchangeListproject() 
{
	if (LB_ERR != m_ctrlList.GetCurSel())
	{
		CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
		pParent->SetWizardButtons(PSWIZB_BACK | PSWIZB_FINISH);
	}
}

BOOL CWizPageProject::OnWizardFinish() 
{
	int index = m_ctrlList.GetCurSel();
	CNodeDataValueFileDoc *pDoc = (CNodeDataValueFileDoc *)m_ctrlList.GetItemData(index);
	CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
	pParent->m_pDstDoc = pDoc;

	pParent->m_listCopyDir.RemoveAll();
	int count = m_ctrlListCopy.GetSelCount();
	// BUGBUG**
	// Dont think we'll have more than 100 items, but the delete operator crashes here
	int items[100];
	m_ctrlListCopy.GetSelItems(100,items);
	for (int x=0;x<count;x++)
	{
		CNodeObject *pObj = (CNodeObject *)m_ctrlListCopy.GetItemData(items[x]);
		pParent->m_listCopyDir.AddHead(pObj);
	}

	return CPropertyPage::OnWizardFinish();
}
