// DlgPlaybackSetup.cpp : implementation file
//

#include "stdafx.h"
#include "DlgPlaybackSetup.h"
#include <string.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgPlaybackSetup dialog


CDlgPlaybackSetup::CDlgPlaybackSetup(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgPlaybackSetup::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgPlaybackSetup)
	m_strCurrentFile = _T("");
	m_strFileName = _T("");
	m_strFileSize = _T("");
	m_strFileCount = _T("");
	//}}AFX_DATA_INIT

	m_bFolderPlayback = FALSE;
	
}


void CDlgPlaybackSetup::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgPlaybackSetup)
	DDX_Control(pDX, IDC_FILECOUNT, m_ctrlFileCount);
	DDX_Control(pDX, IDC_FILESIZE, m_ctrlFileSize);
	DDX_Control(pDX, IDC_FILENAME, m_ctrlFileName);
	DDX_Control(pDX, IDC_PLAY_SPEED, m_ctrlPlaySpeed);
	DDX_Control(pDX, IDC_CURRENT_FILE, m_ctrlCurrentFile);
	DDX_Control(pDX, IDC_OPEN_FOLDER, m_ctrlOpenFolder);
	DDX_Control(pDX, IDC_OPEN_FILE, m_ctrlOpenFile);
	DDX_Text(pDX, IDC_CURRENT_FILE, m_strCurrentFile);
	DDX_Text(pDX, IDC_FILENAME, m_strFileName);
	DDX_Text(pDX, IDC_FILESIZE, m_strFileSize);
	DDX_Text(pDX, IDC_FILECOUNT, m_strFileCount);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgPlaybackSetup, CDialog)
	//{{AFX_MSG_MAP(CDlgPlaybackSetup)
	ON_BN_CLICKED(IDC_OPEN_FILE, OnOpenFile)
	ON_BN_CLICKED(IDC_OPEN_FOLDER, OnOpenFolder)
	ON_CBN_SELCHANGE(IDC_PLAY_SPEED, OnSelchangePlaySpeed)
	ON_WM_DESTROY()
	ON_WM_CANCELMODE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgPlaybackSetup message handlers

BOOL CDlgPlaybackSetup::OnInitDialog() 
{
	CDialog::OnInitDialog();

	playSpeed[0] = 10;
	playSpeed[1] = 25;
	playSpeed[2] = 40;
	playSpeed[3] = 65;
	playSpeed[4] = 100;
	
	m_ctrlOpenFile.SetShade(SHS_METAL,8,40,4,RGB(55,55,255));
	m_ctrlOpenFolder.SetShade(SHS_METAL,8,40,4,RGB(55,55,255));
	
	m_ctrlCurrentFile.SetTextColor(RGB(255,155,50));
	m_ctrlCurrentFile.SetBkColor(RGB(0,0,0));
	m_ctrlFileName.SetTextColor(RGB(255,155,50));
	m_ctrlFileName.SetBkColor(RGB(0,0,0));
	m_ctrlFileSize.SetTextColor(RGB(255,155,50));
	m_ctrlFileSize.SetBkColor(RGB(0,0,0));
	m_ctrlFileCount.SetTextColor(RGB(255,155,50));
	m_ctrlFileCount.SetBkColor(RGB(0,0,0));

	m_ctrlPlaySpeed.SetCurSel(2);

	m_strCurrentFile.Format("No Files or Folder Selected");
	UpdateData(FALSE);

	filesBuffer = new char[MAX_FILE_BUFFER];
	ZeroMemory(filesBuffer, MAX_FILE_BUFFER);

	fileCount = 5;
	names = NULL;
	names = new CString[10];

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgPlaybackSetup::OnOpenFile() 
{
	CFileDialog dlg(TRUE,NULL, NULL,NULL,"7K Files (*.s7k)|*.s7k|XTF Files (*.xtf)|*.xtf||", this);

	((CMy6046DryEndDlg*)AfxGetMainWnd())->CloseAllOpenFiles();

	if(dlg.DoModal() != IDCANCEL)
	{	
		//Vars for storing file info
		CString name, full,fullpath, size;
		full = dlg.GetPathName();
		fullpath = full; //need 2 since we truncate one
		CFile m_file;
		if(m_file.Open(full, CFile::modeRead, NULL))
		{
			//Get just the name
			name = m_file.GetFileName();
			//Subtract the name from the path
			LPTSTR path = full.GetBufferSetLength(full.GetLength() - (name.GetLength()+1));
			//Show file info
			m_strCurrentFile.Format(path);
			m_strFileName.Format(name);
			
			//Check file length for label
			DWORD length = m_file.GetLength();
			if(length > 1000000)
				size.Format("%6f MB", (float)length/1000000);
			else
				size.Format("%d KB", length);

			m_strFileSize.Format(size);
			m_strFileCount.Format("1");

			//Close file
			m_file.Close();
			UpdateData(FALSE);

			//Now pass this path to the main dlg for reading
			((CMy6046DryEndDlg*)AfxGetMainWnd())->BeginPlay(FALSE, fullpath, playSpeed[m_ctrlPlaySpeed.GetCurSel()]);

			TRACE("Playing File %s\r\n", fullpath);
	
		}
		else
			AfxMessageBox("Error Accessing file, check file status/permissions");
	}	
}

//This function only used for quick sort in OnOpenFolder()
int compare( const void *arg1, const void *arg2 )
{
    /* Compare all of both strings: */
    const char *str1,*str2;
	str1 = ((CString*)arg1)->GetBuffer(MAX_PATH);
	str2 = ((CString*)arg2)->GetBuffer(MAX_PATH);

	int result = stricmp(str1,str2);

	((CString*)arg1)->ReleaseBuffer();
	((CString*)arg2)->ReleaseBuffer();

	return result;

}

void CDlgPlaybackSetup::OnOpenFolder() 
{
	CFileDialog dlg(TRUE,NULL, NULL,OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT,"7K Files (*.s7k)|*.s7k|XTF Files (*.xtf)|*.xtf||", this);

	dlg.m_ofn.lpstrTitle = "Choose Files to Playback...";
	dlg.m_ofn.lpstrFile	 = filesBuffer;
	dlg.m_ofn.nMaxFile	 = MAX_FILE_BUFFER;

	CString name;
	BOOL	bFilesDone = FALSE;

	if(dlg.DoModal() == IDOK)
	{
		((CMy6046DryEndDlg*)AfxGetMainWnd())->CloseAllOpenFiles();

		m_fileVector.clear();

		filePosition = dlg.GetStartPosition(); // Get pointer to list of files	
		if(!filePosition)
		{
			AfxMessageBox("Error Opening File(s)", MB_OK); 
			return; 
		}
		
		int index = 0;
		// Find out how many files have been selected 
		while(!bFilesDone)
		{
			if(filePosition == NULL)
			{
				bFilesDone = TRUE;
				break;
			}
			
			// This call will point filePosition to the next path name or NULL when done
			name = dlg.GetNextPathName(filePosition); 

			//increment index
			index++;
		}
		
		if(index >= fileCount)
		{
			delete [] names;
			names = new CString[index+1];
			fileCount = index;
		}		
		
		index = 0;
		bFilesDone = FALSE;
		filePosition = dlg.GetStartPosition(); // Get pointer to list of files	
		while(!bFilesDone)
		{
			if(filePosition == NULL)
			{
				bFilesDone = TRUE;
				break;
			}
			
			// This call will point filePosition to the next path name or NULL when done
			name = dlg.GetNextPathName(filePosition); 

			names[index] = name;

			//increment index
			index++;
		}
			
		//Now were done and ready to qsort the names
		qsort(names, index, sizeof(CString), compare);
		
		//Add file path to our main vector
		for(int i=0; i<index; i++)
		{
			m_fileVector.push_back(names[i]);
		}	
		
		UpdateData(FALSE);
	}
	else
		return;
	
	BeginFolderPlay();
	LoadNextFile();
	
}


void CDlgPlaybackSetup::OnSelchangePlaySpeed() 
{
	((CMy6046DryEndDlg*)AfxGetMainWnd())->ModifyPlaySpeed(playSpeed[m_ctrlPlaySpeed.GetCurSel()]);
}

void CDlgPlaybackSetup::OnDestroy() 
{
	delete [] filesBuffer;
	filesBuffer = NULL;

	if(names)
		delete [] names;

	CDialog::OnDestroy();
	
	m_ctrlOpenFolder.~CxShadeButton();
	m_ctrlOpenFile.~CxShadeButton();
}

void CDlgPlaybackSetup::OnCancelMode() 
{
	CDialog::OnCancelMode();	
}

LRESULT CDlgPlaybackSetup::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if((message == FILE_READ_DONE) && (m_bFolderPlayback == TRUE))
	{
		//start playing the next file
		LoadNextFile();
	}
	else if(message == RESTART_FLDR_PLAY)
	{
		BeginFolderPlay();
		LoadNextFile();
	}

	return CDialog::WindowProc(message, wParam, lParam);
}

void CDlgPlaybackSetup::BeginFolderPlay()
{
	//Setup some initial states
	m_bFolderPlayback = TRUE;
	m_uiNextFileIndex = 0;

}

void CDlgPlaybackSetup::LoadNextFile()
{
	//This will end the folder playback loop
	if(m_uiNextFileIndex == m_fileVector.size())
	{
		::PostMessage(((CMy6046DryEndDlg*)AfxGetMainWnd())->m_hWnd,ALL_FILES_PLAYED,NULL,NULL);
		return;
	}

	((CMy6046DryEndDlg*)AfxGetMainWnd())->CloseAllOpenFiles();
	
	//Setup...
	CString name, full,fullpath, size;
	CFile m_file;
	
	//Get next file name from vector
	full = m_fileVector.at(m_uiNextFileIndex);
	
	fullpath = full; //need 2 since we truncate one
	
	if(m_file.Open(full, CFile::modeRead, NULL))
	{
		//Get just the name
		name = m_file.GetFileName();
		//Subtract the name from the path
		LPTSTR path = full.GetBufferSetLength(full.GetLength() - (name.GetLength()+1));
		//Show file info
		m_strCurrentFile.Format(path);
		m_strFileName.Format(name);
		
		//Check file length for label
		DWORD length = m_file.GetLength();
		if(length > 1000000)
			size.Format("%6f MB", (float)length/1000000);
		else
			size.Format("%d KB", length);

		m_strFileSize.Format(size);
		
		m_strFileCount.Format("%d", m_fileVector.size() - m_uiNextFileIndex);
		m_uiNextFileIndex++;
		
		//Close file
		m_file.Close();
		UpdateData(FALSE);

	}
	else
	{
		AfxMessageBox("Error Accessing file(s), check file status/permissions");
		return;
	}

	//Now pass this path to the main dlg for reading
	((CMy6046DryEndDlg*)AfxGetMainWnd())->BeginPlay(TRUE, fullpath, playSpeed[m_ctrlPlaySpeed.GetCurSel()]);

}


void CDlgPlaybackSetup::LoadSettings()
{

}

void CDlgPlaybackSetup::SaveSettings()
{

}

BOOL CDlgPlaybackSetup::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
	{
		return TRUE ;
	}

	return CDialog::PreTranslateMessage(pMsg);
}
