// AddScriptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "AddScriptDlg.h"
#include "GameInterface.h"
#include "EdScript.h"
#include "ScriptParamDlg.h"
#include <GameOS\ToolOS.hpp>


/////////////////////////////////////////////////////////////////////////////
// CAddScriptDlg dialog


CAddScriptDlg::CAddScriptDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAddScriptDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAddScriptDlg)
	m_DFName = _T("");
	//}}AFX_DATA_INIT
	m_DFName=MakeUniqueFileName("UserScript");
}


void CAddScriptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAddScriptDlg)
	DDX_Control(pDX, IDC_SCRIPTLIST, m_ScriptList);
	DDX_Text(pDX, IDC_DFNAME, m_DFName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAddScriptDlg, CDialog)
//{{AFX_MSG_MAP(CAddScriptDlg)
ON_CBN_SELCHANGE(IDC_SCRIPTLIST, OnSelchangeScriptlist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddScriptDlg message handlers

void CAddScriptDlg::OnOK() 
{
	UpdateData(TRUE);
	m_DFName=MakeUniqueFileName(m_DFName);
	UpdateData(FALSE);
	
	int sel=m_ScriptList.GetCurSel();
	if(sel<0) return;
	CString ddir,spath;
	ddir.Format("Content\\Missions\\%s\\Scripts",GetMissionName());
	spath=ddir+"\\"+m_DFName+".abl";
	// TODO: Add extra validation here
	if(sel==0)
	{
		
		CFileDialog dlg1(TRUE,"abl",NULL,OFN_NOCHANGEDIR,"Abl Scripts(*.abl)|*.abl||",this);
		CString dfilename;
		
		
		if(dlg1.DoModal()==IDOK)
		{
			CreateDirectory(ddir,NULL);
			if(gos_DoesFileExist(ddir)) 
			{
				CopyFile(dlg1.GetPathName(),spath,TRUE);
			}
			else
			{
				MessageBox("The Mission Must Be Saved before Attempting this Operation","Error",MB_OK);
			}
		}
		
		
		
	}
	else
	{
		CString sname;
		m_ScriptList.GetLBText(sel,sname);
		EdScript script;
		script.LoadTemplate(sname);
		CScriptParamDlg dlg(&script,this);
		if(dlg.DoModal()!=IDOK) return;
		CString fscp;
		fscp=script.GetScript();
		fscp.Replace("\n","\x0D\x0A");

		if(!gos_DoesFileExist(ddir))
		{
			MessageBox("Please save the mission before modifying scripts");
		}
		else
		{
			FILE *fl=fopen(spath,"wb");
			if(!fl) PAUSE(("Cannont open %s for write",spath));
			else
			{
				fwrite((LPCSTR)fscp,fscp.GetLength(),1,fl);
				fclose(fl);
			}
		}
	}
	
	UpdateData(TRUE);
	CDialog::OnOK();
	
}

void CAddScriptDlg::OnSelchangeScriptlist() 
{
	// TODO: Add your control notification handler code here
	CString sname;
	m_ScriptList.GetLBText(m_ScriptList.GetCurSel(),sname);
	m_DFName=MakeUniqueFileName(sname);
	UpdateData(FALSE);
	
}

BOOL CAddScriptDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_ScriptList.AddString("<From Disk>");
	
	WIN32_FIND_DATA file_data;
	HANDLE file_find;
	
	file_find=FindFirstFile("Content\\ABLScripts\\GenericScripts\\*.tpl",&file_data);
	
	if(file_find != INVALID_HANDLE_VALUE)
	{
		CString script_name;
		do
		{
			script_name=file_data.cFileName;
			script_name=script_name.Left(script_name.ReverseFind('.'));
			m_ScriptList.AddString(script_name);
		}
		while (FindNextFile(file_find,&file_data));
	}
	
	
	
	m_ScriptList.SetCurSel(0);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

CString CAddScriptDlg::MakeUniqueFileName(CString fname)
{
	int num=-1;
	CString ddir,basename=fname;
	ddir.Format("Content\\Missions\\%s\\Scripts\\%s.abl",GetMissionName(),basename);
	while(gos_DoesFileExist(ddir))
	{
		num++;
		basename.Format("%s%04i",fname,num);
		ddir.Format("Content\\Missions\\%s\\Scripts\\%s.abl",GetMissionName(),basename);
	}
	return basename;
}
