// DlgPlayBack.cpp : implementation file
//

#include "stdafx.h"
#include "MsSDKTest.h"
#include "DlgPlayBack.h"
#include <math.h>

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

#define TIMER_PLAY 1000
/////////////////////////////////////////////////////////////////////////////
// CDlgPlayBack dialog


CDlgPlayBack::CDlgPlayBack(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgPlayBack::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgPlayBack)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgPlayBack::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgPlayBack)
	DDX_Control(pDX, IDC_STATIC_TIME, m_wndTime);
	DDX_Control(pDX, IDC_SLIDER_PLAY, m_slider);
	DDX_Control(pDX, IDC_STATIC_DISPLAY, m_wndPlay);
	DDX_Control(pDX, IDC_LIST_FILE, m_listFile);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgPlayBack, CDialog)
	//{{AFX_MSG_MAP(CDlgPlayBack)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTN_SLOWLY, OnBtnSlowly)
	ON_BN_CLICKED(IDC_BTN_QUICKLY, OnBtnQuickly)
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_BN_CLICKED(IDC_BTN_PAUSE, OnBtnPause)
	ON_BN_CLICKED(IDC_BTN_FIND, OnBtnFind)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_FILE, OnDblclkListFile)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_PLAY, OnClickSlider)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgPlayBack message handlers

BOOL CDlgPlayBack::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_nSpeed = IPC_PLAY_SPEED_1;
	m_nSpeedSlow = IPC_PLAY_SPEED_1;
	m_nSpeedQuick = IPC_PLAY_SPEED_1;
	m_bPause = FALSE;
	m_bStop = FALSE;
	memset(m_sVideoFile, 0, sizeof(m_sVideoFile));
	InitListCtrl();

	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START_DATE))->SetFormat(_T("yyyy-MM-dd"));
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START_TIME))->SetFormat(_T("HH:mm:ss"));
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_STOP_DATE))->SetFormat(_T("yyyy-MM-dd"));
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_STOP_TIME))->SetFormat(_T("HH:mm:ss"));

	SYSTEMTIME startTime = {0}; 
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START_TIME))->GetTime(&startTime);
	startTime.wHour = 0;
	startTime.wMinute = 0;
	startTime.wSecond = 0;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START_TIME))->SetTime(&startTime);


	char sVideFilePath[MAX_PATH] = {0};
	if(!GetProgramFilePath(sVideFilePath, sizeof(sVideFilePath)))
		_snprintf(sVideFilePath, sizeof(sVideFilePath), "%s\\test.avi", sVideFilePath);
	else
		_snprintf(sVideFilePath, sizeof(sVideFilePath)-1, "H:\\MyProject\\SDK\\MsIpcSDK\\MsIpcSDK_Project\\MsSDKTest\\test.avi");
	PlayFile(sVideFilePath);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgPlayBack::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	KillTimer(TIMER_PLAY);
	Ms_IpcClient_StopPlayBack(m_nUserId);
}

void CDlgPlayBack::OnBtnSlowly() 
{
	if(m_nSpeedSlow == IPC_PLAY_SPEED_16){
		m_nSpeedSlow = IPC_PLAY_SPEED_1;
		Ms_IpcClient_PlayBackControl(m_nUserId, IPC_PLAY_COMMAND_NORMAL, 0);
		return;
	}else{
		m_nSpeedSlow++;
	}
	/*int nQuick = pow(2, m_nSpeedQuick);
	int nSlow = pow(2, m_nSpeedSlow);
	if(nQuick > nSlow)
		m_nSpeed = nQuick/nSlow;
	else
		m_nSpeed = nSlow/nQuick;
	m_nSpeed = sqrt(m_nSpeed);*/

	m_nSpeed = (m_nSpeedSlow - m_nSpeedQuick);
	if(m_nSpeed > 0)
		Ms_IpcClient_PlayBackControl(m_nUserId, IPC_PLAY_COMMAND_SLOW, abs(m_nSpeed));
	else
		Ms_IpcClient_PlayBackControl(m_nUserId, IPC_PLAY_COMMAND_QUICK, abs(m_nSpeed));
}

void CDlgPlayBack::OnBtnQuickly() 
{
	if(m_nSpeedQuick == IPC_PLAY_SPEED_16){
		m_nSpeedQuick = IPC_PLAY_SPEED_1;
		Ms_IpcClient_PlayBackControl(m_nUserId, IPC_PLAY_COMMAND_NORMAL, 0);
		return;
	}else{
		m_nSpeedQuick++;
	}
	/*int nQuick = pow(2, m_nSpeedQuick);
	int nSlow = pow(2, m_nSpeedSlow);
	if(nQuick > nSlow)
		m_nSpeed = nQuick/nSlow;
	else
		m_nSpeed = nSlow/nQuick;
	m_nSpeed = sqrt(m_nSpeed);*/

	m_nSpeed = (m_nSpeedQuick-m_nSpeedSlow);
	if(m_nSpeed > 0)
		Ms_IpcClient_PlayBackControl(m_nUserId, IPC_PLAY_COMMAND_QUICK, abs(m_nSpeed));
	else
		Ms_IpcClient_PlayBackControl(m_nUserId, IPC_PLAY_COMMAND_SLOW, abs(m_nSpeed));
}

void CDlgPlayBack::OnBtnStop() 
{
	// TODO: Add your control notification handler code here
	if(!m_bStop){
		Ms_IpcClient_StopPlayBack(m_nUserId);
		m_wndPlay.Invalidate();
		GetDlgItem(IDC_BTN_STOP)->SetWindowText("Play");
		m_slider.SetPos(0);
	}else{
		PlayFile(m_sVideoFile);
		m_nSpeed = IPC_PLAY_SPEED_1;
		m_nSpeedSlow = IPC_PLAY_SPEED_1;
		m_nSpeedQuick = IPC_PLAY_SPEED_1;
		m_bPause = FALSE;
		GetDlgItem(IDC_BTN_STOP)->SetWindowText("Stop");
	}
	m_bStop = !m_bStop;
	UpdateData(FALSE);
}

void CDlgPlayBack::OnBtnPause() 
{
	// TODO: Add your control notification handler code here
	if(!m_bPause){
		Ms_IpcClient_PlayBackPause(m_nUserId);
		GetDlgItem(IDC_BTN_PAUSE)->SetWindowText("Resume");
	}else{
		Ms_IpcClient_PlayBackResume(m_nUserId);
		GetDlgItem(IDC_BTN_PAUSE)->SetWindowText("Pause");
	}
	m_bPause = !m_bPause;
	UpdateData(FALSE);
}

void CDlgPlayBack::OnBtnFind() 
{
	// TODO: Add your control notification handler code here
	Ms_Ipc_Date startTime = {0}, stopTime = {0};
	UpdateData(TRUE);
	SYSTEMTIME systemtime = {0}; 
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START_DATE))->GetTime(&systemtime);
	startTime.nYear = systemtime.wYear;
	startTime.nMonth = systemtime.wMonth;
	startTime.nDay = systemtime.wDay;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START_TIME))->GetTime(&systemtime);
	startTime.nHour = systemtime.wHour;
	startTime.nMinute = systemtime.wMinute;
	startTime.nSecond = systemtime.wSecond;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_STOP_DATE))->GetTime(&systemtime);
	stopTime.nYear = systemtime.wYear;
	stopTime.nMonth = systemtime.wMonth;
	stopTime.nDay = systemtime.wDay;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_STOP_TIME))->GetTime(&systemtime);
	stopTime.nHour = systemtime.wHour;
	stopTime.nMinute = systemtime.wMinute;
	stopTime.nSecond = systemtime.wSecond;	
	//startTime.nYear = 2012,startTime.nMonth = 12, startTime.nDay = 1;
	//stopTime.nYear = 2013,stopTime.nMonth = 5, stopTime.nDay = 23;
	Ms_Ipc_FileInfo fileInfo = {0};
	Ms_IpcClient_FindFiles(m_nUserId, m_sDiskPath, IPC_FILE_TYPE_ALL, &startTime, &stopTime, &fileInfo);
	LoadVideoFile(&fileInfo);
	Ms_IpcClient_DestroyFindFiles(&fileInfo);
}

enum LIST_COLUMN
{
	COLUMN_FILE_NAME = 0,
	COLUMN_FILE_DATE,
	COLUMN_FILE_DURATION,
};

BOOL CDlgPlayBack::InitListCtrl()
{
	DWORD dwStyle = m_listFile.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	m_listFile.SetExtendedStyle(dwStyle);


	m_listFile.InsertColumn(COLUMN_FILE_NAME, "Name", LVCFMT_LEFT, 200);
	m_listFile.InsertColumn(COLUMN_FILE_DATE, "StartTime", LVCFMT_LEFT, 140);
	m_listFile.InsertColumn(COLUMN_FILE_DURATION, "Duration(s)", LVCFMT_LEFT, 100);

	return TRUE;
}

BOOL CDlgPlayBack::LoadVideoFile(Ms_Ipc_FileInfo *pFileInfo)
{
	char sDate[32] = {0}, sDuration[32] = {0};
	DeleteAllItem();
	Ms_Ipc_FileInfo *pNext = pFileInfo;
	while (pNext)
	{
		if(pNext->sFilePath[0] != '\0'){
			int nItem = m_listFile.InsertItem(0, pNext->sFilePath, 0);
			_snprintf(sDate, sizeof(sDate)-1, "%d-%d-%d %02d:%02d:%02d", pNext->dtStartTime.nYear, pNext->dtStartTime.nMonth, 
				pNext->dtStartTime.nDay, pNext->dtStartTime.nHour, pNext->dtStartTime.nMinute, pNext->dtStartTime.nSecond);
			_snprintf(sDuration, sizeof(sDuration)-1, "%d", pNext->dwFileDuration);
			m_listFile.SetItemText(nItem, COLUMN_FILE_DATE, sDate);
			m_listFile.SetItemText(nItem, COLUMN_FILE_DURATION, sDuration);
		}
		pNext = pNext->pNext;
	}
	
	return TRUE;
}

void CDlgPlayBack::DeleteAllItem()
{
	m_listFile.DeleteAllItems();
}

void CDlgPlayBack::OnDblclkListFile(NMHDR* pNMHDR, LRESULT* pResult) 
{
	char sFilePath[MAX_PATH] = {0};
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	if (pNMListView->iSubItem != -1 && pNMListView->iItem != -1){
		m_listFile.GetItemText(pNMListView->iItem, 0, sFilePath, sizeof(sFilePath));
		OutputDebugString(sFilePath);
		PlayFile(sFilePath);
	}	
	*pResult = 0;
}

BOOL CDlgPlayBack::PlayFile(char *sFilePath)
{
	KillTimer(TIMER_PLAY);
	Ms_IpcClient_PlayBack(m_nUserId, sFilePath, m_wndPlay.m_hWnd);
	memcpy(m_sVideoFile, sFilePath, MAX_PATH);
	SetTimer(TIMER_PLAY, 1000, NULL);

	int nDuration = Ms_IpcClient_GetFileDuration(m_nUserId);
	m_slider.SetRange(0, nDuration, TRUE);
	m_slider.SetPos(0);
	m_slider.EnableWindow(TRUE);
	return TRUE;
}

void CDlgPlayBack::OnTimer(UINT nIDEvent) 
{
	char sTimerTips[256] = {0};
	if(nIDEvent == TIMER_PLAY){
		//m_slider.SetPos();
		int nCurSeconds = Ms_IpcClient_GetCurrentTime(m_nUserId);
		int nDuration = Ms_IpcClient_GetFileDuration(m_nUserId);
		m_slider.SetPos(nCurSeconds);
	
		_snprintf(sTimerTips, sizeof(sTimerTips)-1, "%02d:%02d:%02d/%02d:%02d:%02d", 
			nCurSeconds/3600, (nCurSeconds%3600)/60, nCurSeconds%60, nDuration/3600, (nDuration%3600)/60, nDuration%60);
		m_wndTime.SetWindowText(sTimerTips);
		
		LONG nIsFinished = Ms_IpcClient_IsPlayFinished(m_nUserId);
		if(nIsFinished == 1){
			m_slider.SetPos(0);
			CDialog::OnTimer(nIDEvent);
			return;
		}
	}	
	CDialog::OnTimer(nIDEvent);
}

void CDlgPlayBack::OnClickSlider(NMHDR* pNMHDR, LRESULT* pResult)
{
	int nSecondPos = m_slider.GetPos();
	Ms_IpcClient_SeekPlayTime(m_nUserId, nSecondPos);
}
