
/*	Copyright (C) G. Jespersen
	See Licence.txt.*/

// EditMediaItemDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "EditMediaItemDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEditMediaItemDlg dialog


CEditMediaItemDlg::CEditMediaItemDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEditMediaItemDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEditMediaItemDlg)
	m_mediaName = _T("");
	m_mediaUrl = _T("");
	m_bFavorite = FALSE;
	//}}AFX_DATA_INIT

	m_pDoc = NULL;
	m_dlgTitle = _T("Edit MediaItem");
}

void CEditMediaItemDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditMediaItemDlg)
	DDX_Control(pDX, IDC_COMBO_TYPES, m_ctrlComboTypes);
	DDX_Text(pDX, IDC_EDIT_MEDIA_NAME, m_mediaName);
	DDX_Text(pDX, IDC_EDIT_URL, m_mediaUrl);
	DDX_Control(pDX, IDC_CHECK_FAVORITE, m_ctrlChkFavorites);
	DDX_Control(pDX, IDC_EDIT_URL, m_ctrlEditUrl);
	DDX_Control(pDX, IDC_EDIT_MEDIA_NAME, m_ctrlEditName);
	DDX_Check(pDX, IDC_CHECK_FAVORITE, m_bFavorite);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEditMediaItemDlg, CDialog)
	//{{AFX_MSG_MAP(CEditMediaItemDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditMediaItemDlg message handlers

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

	SetWindowText(m_dlgTitle);

	// If new MediaItem.
	BOOL isNew = FALSE;
	if(m_dlgTitle == MEDIA_NEW_STR)
		isNew = TRUE;

	// Caller must have initialized m_mediaItem.
	if(m_pDoc && (m_mediaItem.IsValidItem() || isNew) )
	{
		// Set Combo extended UI.
		//	- allows arrow keys.
		//	- disables mouse wheel.
		//	- good handling for invalid mouse clicks.
		m_ctrlComboTypes.SetExtendedUI(TRUE);

		// Set media type combo.
		InitTypesCombo();
		SetComboTypesSel();

		m_mediaName = m_mediaItem.m_mediaName;
		m_bFavorite = m_mediaItem.m_isFavorite;
		m_mediaUrl = m_mediaItem.m_mediaUrl;
		UpdateData(FALSE);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//////////////////////////////////////////////////////////
// Media Types Combo:
void CEditMediaItemDlg::InitTypesCombo()
{
	// todo - need code in CMediaItem for user strings.
	m_ctrlComboTypes.AddString(_T("Unknown Stream"));
	m_ctrlComboTypes.AddString(_T("Audio Stream"));
	m_ctrlComboTypes.AddString(_T("Video Stream"));
	m_ctrlComboTypes.AddString(_T("Video File"));
	m_ctrlComboTypes.AddString(_T("Audio File"));

	// Set width.
	//
	// Find the longest string in the combo box.
	CString str;
	CSize   sz;
	int     dx=0;
	CDC*    pDC = m_ctrlComboTypes.GetDC();
	int nTypes = m_ctrlComboTypes.GetCount();
	for(int i = 0; i < nTypes; i++)
	{
		m_ctrlComboTypes.GetLBText(i, str);
		sz = pDC->GetTextExtent(str);

		if(sz.cx > dx)
			dx = sz.cx;
	}
	m_ctrlComboTypes.ReleaseDC(pDC);

	// Adjust the width for the vertical scroll bar and the left and right border.
	dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2 * ::GetSystemMetrics(SM_CXEDGE);

	// Set the width of the list box so that every item is completely visible.
	m_ctrlComboTypes.SetDroppedWidth(dx);
}

void CEditMediaItemDlg::SetComboTypesSel()
{
	CString str = m_mediaItem.GetStreamTypeString();
	m_ctrlComboTypes.SelectString(-1, str);
}

MEDIA_TYPE CEditMediaItemDlg::GetComboTypesSelMediaType()
{
	// todo - need code in CMediaItem for user strings.
	MEDIA_TYPE type = NONE;
	CString str;

	int index = m_ctrlComboTypes.GetCurSel();
	m_ctrlComboTypes.GetLBText(index, str);

	if(str == _T("Unknown Stream"))
		type = NONE;
	else if(str == _T("Audio Stream"))
		type = AUDU;
	else if(str == _T("Video Stream"))
		type = VIDU;
	else if(str == _T("Video File"))
		type = VIDF;
	else if(str == _T("Audio File"))
		type = AUDF;
	return type;
}

////////////////////////////////////////////////////////////
// OK

// User pressed OK.
void CEditMediaItemDlg::OnOK() 
{
	// Zero flags.
	m_bFavsChange = FALSE;
	m_bTitleUrlTypeChange = FALSE;

	// Get entered data.
	UpdateData(TRUE);
	CString str;

	// Ensure entries.
	//
	// Name.
	if(m_mediaName == _T(""))
	{
		AfxMessageBox(_T("Please enter valid Media Name."));
		return;
	}
	// URL.
	if(m_mediaUrl == _T(""))
	{
		AfxMessageBox(_T("Please enter valid Media URL/File Path."));
		return;
	}
	// Media Type.
	MEDIA_TYPE type = GetComboTypesSelMediaType();
	if(type == NONE)
	{
		AfxMessageBox(_T("Please enter valid Media Type."));
		return;
	}	
	
	// Set flags.
	if(type != m_mediaItem.m_mediaType)
	{
		m_mediaItem.m_mediaType = type; 
		m_bTitleUrlTypeChange = TRUE;
	}
	// Name.
	if(m_mediaName != m_mediaItem.m_mediaName)
	{
		m_mediaItem.m_mediaName = m_mediaName; 
		m_bTitleUrlTypeChange = TRUE;
	}
	// Url.
	if(m_mediaUrl != m_mediaItem.m_mediaUrl)
	{
		m_mediaItem.m_mediaUrl = m_mediaUrl; 
		m_bTitleUrlTypeChange = TRUE;
	}
	// Favs.
	if(m_bFavorite != m_mediaItem.m_isFavorite)
	{
		m_mediaItem.m_isFavorite = m_bFavorite; 
		m_bFavsChange = TRUE;
	}
	// Done - caller to proccess results per flags.
	TRACE(_T("CEditMediaItemDlg::OnOK - calling CDialog::OnOK\n"));
	CDialog::OnOK();
}



