// MissionPropsPanel.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "MissionPropsPanel.h"
#include "ObjectManager.h"
#include "GameInterface.h"
#include "GameTypesPanel.h"
#include <Adept\Adept.hpp>
using namespace Adept;


/////////////////////////////////////////////////////////////////////////////
// CMissionPropsPanel dialog


CMissionPropsPanel::CMissionPropsPanel(ObjectManager *gobj,UndoCommand **cmd,CWnd* pParent )
: CPanelDlg(CMissionPropsPanel::IDD, pParent)
{
	ObjMan=gobj;
	CmdList=cmd;
	//{{AFX_DATA_INIT(CMissionPropsPanel)
	m_Night = FALSE;
	m_Respawn = FALSE;
	m_RunningLight = FALSE;
	m_SearchLight = FALSE;
	m_HSEff = 0.0f;
	m_WatLevel = _T("");
	//}}AFX_DATA_INIT
}


void CMissionPropsPanel::DoDataExchange(CDataExchange* pDX)
{
	CPanelDlg::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMissionPropsPanel)
	DDX_Control(pDX, IDC_NIGHTWEATHER, m_NightWeather);
	DDX_Control(pDX, IDC_DAYWEATHER, m_DayWeather);
	DDX_Control(pDX, IDC_DAYSKYLIST, m_DaySkyList);
	DDX_Control(pDX, IDC_NIGHTSKYLIST, m_NightSkyList);
	DDX_Check(pDX, IDC_NIGHTMISSION, m_Night);
	DDX_Check(pDX, IDC_RESPAWN, m_Respawn);
	DDX_Check(pDX, IDC_RUNNINGLIGHTS, m_RunningLight);
	DDX_Check(pDX, IDC_SEARCHLIGHTS, m_SearchLight);
	DDX_Text(pDX, IDC_HSEFF, m_HSEff);
	DDX_Text(pDX, IDC_WATLEVEL, m_WatLevel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMissionPropsPanel, CPanelDlg)
//{{AFX_MSG_MAP(CMissionPropsPanel)
ON_BN_CLICKED(IDC_MPGAMES, OnMpgames)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMissionPropsPanel message handlers




void CMissionPropsPanel::OnOK() 
{
	UpdateData(TRUE);	
	
	ObjMan->MisDat.SetRespawn(m_Respawn==TRUE?true:false);
	ObjMan->MisDat.SetRunningLights(m_RunningLight==TRUE?true:false);
	ObjMan->MisDat.SetSearchLights(m_SearchLight==TRUE?true:false);
	
	
	ObjMan->MisDat.SetHeatSinkEfficiency(m_HSEff);
	
	int sel;
	CString str;
	sel=m_DaySkyList.GetCurSel();
	
	if(sel!=-1)
		SetDaySky(*((CString *)m_DaySkyList.GetItemDataPtr(sel)));
	
	sel=m_NightSkyList.GetCurSel();
	
	if(sel!=-1)
		SetNightSky(*((CString *)m_NightSkyList.GetItemDataPtr(sel)));
	
	sel=m_DayWeather.GetCurSel();
	if(sel!=-1)
	{
		ResourceID *rid=(ResourceID *)m_DayWeather.GetItemDataPtr(sel);
		if(rid)
			ObjMan->MisDat.SetWeather(*rid);
		else
			ObjMan->MisDat.SetWeather(ResourceID::Null);
		
	}
	
	sel=m_NightWeather.GetCurSel();
	if(sel!=-1)
	{
		ResourceID *rid=(ResourceID *)m_NightWeather.GetItemDataPtr(sel);
		if(rid)
			ObjMan->MisDat.SetNightWeather(*rid);
		else
			ObjMan->MisDat.SetNightWeather(ResourceID::Null);
	}
	
	
	int i;
	for(i=0;i<m_NightSkyList.GetCount();i++) delete m_NightSkyList.GetItemDataPtr(i);
	for(i=0;i<m_DaySkyList.GetCount();i++) delete m_DaySkyList.GetItemDataPtr(i);
	
	
	
	MakeMissionNight(m_Night==TRUE?true:false);
}

void CMissionPropsPanel::FillEffectList(CComboBox *box)
{
	box->ResetContent();
	
	box->AddString("<None>");
	if(Adept::ResourceManager::Instance==NULL) return;
	Adept::Resource curres;
	curres.First(NULL);
	
	
	CString res_name,base_name,dir_name;
	int item;
	int depth,sp,i;
	
	while(curres.ReadAndNext())
	{
		res_name=(char *)curres.GetName();
		res_name.MakeLower();
		
		if(res_name.Right(5).Compare(".data")==NULL)
		{									// This is a Data File
			base_name=res_name.Mid(res_name.ReverseFind('\\')+1);
			base_name=base_name.Left(base_name.GetLength()-5);
			
			sp=res_name.Find("effects\\");
			
			depth=0;
			if(sp>=0)
				for(i=sp;i<res_name.GetLength();i++) 
					if(res_name[i]=='\\') 
						depth++;	// Count Tree Depth	
					
					if( sp!=-1 && depth==2)
					{
						item=box->AddString(base_name);
						box->SetItemDataPtr(item,new ResourceID(curres.GetResourceID()));
					}
					
					
		}
	}
}

void CMissionPropsPanel::OnCancel() 
{
}


BOOL CMissionPropsPanel::OnInitDialog() 
{
	CPanelDlg::OnInitDialog();
	m_Night=IsMissionNight();
	
	m_Respawn=ObjMan->MisDat.GetRespawn()?TRUE:FALSE;
	m_RunningLight=ObjMan->MisDat.GetRunningLights()?TRUE:FALSE;
	m_SearchLight=ObjMan->MisDat.GetSearchLights()?TRUE:FALSE;
	
	FillEffectList(&m_DayWeather);
	FillEffectList(&m_NightWeather);
	
	int sel;
	
	
	
	m_HSEff=ObjMan->MisDat.GetHeatSinkEfficiency();
	
	LoadSkys(&m_DaySkyList);
	LoadSkys(&m_NightSkyList);
	
	m_DaySkyList.SetCurSel(m_DaySkyList.FindString(-1,DaySkyName()));
	m_NightSkyList.SetCurSel(m_NightSkyList.FindString(-1,NightSkyName()));
	m_WatLevel.Format("%5.3fm",GetWaterLevel());
	
	UpdateData(FALSE);
	
	
	ResourceID resid;
	
	resid=ObjMan->MisDat.GetWeather();
	sel=0;
	if(resid!=ResourceID::Null)
	{
		sel=-1;
		int i;
		for(i=0;i<m_DayWeather.GetCount() && sel<0;i++)
		{
			void *ptr=m_DayWeather.GetItemDataPtr(i);
			if(ptr)
			{
				if(*((ResourceID *)ptr)==resid) sel=i;
			}
			else
			{
				if(ResourceID::Null==resid) sel=i;
				
			}
		}
	}
	m_DayWeather.SetCurSel(sel);
	
	resid=ObjMan->MisDat.GetNightWeather();
	sel=0;
	if(resid!=ResourceID::Null)
	{
		sel=-1;
		int i;
		for(i=0;i<m_NightWeather.GetCount() && sel<0;i++)
		{
			void *ptr=m_NightWeather.GetItemDataPtr(i);
			
			if(ptr)
			{
				if(*((ResourceID *)ptr)==resid) sel=i;
			}
			else
			{
				if(ResourceID::Null==resid) sel=i;
				
			}
		}
	}
	m_NightWeather.SetCurSel(sel);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}



void CMissionPropsPanel::OnMpgames() 
{
	CGameTypesPanel dlg(ObjMan,CmdList,this);
	
	if(dlg.DoModal()==IDOK)
	{
		
	}

	CString msg;
	if(ObjMan->AddEssentialObjects(msg))
		{
		MessageBox(msg,"Mission Modified",MB_OK);
		}
	
	
}
