// AudioFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "AudioFileDlg.h"
#include "GameInterface.h"
#include <Stuff\Stuff.hpp>

using namespace Stuff;

/////////////////////////////////////////////////////////////////////////////
// CAudioFileDlg dialog


CAudioFileDlg::CAudioFileDlg(CString mis_name,CWnd* pParent /*=NULL*/)
	: CDialog(CAudioFileDlg::IDD, pParent)
{
MisName=mis_name;
	//{{AFX_DATA_INIT(CAudioFileDlg)
	m_AudName = _T("");
	//}}AFX_DATA_INIT
}


void CAudioFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAudioFileDlg)
	DDX_Control(pDX, IDC_NEWFILELIST, m_NewFileList);
	DDX_Control(pDX, IDC_AUDIOLIST, m_AudList);
	DDX_Text(pDX, IDC_AUDNAME, m_AudName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAudioFileDlg, CDialog)
	//{{AFX_MSG_MAP(CAudioFileDlg)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_LBN_SELCHANGE(IDC_AUDIOLIST, OnSelchangeAudiolist)
	ON_EN_CHANGE(IDC_AUDNAME, OnChangeAudname)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAudioFileDlg message handlers

void CAudioFileDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	
	UpdateData(TRUE);
	if(m_NewFileList.GetCurSel()>=0)
	{
	CString str;
	m_NewFileList.GetText(m_NewFileList.GetCurSel(),str);
	int itm;
	itm=m_AudList.AddString(str);
	m_AudList.SetItemDataPtr(itm,new CString(str));
	m_NewFileList.DeleteString(m_NewFileList.GetCurSel());
	}
}

void CAudioFileDlg::OnDelete() 
{
	UpdateData(TRUE);
	if(m_AudList.GetCurSel()>=0)
	{
	CString str;
	delete m_AudList.GetItemDataPtr(m_AudList.GetCurSel());
	m_AudList.GetText(m_AudList.GetCurSel(),str);
	m_NewFileList.AddString(str);
	m_AudList.DeleteString(m_AudList.GetCurSel());
	}
}

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

void CAudioFileDlg::OnOK() 
{
	UpdateData(TRUE);

	CString aud_path;
	aud_path.Format("Content\\Missions\\%s\\%s.audio",MisName,MisName);
	NotationFile aud_file(aud_path);
	MakeFileWriteable(aud_path);

	aud_file.DeleteAllPages();

	CString table_path;
	table_path.Format("Content\\Missions\\%s\\%s.table",MisName,MisName);
	NotationFile table_file(table_path);
	MakeFileWriteable(table_path);

	Page *tablepage=table_file.SetPage("4");
	tablepage->DeleteAllNotes();

	int i;
	CString str,numstr;
	Page *page;
	for(i=0;i<m_AudList.GetCount();i++)
	{
	m_AudList.GetText(i,str);
	str="Content\\Audio\\"+str+".wav";
	numstr.Format("%i",i);
	page=aud_file.SetPage(numstr);
	page->SetEntry("Type","VoiceOverComponent");
	page->SetEntry("Sample",str);
	page->SetEntry("RendererShouldExecute",1);
	
	CString *strptr;
	strptr=(CString *)m_AudList.GetItemDataPtr(i);
	tablepage->SetEntry(*strptr,69);
	delete strptr;
	}

	
	

	for(i=0;i<m_AudList.GetCount();i++)
	{
	m_AudList.GetText(i,str);
	str="Content\\Audio\\"+str+".wav";
	numstr.Format("%i",i);
	page=aud_file.SetPage(numstr);
	page->SetEntry("Type","VoiceOverComponent");
	page->SetEntry("Sample",str);
	page->SetEntry("RendererShouldExecute",1);

	}
	

	aud_file.Save();
	table_file.Save();
	CDialog::OnOK();
}

BOOL CAudioFileDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	

	CString aud_path;
	aud_path.Format("Content\\Missions\\%s\\%s.audio",MisName,MisName);
	NotationFile aud_file(aud_path);
	
	
	Page		*page;

	CString table_path;
	table_path.Format("Content\\Missions\\%s\\%s.table",MisName,MisName);
	NotationFile table_file(table_path);
	Page *sndpage=table_file.SetPage("4");
	Check_Object(sndpage);


	CString str;
	NotationFile ::PageIterator *page_list=aud_file.MakePageIterator();
	Check_Object(page_list);
	Page::NoteIterator *snd_notes=sndpage->MakeNoteIterator();
	Check_Object(snd_notes);
	Note *note;
	while((page=page_list->ReadAndNext())!=NULL)
	{
	note=snd_notes->ReadAndNext();
	Check_Object(note);
	Check_Object(page);
	const char *sname;
	page->GetEntry("Sample",&sname);
	str=sname;
	str=str.Left(str.ReverseFind('.'));
	str=str.Mid(str.ReverseFind('\\')+1);
	int itm;
	itm=m_AudList.AddString(str);
	m_AudList.SetItemDataPtr(itm,new CString(note->GetName()));
	}

	FillNewList();
	m_NewFileList.SetCurSel(0);
	m_AudList.SetCurSel(0);

	// 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 CAudioFileDlg::PruneNewList()
{
int i,idx;
CString str;

for(i=0;i<m_AudList.GetCount();i++)
{
m_AudList.GetText(i,str);
idx=m_NewFileList.FindString(-1,str);
if(idx>=0)
	m_NewFileList.DeleteString(idx);
}



}

void CAudioFileDlg::FillNewList()
{
	m_NewFileList.ResetContent();
	
	WIN32_FIND_DATA file_data;
	HANDLE file_find;

	file_find=FindFirstFile("Content\\Audio\\*.wav",&file_data);
	
	if(file_find != INVALID_HANDLE_VALUE)
	{
		CString afile_name;
		do
		{
			afile_name=file_data.cFileName;
			afile_name=afile_name.Left(afile_name.ReverseFind('.'));
			m_NewFileList.AddString(afile_name);
		}
		while (FindNextFile(file_find,&file_data));
	}

	PruneNewList();
}


void CAudioFileDlg::OnSelchangeAudiolist() 
{
UpdateData(TRUE);
	int sel=m_AudList.GetCurSel();
	if(sel>=0)
	{
	CurStr=(CString *)m_AudList.GetItemDataPtr(sel);
	m_AudName=*CurStr;
	}
	else
	{
		CurStr=NULL;
	}
UpdateData(FALSE);
}

void CAudioFileDlg::OnChangeAudname() 
{
UpdateData(TRUE);
if(CurStr)
	*CurStr=m_AudName;
	
}
