// NextVideoDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "By5000Asiplay Demo.h"
#include "NextVideoDlg.h"
#include ".\nextvideodlg.h"
#include "By5000asiplay.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CNextVideoDlg 对话框

IMPLEMENT_DYNAMIC(CNextVideoDlg, CDialog)
CNextVideoDlg::CNextVideoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNextVideoDlg::IDD, pParent)
	, m_uiCardNumber(0),m_EnableSetNextDvdInfo(FALSE),m_EnableSetNextAudioInfo(FALSE),uiLastDvdaudionum(0),uiLastDvdsubnum(0)
{
}

CNextVideoDlg::~CNextVideoDlg()
{
	//KillTimer(m_IDTimer);
}

void CNextVideoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CNextVideoDlg, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_NEXT_WINPOS_SET, OnBnClickedButtonNextWinposSet)
	ON_BN_CLICKED(IDC_BUTTON_NEXT_SET_SEEK, OnBnClickedButtonNextSetSeek)
	//ON_BN_CLICKED(IDC_BUTTON_SUBPICTURE_INDEX, OnBnClickedButtonSubpictureIndex)
	//ON_BN_CLICKED(IDC_BUTTON_DVD_AUDIO_INDEX, OnBnClickedButtonDvdAudioIndex)
	ON_CBN_SELCHANGE(IDC_EDIT_SUBPICTURE_INDEX, OnCbnSelchangeEditSubpictureIndex)
	ON_CBN_SELCHANGE(IDC_EDIT_DVD_AUDIO_INDEX, OnCbnSelchangeEditDvdAudioIndex)
END_MESSAGE_MAP()


// CNextVideoDlg 消息处理程序

void CNextVideoDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
//	OnOK();
}

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

	// TODO:  Add extra initialization here
	UINT uiTemp;
	Seekfit_By5000asiplay_GetVideoMode(uiTemp, m_uiCardNumber);
	m_uiVideoMode = uiTemp;

	SetDlgItemText(IDC_EDIT_SUBPICTURE_INDEX, _T("0"));
	SetDlgItemText(IDC_EDIT_DVD_AUDIO_INDEX, _T("0"));

	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRCX))->SetWindowText(_T("0"));
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRCY))->SetWindowText(_T("0"));
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRC_WIDTH))->SetWindowText(_T("0"));
	if (m_uiVideoMode == VIDEO_MODE_PAL)
		((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRC_HEIGHT))->SetWindowText(_T("0"));
	else
		((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRC_HEIGHT))->SetWindowText(_T("0"));
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DSTX))->SetWindowText(_T("0"));
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DSTY))->SetWindowText(_T("0"));
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DST_WIDTH))->SetWindowText(_T("720"));
	if (m_uiVideoMode == VIDEO_MODE_PAL)
		((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DST_HEIGHT))->SetWindowText(_T("576"));
	else
		((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DST_HEIGHT))->SetWindowText(_T("480"));

	SetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_HOUR, _T("00"));
	SetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_MINUTE, _T("00"));
	SetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_SECOND, _T("00"));
	SetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_FRAME, _T("00"));

	SetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_HOUR, _T("00"));
	SetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_MINUTE, _T("00"));
	SetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_SECOND, _T("00"));
	SetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_FRAME, _T("00"));

	m_IDTimer = SetTimer(1,20,NULL);

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

void CNextVideoDlg::OnBnClickedButtonNextWinposSet()
{
	// TODO: Add your control notification handler code here
	CString strText;
	UINT uiXStart,uiYStart,uiWidth,uiHeight;
	uiXStart=uiYStart=uiWidth=uiHeight = 0;
	Seekfit_By5000asiplay_MediaEnablePlaybackDve(m_uiCardNumber);
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRCX))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRCY))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRC_WIDTH))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_SRC_HEIGHT))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	Seekfit_By5000asiplay_MediaNextSetDveSourceWindow(uiXStart,uiYStart,uiWidth,uiHeight,m_uiCardNumber);

	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DSTX))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DSTY))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DST_WIDTH))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	((CEdit *)GetDlgItem(IDC_EDIT_NEXT_DST_HEIGHT))->GetWindowText(strText);
#ifdef _UNICODE
	swscanf_s(strText,_T("%d"),&uiXStart);

#else
	sscanf_s(strText,_T("%d"),&uiXStart);
#endif
	Seekfit_By5000asiplay_MediaNextSetDveDestWindow(uiXStart,uiYStart,uiWidth,uiHeight,m_uiCardNumber);
}

void CNextVideoDlg::OnBnClickedButtonNextSetSeek()
{
	// TODO: Add your control notification handler code here
	UINT uiHour;
	UINT uiMinute;
	UINT uiSecond;
	UINT uiFrame;
	ULONG ulSeekIn;
	ULONG ulSeekOut;
	CString str;

	GetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_HOUR, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiHour);
#else
	sscanf_s(str, _T("%d"), &uiHour);
#endif
	GetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_MINUTE, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiMinute);
#else
	sscanf_s(str, _T("%d"), &uiMinute);
#endif
	GetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_SECOND, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiSecond);

#else
	sscanf_s(str, _T("%d"), &uiSecond);
#endif
	GetDlgItemText(IDC_EDIT_NEXT_SEEK_IN_FRAME, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiFrame);

#else
	sscanf_s(str, _T("%d"), &uiFrame);
#endif

	if (uiHour >= 256 || uiMinute >= 60 || uiSecond >= 60 || uiFrame >= 60)
	{
		AfxMessageBox(_T("请检查参数设置是否正确！"));
		return;
	}

	ulSeekIn = (uiHour << 24) + (uiMinute << 16) + (uiSecond << 8) + uiFrame;

	GetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_HOUR, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiHour);

#else
	sscanf_s(str, _T("%d"), &uiHour);
#endif
	GetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_MINUTE, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiMinute);

#else
	sscanf_s(str, _T("%d"), &uiMinute);
#endif
	GetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_SECOND, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiSecond);

#else
	sscanf_s(str, _T("%d"), &uiSecond);
#endif
	GetDlgItemText(IDC_EDIT_NEXT_SEEK_OUT_FRAME, str);
#ifdef _UNICODE
	swscanf_s(str, _T("%d"), &uiFrame);

#else
	sscanf_s(str, _T("%d"), &uiFrame);
#endif

	if (uiHour >= 256 || uiMinute >= 60 || uiSecond >= 60 || uiFrame >= 60)
	{
		AfxMessageBox(_T("请检查参数设置是否正确！"));
		return;
	}

	ulSeekOut = (uiHour << 24) + (uiMinute << 16) + (uiSecond << 8) + uiFrame;

	Seekfit_By5000asiplay_MediaNextSeek(ulSeekIn, ulSeekOut, m_uiCardNumber);
}


void CNextVideoDlg::OnTimer(UINT nIDEvent)
{
	CString str;
	UINT uiHour;
	UINT uiMinute;
	UINT uiSecond;
	UINT uiFrame;
	//ULONG ulTime;

	Seekfit_By5000asiplay_MediaNextGetFileInformation(m_mfiMediaNextFileInformation, m_uiCardNumber);

	uiHour = m_mfiMediaNextFileInformation.ulTimeDuration >> 24;
	uiMinute = (m_mfiMediaNextFileInformation.ulTimeDuration >> 16) & 0xff;
	uiSecond = (m_mfiMediaNextFileInformation.ulTimeDuration >> 8) & 0xff;
	uiFrame = (m_mfiMediaNextFileInformation.ulTimeDuration >> 0) & 0xff;
	str.Format(_T("%02d:%02d:%02d:%02d"), uiHour, uiMinute, uiSecond, uiFrame);
	GetDlgItem(IDC_NEXT_FILE_TIME_DURATION)->SetWindowText(str);
	str.Format(_T("%d"), m_mfiMediaNextFileInformation.uiVideoWidth);
	GetDlgItem(IDC_NEXT_FILE_WIDTH)->SetWindowText(str);
	//GetDlgItem(IDC_EDIT_NEXT_SRC_WIDTH)->SetWindowText(str);
	str.Format(_T("%d"), m_mfiMediaNextFileInformation.uiVideoHeight);
	GetDlgItem(IDC_NEXT_FILE_HEIGHT)->SetWindowText(str);
	//GetDlgItem(IDC_EDIT_NEXT_SRC_HEIGHT)->SetWindowText(str);

	if (uiLastDvdsubnum !=m_mfiMediaNextFileInformation.uiDvdSubPictureNumber)
	{
		m_EnableSetNextDvdInfo = FALSE;
		((CComboBox *)GetDlgItem(IDC_EDIT_SUBPICTURE_INDEX))->ResetContent();
		uiLastDvdsubnum =m_mfiMediaNextFileInformation.uiDvdSubPictureNumber;
	}
	if (uiLastDvdaudionum != m_mfiMediaNextFileInformation.uiDvdAudioNumber)
	{
		m_EnableSetNextAudioInfo = FALSE;
		((CComboBox *)GetDlgItem(IDC_EDIT_DVD_AUDIO_INDEX))->ResetContent();
		uiLastDvdaudionum = m_mfiMediaNextFileInformation.uiDvdAudioNumber;
	}

	if (!m_EnableSetNextDvdInfo||!m_EnableSetNextAudioInfo)
	{

		if (m_mfiMediaNextFileInformation.uiDvdSubPictureNumber>0||m_mfiMediaNextFileInformation.uiDvdAudioNumber>0)
		{
			for(INT i=0;i<m_mfiMediaNextFileInformation.uiDvdSubPictureNumber;i++)
			{
				str.Format(_T("%d"),i);
				((CComboBox *)GetDlgItem(IDC_EDIT_SUBPICTURE_INDEX))->AddString(str);
			}
			((CComboBox *)GetDlgItem(IDC_EDIT_SUBPICTURE_INDEX))->SetCurSel(0);
			for(INT i=0;i<m_mfiMediaNextFileInformation.uiDvdAudioNumber;i++)
			{
				str.Format(_T("%d"),i);
				((CComboBox *)GetDlgItem(IDC_EDIT_DVD_AUDIO_INDEX))->AddString(str);
				
			}
			((CComboBox *)GetDlgItem(IDC_EDIT_DVD_AUDIO_INDEX))->SetCurSel(0);
		} 
		else
		{
			if (!m_EnableSetNextDvdInfo)
			{
				((CComboBox *)GetDlgItem(IDC_EDIT_SUBPICTURE_INDEX))->AddString(_T("0"));
				((CComboBox *)GetDlgItem(IDC_EDIT_SUBPICTURE_INDEX))->SetCurSel(0);
			}
			if (!m_EnableSetNextAudioInfo)
			{
				((CComboBox *)GetDlgItem(IDC_EDIT_DVD_AUDIO_INDEX))->AddString(_T("0"));
				((CComboBox *)GetDlgItem(IDC_EDIT_DVD_AUDIO_INDEX))->SetCurSel(0);
			}
			
		}
		m_EnableSetNextDvdInfo = TRUE;
		m_EnableSetNextAudioInfo = TRUE;
	}
	CDialog::OnTimer(nIDEvent);
}

// void CNextVideoDlg::OnBnClickedButtonSubpictureIndex()
// {
// 	// TODO: 在此添加控件通知处理程序代码
// 	CString str = _T("");
// 	UINT index=0 ;
// 	((CEdit *)GetDlgItem(IDC_EDIT_SUBPICTURE_INDEX))->GetWindowText(str);
// 	sscanf_s(str,_T("%d"),&index);
// 	Seekfit_By5000asiplay_MediaNextSetDvdSubPictureIndex(index, m_uiCardNumber);
// }
// 
// void CNextVideoDlg::OnBnClickedButtonDvdAudioIndex()
// {
// 	// TODO: 在此添加控件通知处理程序代码
// 	CString str = _T("");
// 	UINT index=0 ;
// 	((CEdit *)GetDlgItem(IDC_EDIT_DVD_AUDIO_INDEX))->GetWindowText(str);
// 	sscanf_s(str,_T("%d"),&index);
// 	Seekfit_By5000asiplay_MediaNextSetDvdAudioIndex(index, m_uiCardNumber);
// }

void CNextVideoDlg::OnCbnSelchangeEditSubpictureIndex()
{
	// TODO: 在此添加控件通知处理程序代码
	INT nIndex = ((CComboBox*)GetDlgItem(IDC_EDIT_SUBPICTURE_INDEX))->GetCurSel();
	Seekfit_By5000asiplay_MediaNextSetDvdSubPictureIndex(nIndex, m_uiCardNumber);
}

void CNextVideoDlg::OnCbnSelchangeEditDvdAudioIndex()
{
	// TODO: 在此添加控件通知处理程序代码
	INT nIndex = ((CComboBox*)GetDlgItem(IDC_EDIT_DVD_AUDIO_INDEX))->GetCurSel();
	Seekfit_By5000asiplay_MediaNextSetDvdAudioIndex(nIndex, m_uiCardNumber);
}
