// ResourceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MW4GameEd2.h"
#include "ResourceDlg.h"
#include "ObjectManager.h"
#include "ObjCreateInfo.h"
#include <Adept\Resource.hpp>


/////////////////////////////////////////////////////////////////////////////
// CResourceDlg dialog


CResourceDlg::CResourceDlg(ObjectManager *objman,CWnd* pParent /*=NULL*/)
: CDialog(CResourceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CResourceDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	Initalized=false;	
	
	ObjMan=objman;
	Create(CResourceDlg::IDD,pParent);
	//	ShowWindow(SW_SHOW);
	
}


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


BEGIN_MESSAGE_MAP(CResourceDlg, CDialog)
//{{AFX_MSG_MAP(CResourceDlg)
ON_WM_CLOSE()
ON_LBN_SELCHANGE(IDC_RESOURCELIST, OnSelchangeResourcelist)
ON_NOTIFY(TCN_SELCHANGE, IDC_TYPETAB, OnSelchangeTypetab)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResourceDlg message handlers

void CResourceDlg::OnCancel() 
{
	
	//	CDialog::OnCancel();
}

void CResourceDlg::OnOK() 
{
	
	//	CDialog::OnOK();
}

void CResourceDlg::OnClose() 
{
	ShowWindow(SW_HIDE);
	
	//	CDialog::OnClose();
}

BOOL CResourceDlg::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");
	MiscList=m_TTab.InsertItem(idx++,"Misc");
	
	Initalized=true;	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CResourceDlg::OnSelchangeResourcelist() 
{
	ObjMan->SetAddInfo((ObjCreateInfo *)m_RList.GetItemData(m_RList.GetCurSel()));
}

void CResourceDlg::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();
	
	if(tabsel==LightList)
	{
		ObjCreateInfo *inf;
		item=m_RList.AddString("Ambient");
		inf=new ObjCreateInfo;
		inf->CreateType=ObjCreateInfo::OT_LIGHT;
		inf->SubType=ObjCreateInfo::LST_AMBIENT;
		m_RList.SetItemData(item,(unsigned long)inf);
		
		item=m_RList.AddString("Infinite");
		inf=new ObjCreateInfo;
		inf->CreateType=ObjCreateInfo::OT_LIGHT;
		inf->SubType=ObjCreateInfo::LST_INFINITE;
		m_RList.SetItemData(item,(unsigned long)inf);
		
/*		
		item=m_RList.AddString("Point");
		inf=new ObjCreateInfo;
		inf->CreateType=ObjCreateInfo::OT_LIGHT;
		inf->SubType=ObjCreateInfo::LST_POINT;
		m_RList.SetItemData(item,(unsigned long)inf);

		item=m_RList.AddString("Spot");
		inf=new ObjCreateInfo;
		inf->CreateType=ObjCreateInfo::OT_LIGHT;
		inf->SubType=ObjCreateInfo::LST_SPOT;
		m_RList.SetItemData(item,(unsigned long)inf);
*/		
	}
	else
	{
		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);
				CString fchar;
				fchar=base_name.Left(1);
				base_name.Delete(0);
				fchar.MakeUpper();
				base_name.MakeLower();
				base_name=fchar+base_name;

				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\\"); 
					else if(tabsel==MiscList)  
						sp=res_name.Find("misc\\"); 
					else sp=-1;
					
					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);
							}
							
							if( sp!=-1 && tabsel==MiscList  && depth==2 && 
								base_name.CompareNoCase("path") && 
								base_name.CompareNoCase("objective") 
								) 
							{ 
								item=m_RList.AddString(base_name);
								ObjCreateInfo *inf=new ObjCreateInfo;
								inf->Path=res_name;
								
								if(!base_name.CompareNoCase("dropzone"))
								{
									inf->CreateType=ObjCreateInfo::OT_DROPZONE;
								}
								else if(!base_name.CompareNoCase("cameraship"))
								{					
									inf->CreateType=ObjCreateInfo::OT_CAMERA;
								}
								else if(!base_name.CompareNoCase("navpoint"))
								{					
									inf->CreateType=ObjCreateInfo::OT_NVGAMEOBJ;
								}
								else if(!base_name.CompareNoCase("objective"))
								{					
								}
								else if(!base_name.CompareNoCase("fxgenerator"))
								{					
									inf->CreateType=ObjCreateInfo::OT_FX;
								}
								
								m_RList.SetItemData(item,(unsigned long)inf);
							}
							
							
				}	
			}
		}
	}
	if(tabsel==MiscList)  
	{
		ObjCreateInfo *inf;
		item=m_RList.AddString("Node");
		inf=new ObjCreateInfo;
		inf->CreateType=ObjCreateInfo::OT_NODE;
		m_RList.SetItemData(item,(unsigned long)inf);
		
		item=m_RList.AddString("Clipboard Contents");
		inf=new ObjCreateInfo;
		inf->CreateType=ObjCreateInfo::OT_CLIPBOARD;
		m_RList.SetItemData(item,(unsigned long)inf);
		
	}
	
	m_RList.SetCurSel(0);
	ObjMan->SetAddInfo((ObjCreateInfo *)m_RList.GetItemData(m_RList.GetCurSel()));
	
}

void CResourceDlg::LoadFromReg()
{
	CRect rct;
	CString wintitle;
	GetWindowText(wintitle);
	unsigned long size;
	
	size=sizeof(rct);
	gos_LoadDataFromRegistry((char *)(LPCSTR)(wintitle+"Rect"),&rct,&size);
	if(size) SetWindowPos(NULL,rct.left,rct.top,rct.Width(),rct.Height(),SWP_NOZORDER);
	
	int tdat;
	size=sizeof(tdat);
	gos_LoadDataFromRegistry((char *)(LPCSTR)(wintitle+"Visible"),&tdat,&size);
	
	if(size && tdat==0) 
		ShowWindow(SW_HIDE);
	else
		ShowWindow(SW_SHOW);
	
}

void CResourceDlg::SaveToReg()
{
	CRect rct;
	GetWindowRect(&rct);
	CString wintitle;
	int tdat;
	
	GetWindowText(wintitle);
	gos_SaveDataToRegistry((char *)(LPCSTR)(wintitle+"Rect"),&rct,sizeof(rct));
	tdat=IsWindowVisible();	gos_SaveDataToRegistry((char *)(LPCSTR)(wintitle+"Visible"),&tdat,sizeof(tdat));
	
}


void CResourceDlg::OnSelchangeTypetab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	GetGameData();
	*pResult = 0;
}

void CResourceDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	
	
	if(Initalized)
	{
		CRect rct;
		GetClientRect(&rct);
		m_TTab.SetWindowPos(NULL,10,10,rct.Width()-20,rct.Height()-20,SWP_NOZORDER|SWP_NOMOVE);
		m_RList.SetWindowPos(NULL,10+10,10+20,rct.Width()-40,rct.Height()-60,SWP_NOZORDER|SWP_NOMOVE);
	}
}
