// ChangeResDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "ChangeResDlg.h"
#include "ObjCreateInfo.h"
#include <MW4\MW4.hpp>



/////////////////////////////////////////////////////////////////////////////
// CChangeResDlg dialog


CChangeResDlg::CChangeResDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChangeResDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChangeResDlg)
	//}}AFX_DATA_INIT
}


void CChangeResDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChangeResDlg)
	DDX_Control(pDX, IDC_RESOURCELIST, m_RList);
	DDX_Control(pDX, IDC_TYPETAB, m_TTab);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChangeResDlg, CDialog)
	//{{AFX_MSG_MAP(CChangeResDlg)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TYPETAB, OnSelchangeTypetab)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChangeResDlg message handlers

void CChangeResDlg::OnSelchangeTypetab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	GetGameData();

	*pResult = 0;
}

void CChangeResDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CChangeResDlg::OnOK() 
{
	ObjCreateInfo *inf=(ObjCreateInfo *)m_RList.GetItemData(m_RList.GetCurSel());
	m_CreateInfo=*inf;
	m_NewResource=inf->Path;
	CDialog::OnOK();
}

void CChangeResDlg::GetGameData()
{

	if(Adept::ResourceManager::Instance==NULL) return;
	Adept::Resource curres;
	curres.First(NULL);
	
	
	CString res_name,base_name,dir_name;
	int item,tabsel;
	tabsel=m_TTab.GetCurSel();
	int depth,sp,i;
	
	for(i=0;i<m_RList.GetCount();i++) 
	{
		ObjCreateInfo *inf=(ObjCreateInfo *)m_RList.GetItemData(i);
		if(inf) delete inf;
	}
	
	m_RList.ResetContent();
	
	while(curres.ReadAndNext())
	{
		res_name=(char *)curres.GetName();
		res_name.MakeLower();
		
		if(res_name.Right(9).Compare(".instance")==NULL)
		{									// This is a Data File
			base_name=res_name.Mid(res_name.ReverseFind('\\')+1);
			base_name=base_name.Left(base_name.GetLength()-9);
				
			if(base_name.CompareNoCase("Interface"))
			{
					if(tabsel==BuildList)
						sp=res_name.Find("buildings\\");
					else if(tabsel==VehList) 
						sp=res_name.Find("vehicles\\");
					else if(tabsel==MechList)  
						sp=res_name.Find("mechs\\"); 
					else if(tabsel==CultList)  
						sp=res_name.Find("culturals\\"); 
				
				depth=0;
				if(sp>=0)
					for(i=sp;i<res_name.GetLength();i++) 
						if(res_name[i]=='\\') 
							depth++;	// Count Tree Depth	
						
							if( sp!=-1 &&
								(res_name.Find("transpork")<0)
								&&(
								(tabsel==BuildList && depth==2) ||
								(tabsel==VehList   && depth==2) ||
								(tabsel==MechList  && depth==2) || 
								(tabsel==CultList  && depth==2) )
								)
						{
							item=m_RList.AddString(base_name);
							ObjCreateInfo *inf=new ObjCreateInfo;
							inf->CreateType=ObjCreateInfo::OT_GAMEOBJ;
							inf->Path=res_name;
							m_RList.SetItemData(item,(unsigned long)inf);
						}
					
			}	
		}
	}


	m_RList.SetCurSel(0);
}

BOOL CChangeResDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
		int idx=0;

	MechList=m_TTab.InsertItem(idx++,"Mechs");
	BuildList=m_TTab.InsertItem(idx++,"Buildings");
	VehList=m_TTab.InsertItem(idx++,"Vehicles");
	LightList=m_TTab.InsertItem(idx++,"Lights");
	CultList=m_TTab.InsertItem(idx++,"Culturals");

GetGameData();
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CChangeResDlg::OnClose() 
{
	for(int i=0;i<m_RList.GetCount();i++) 
	{
		ObjCreateInfo *inf=(ObjCreateInfo *)m_RList.GetItemData(i);
		if(inf) delete inf;
	}
	
	CDialog::OnClose();
}
