// MixerPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "MixerPage.h"

//included for _chdir
#include <direct.h> 


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMixerPage property page

IMPLEMENT_DYNCREATE(CMixerPage, CPropertyPage)

CMixerPage::CMixerPage() : CPropertyPage(CMixerPage::IDD)
{
	//{{AFX_DATA_INIT(CMixerPage)
	m_cSelectedRes = _T("");
	m_LCBPath = _T("");
	m_bDebugBreak = FALSE;
	m_bFirstOccurence = FALSE;
	m_bShowBankWithId = FALSE;
	m_bShowResWithId = FALSE;
	m_lBankId = 0;
	m_lResId = 0;
	m_ulSelectedResVol = 0;
	m_cSelectedResName = _T("");
	//}}AFX_DATA_INIT
}

CMixerPage::~CMixerPage()
{
}

void CMixerPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMixerPage)
	DDX_Control(pDX, IDC_VOLUME_SLIDER, m_VolumeSlider);
	DDX_Control(pDX, IDC_RES_LIST, m_ResList);
	DDX_Text(pDX, IDC_EDIT_LCB_PATH, m_LCBPath);
	DDX_Check(pDX, IDC_CHECK_DEBUG_ON_RES, m_bDebugBreak);
	DDX_Check(pDX, IDC_CHECK_FIRST_RES_OCCUR, m_bFirstOccurence);
	DDX_Check(pDX, IDC_CHECK_RES_BANK, m_bShowBankWithId);
	DDX_Check(pDX, IDC_CHECK_RES, m_bShowResWithId);
	DDX_Text(pDX, IDC_EDIT_RES_BANK_ID, m_lBankId);
	DDV_MinMaxLong(pDX, m_lBankId, 0, 256);
	DDX_Text(pDX, IDC_EDIT_RES_ID, m_lResId);
	DDV_MinMaxLong(pDX, m_lResId, 0, 1024);
	DDX_Text(pDX, IDC_EDIT_RES_VOL, m_ulSelectedResVol);
	DDV_MinMaxLong(pDX, m_ulSelectedResVol, 0, 127);
	DDX_Text(pDX, IDC_SEL_RES_NAME, m_cSelectedResName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMixerPage, CPropertyPage)
	//{{AFX_MSG_MAP(CMixerPage)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_PAUSE_RES_BUTTON, OnPauseResButton)
	ON_BN_CLICKED(IDC_RESET_RESLIST_BUTTON, OnResetReslistButton)
	ON_BN_CLICKED(IDC_CHECK_DEBUG_ON_RES, OnCheckDebugOnRes)
	ON_BN_CLICKED(IDC_CHECK_FIRST_RES_OCCUR, OnCheckFirstResOccur)
	ON_BN_CLICKED(IDC_CHECK_RES, OnCheckRes)
	ON_BN_CLICKED(IDC_CHECK_RES_BANK, OnCheckResBank)
	ON_EN_CHANGE(IDC_EDIT_RES_BANK_ID, OnChangeEditResBankId)
	ON_EN_CHANGE(IDC_EDIT_RES_ID, OnChangeEditResId)
	ON_BN_CLICKED(IDC_BROWSE_FOR_LCB, OnBrowseForLcb)
	ON_BN_CLICKED(IDC_VOL_MOINS, OnVolMoins)
	ON_BN_CLICKED(IDC_VOL_PLUS, OnVolPlus)
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMixerPage message handlers



BOOL CMixerPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();


	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	//Initialise the ClistCtrl containing ressources descriptions
	LV_COLUMN* lvc;

	lvc = (LV_COLUMN*)malloc(sizeof(LV_COLUMN));

	lvc->mask = LVCF_FMT |LVCF_SUBITEM 
		| LVCF_TEXT | LVCF_WIDTH;			// members contain valid information
    lvc->fmt = LVCFMT_LEFT;					// Alignment of the column
    lvc->cx = 75;							// width of the column, in pixels
    lvc->pszText = "Time";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_ResList.InsertColumn(0, lvc);


	lvc->pszText = "Resource Name";		// column heading 
	lvc->cx = 125;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_ResList.InsertColumn(1, lvc);

	lvc->pszText = "Id";			// column heading 
	lvc->cx = 30;							// width of the column, in pixels
    lvc->iSubItem = 2;						// index of subitem associated with column
	m_ResList.InsertColumn(2, lvc);
	
	lvc->pszText = "Bank Name";				// column heading 
	lvc->cx = 100;							// width of the column, in pixels
    lvc->iSubItem = 3;						// index of subitem associated with column
	m_ResList.InsertColumn(3, lvc);
	
	lvc->pszText = "Bank Id";				// column heading 
	lvc->cx = 60;							// width of the column, in pixels
    lvc->iSubItem = 4;						// index of subitem associated with column
	m_ResList.InsertColumn(4, lvc);

	
	lvc->cx = 100;							// width of the column, in pixels
	lvc->pszText = "Type";		// column heading 
    lvc->iSubItem = 5;						// index of subitem associated with column
	m_ResList.InsertColumn(5, lvc);


	lvc->cx = 50;							// width of the column, in pixels
	lvc->pszText = "Volume";				// column heading 
    lvc->iSubItem = 6;						// index of subitem associated with column
	m_ResList.InsertColumn(6, lvc);


	free( lvc );

	
	//
	//Initialisation ds controles du DialogBox
	//
	m_VolumeSlider.SetRange( 0, 127, FALSE);
	m_VolumeSlider.SetPos(127);


	//Set the limit length of the volume edit box
	((CEdit *)(GetDlgItem(IDC_EDIT_RES_VOL)))->LimitText(3);

	//Set LCB Edit box
	((CEdit *)(GetDlgItem(IDC_EDIT_RES_VOL)))->SetReadOnly( TRUE );
	((CEdit *)(GetDlgItem(IDC_SEL_RES_NAME)))->SetReadOnly( TRUE );
	((CEdit *)(GetDlgItem(IDC_EDIT_LCB_PATH)))->SetReadOnly( TRUE );

	m_cSelectedResName = "No Selected Resource";


	//Disable the + and - buttonsa and the slider.  The user wont be able to save nothing
	((CEdit *)(GetDlgItem(IDC_VOL_MOINS)))->EnableWindow( FALSE );
	((CEdit *)(GetDlgItem(IDC_VOL_PLUS)))->EnableWindow( FALSE );
	((CEdit *)(GetDlgItem(IDC_VOLUME_SLIDER)))->EnableWindow( FALSE );


	//Add the LCB Path
	if (pSuperVisApp->m_cLCBPath == "NoLCBPath")
		m_LCBPath = "No LCB file found in the registry. Select One.";
	else
		m_LCBPath = pSuperVisApp->m_cLCBPath + pSuperVisApp->m_cLCBFileTitle + ".lcb";


	//Init du "pause" mode
	m_bPauseProcess = FALSE;
	
	UpdateData(FALSE);

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CMixerPage::PreTranslateMessage(MSG* pMsg) 
{
	
	if(pMsg->message == WM_USER_UPDATE_DATA)
	{
		//We send the updatedata(TRUE) here
		//We cannot pass the CWnd across thread and this cause problem while using updatedata
		UpdateData(TRUE);
	}
	
	return CPropertyPage::PreTranslateMessage(pMsg);
}



void CMixerPage::OnSave() 
{

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

		if (!pSuperVisApp->m_SuperVisThread->m_IEditor->SaveDocument("x:\\cpa\\exe\\main\\gamedata\\world\\sound\\playmo_v.lcb"))
		{
			AfxMessageBox("Could not save the sound project");
			return;
		}

		
}


void CMixerPage::OnPauseResButton() 
{
	if(m_bPauseProcess)
	{
		m_bPauseProcess = FALSE;

		GetDlgItem(IDC_PAUSE_RES_BUTTON)->SetWindowText("&Pause Process");

		//We refresh the list to add the event thatwe missed during pause.
		RefreshResList();
	} 
	else
	{
		m_bPauseProcess = TRUE;

		GetDlgItem(IDC_PAUSE_RES_BUTTON)->SetWindowText("&Unpause Process");
	}	
	
}

void CMixerPage::OnResetReslistButton() 
{
	long i;
	ResourceElement* temp;

	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	
	//Free the events Resources array
	for (i= 0;  i < pSuperVisApp->m_SuperVisThread->m_apResources.GetSize() ;i++)
	{
		temp = (ResourceElement *)(pSuperVisApp->m_SuperVisThread->m_apResources.GetAt(i));
		delete temp;
	}

	pSuperVisApp->m_SuperVisThread->m_apResources.RemoveAll();


	//Empty the CListCtrl
	m_ResList.DeleteAllItems();	
}

void CMixerPage::RefreshResList()
{

	long i;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();


	//Clear the list
	m_ResList.DeleteAllItems();
	
	long temp = pSuperVisApp->m_SuperVisThread->m_apResources.GetSize();
	
	//Cycle through the pointer Array to filter
	for (i = 0; i < pSuperVisApp->m_SuperVisThread->m_apResources.GetSize(); i ++ )
	{
	
		//Update the rsources list
		FilterAndDisplayRes(i);
		

	}	

}

void CMixerPage::FilterAndDisplayRes(long lIndex)
{

	char buffer[25];
	
	unsigned long ulBankId;
	unsigned long ulResId;
	

	ResourceElement *res, *ResFirstOccurTest;
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	res = (ResourceElement *)(pSuperVisApp->m_SuperVisThread->m_apResources.GetAt(lIndex));


	ulBankId = ((res->ulId >> 16) & 0x0000FFFF);
	ulResId = res->ulId & 0x0000FFFF;


	//
	//Check if we have have to filter the events
	//******************************************

	//Retrieve data from the control
	PostMessage(WM_USER_UPDATE_DATA, 0, 0);


	if (m_bShowResWithId)
	{
		if (m_lResId != (long)ulResId)
			return;

	}

	if (m_bShowBankWithId)
	{
		if (m_lBankId != (long)ulBankId)
			return;
	}


	if(m_bFirstOccurence)
	{
		
		long i;

		//look in the Ressource array from 0 to the current event we are filtering.
		for(i = 0; i < lIndex ; i++)
		{
			ResFirstOccurTest = (ResourceElement *)(pSuperVisApp->m_SuperVisThread->m_apResources.GetAt(i));
			if (res->ulId == ResFirstOccurTest->ulId)
				return;
		}
	}

	
	//Check if we have to debugbreak before displaying the events
	if(m_bDebugBreak)
	{
		DebugBreak();
	}



	
/*	
	long i;
	unsigned long ulId;
	
	//Get the group using the editor Id
	IEventGroup Group;
	CString cBankName;
	
	for (i= 0; i < pSuperVisApp->m_SuperVisThread->m_IGroups->GetCount(); i++)
	{
		Group.AttachDispatch(pSuperVisApp->m_SuperVisThread->m_IGroups->Item(i));
		ulId = Group.GetId();
		if(ulId == ulBankId)
		{
			cBankName = Group.GetName();
			break;
		}
	}

	//Get the resources list
	IResources Resources;
	Resources.AttachDispatch(Group.GetResources());

	IResource Resource;
	CString cResName;

	for (i= 0; i < Resources.GetCount(); i++)
	{
		Resource.AttachDispatch(Resources. Item (i));
		ulId = Resource.GetId();
		if(ulId == ulResId)
		{
			cResName = Resource.GetName();
			break;
		}
	}

*/
	//Display time
	sprintf(buffer,"%02d:%02d:%02d:%03d",
		res->time.wHour,
		res->time.wMinute,
		res->time.wSecond,
		res->time.wMilliseconds);
	m_ResList.InsertItem( 0, buffer );


	//Set the item data with the Resources Array index, to be used later for drag&drop, pop-memnu, etc.
	m_ResList.SetItemData( 0, (DWORD)lIndex );


	//resource Name
	m_ResList.SetItem(0, 1, LVIF_TEXT, (LPCSTR)res->cResName, 0, 0, 0, NULL );

	//Event ID 
	sprintf(buffer, "%lu", ulResId);
	m_ResList.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Bank Name
	m_ResList.SetItem(0, 3, LVIF_TEXT, (LPCSTR)res->cResBankName, 0, 0, 0, NULL );
	
	//Bank ID 
	sprintf(buffer, "%lu", ulBankId);
	m_ResList.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	
	//Type
	switch(res->tdeType){

	case TYPE_INVALID:
		sprintf(buffer, "TYPE_INVALID");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SAMPLE:
		sprintf(buffer, "TYPE_SAMPLE");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_MIDI:
		sprintf(buffer, "TYPE_MIDI");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_CDAUDIO:
		sprintf(buffer, "TYPE_CDAUDIO");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SEQUENCE:
		sprintf(buffer, "TYPE_SEQUENCE");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SWITCH_OLD:
		sprintf(buffer, "TYPE_SWITCH_OLD");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SPLIT:
		sprintf(buffer, "TYPE_SPLIT");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME_OLD:
		sprintf(buffer, "TYPE_THEME_OLD");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	case TYPE_SWITCH:
		sprintf(buffer, "TYPE_SWITCH");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME_OLD2:
		sprintf(buffer, "TYPE_THEME_OLD2");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_RANDOM:
		sprintf(buffer, "TYPE_RANDOM");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME:
		sprintf(buffer, "TYPE_THEME");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_FOR_ALIGNEMENT:
		sprintf(buffer, "TYPE_FOR_ALIGNEMENT");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;

	}


	//Volume 
	sprintf(buffer, "%u", res->ucVolume);
	m_ResList.SetItem(0, 6, LVIF_TEXT, buffer, 0, 0, 0, NULL );

}

void CMixerPage::OnCheckDebugOnRes() 
{
	//Update the variable
	UpdateData(TRUE);
	
}

void CMixerPage::OnCheckFirstResOccur() 
{
	//Update the variable
	UpdateData(TRUE);
	RefreshResList();
	
}

void CMixerPage::OnCheckRes() 
{

	//Update the variable
	UpdateData(TRUE);
	RefreshResList();
	

}

void CMixerPage::OnCheckResBank() 
{
	//Update the variable
	UpdateData(TRUE);
	RefreshResList();
	
}

void CMixerPage::OnChangeEditResBankId() 
{
	//Keep the bank Id in the 0 - 256 range
	UpdateData(TRUE);
	if (m_lBankId > 256)
	{
		m_lBankId = 256;
		UpdateData(FALSE);

	}

	RefreshResList();

	
}

void CMixerPage::OnChangeEditResId() 
{
	//Keep the Event Id in the 0 - 1024 range
	UpdateData(TRUE);
	if (m_lResId > 1024)
	{
		m_lResId = 1024;
		UpdateData(FALSE);

	}

	RefreshResList();
	
}

void CMixerPage::OnBrowseForLcb() 
{
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_EventsHistoryPage.BrowseForLcb();
	UpdateData(FALSE);

}

void CMixerPage::SetEditBoxVolume(unsigned long _ulIndex)
{

	unsigned long ulBankId;
	unsigned long ulResId;
	
	ResourceElement* temp;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	
	//Enable the + and - buttons.
	((CEdit *)(GetDlgItem(IDC_VOL_MOINS)))->EnableWindow( TRUE );
	((CEdit *)(GetDlgItem(IDC_VOL_PLUS)))->EnableWindow( TRUE );
	((CEdit *)(GetDlgItem(IDC_VOLUME_SLIDER)))->EnableWindow( TRUE );
	

	//Save the Array Index to be used by the OnVolMoins() and OnVolPlus() messages
	m_ulSelectedResourceArrayIndex = _ulIndex;	
	
	temp = (ResourceElement *)(pSuperVisApp->m_SuperVisThread->m_apResources.GetAt(_ulIndex));

	ulBankId = ((temp->ulId >> 16) & 0x0000FFFF);
	ulResId = temp->ulId & 0x0000FFFF;


	long i;
	unsigned long ulId;
	
	//Get the group using the editor Id 
	IEventGroup Group;

	
	for (i= 0; i < pSuperVisApp->m_SuperVisThread->m_IGroups->GetCount(); i++)
	{
		Group.AttachDispatch(pSuperVisApp->m_SuperVisThread->m_IGroups->Item(i));
		ulId = Group.GetId();
		if(ulId == ulBankId)
			break;
	}

	//Get the resources list
	IResources Resources;
	Resources.AttachDispatch(Group.GetResources());


	for (i= 0; i < Resources.GetCount(); i++)
	{
		pSuperVisApp->m_SuperVisThread->m_Resource.AttachDispatch(Resources. Item (i));
		ulId = pSuperVisApp->m_SuperVisThread->m_Resource.GetId();
		if(ulId == ulResId)
		{
			m_ulSelectedResVol = pSuperVisApp->m_SuperVisThread->m_Resource.GetVolume();
			m_VolumeSlider.SetPos(127 - m_ulSelectedResVol);
			m_cSelectedResName = temp->cResName;
			break;
		}
	}

	UpdateData(FALSE);

}

void CMixerPage::OnVolMoins() 
{
	ResourceElement* temp;
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	m_ulSelectedResVol--;
	m_VolumeSlider.SetPos(127 - m_ulSelectedResVol);
	
	//Set the volume in SounEditor using OLE
	pSuperVisApp->m_SuperVisThread->m_Resource.SetVolume((int)m_ulSelectedResVol);


	//Edit the Sound in realtime....
	temp = (ResourceElement *)(pSuperVisApp->m_SuperVisThread->m_apResources.GetAt(m_ulSelectedResourceArrayIndex));

	temp->res->ucVolume = (unsigned char)m_ulSelectedResVol;

	UpdateData(FALSE);
	
}

void CMixerPage::OnVolPlus() 
{
	ResourceElement* temp;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	m_ulSelectedResVol++;
	m_VolumeSlider.SetPos(127 - m_ulSelectedResVol);
	
	//Set the volume in SounEditor using OLE
	pSuperVisApp->m_SuperVisThread->m_Resource.SetVolume((int)m_ulSelectedResVol);

	
	//Edit the Sound in realtime....
	temp = (ResourceElement *)(pSuperVisApp->m_SuperVisThread->m_apResources.GetAt(m_ulSelectedResourceArrayIndex));

	temp->res->ucVolume = (unsigned char)m_ulSelectedResVol;
	

	UpdateData(FALSE);
	
}


void CMixerPage::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (nSBCode == SB_THUMBTRACK){

		if (pScrollBar == (CScrollBar *)&m_VolumeSlider){


			ResourceElement* temp;
			CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
			
			m_ulSelectedResVol = 127 - m_VolumeSlider.GetPos();

			//Set the volume in SounEditor using OLE
			pSuperVisApp->m_SuperVisThread->m_Resource.SetVolume((int)m_ulSelectedResVol);

	
			//Edit the Sound in realtime....
			temp = (ResourceElement *)(pSuperVisApp->m_SuperVisThread->m_apResources.GetAt(m_ulSelectedResourceArrayIndex));

			temp->res->ucVolume = (unsigned char)m_ulSelectedResVol;


			UpdateData(FALSE);
	
		}


	}
	
	CPropertyPage::OnVScroll(nSBCode, nPos, pScrollBar);
}
