// playtestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "playtest.h"
#include "playtestDlg.h"
#include "DlgNetFile.h"

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

#define PLAYTIME_TIMER	1

#define PLAYEVENT_MSG		(WM_USER + 200)
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlaytestDlg dialog

CPlaytestDlg::CPlaytestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlaytestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlaytestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hPlay = -1;
	m_playtimer  = 0;
	m_bsliderpos = FALSE;
}

void CPlaytestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlaytestDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPlaytestDlg, CDialog)
	//{{AFX_MSG_MAP(CPlaytestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_PLAY, OnBtnPlay)
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTN_OPENFILE, OnBtnOpenfile)
	ON_BN_CLICKED(IDC_BTN_PAUS, OnBtnPaus)
	ON_WM_TIMER()
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BTN_STEP, OnBtnStep)
	ON_BN_CLICKED(IDC_BTN_FAST, OnBtnFast)
	ON_BN_CLICKED(IDC_BTN_SLOW, OnBtnSlow)
	ON_BN_CLICKED(IDC_NETFILE, OnNetfile)
	ON_BN_CLICKED(IDC_CHECK_AUDIO, OnCheckAudio)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_MESSAGE(PLAYEVENT_MSG,   OnPlayEvent)
	ON_BN_CLICKED(IDC_CHECK_CUTFILE, OnCheckCutfile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlaytestDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	LCPLAYM4_InitPlayer("qwert");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPlaytestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPlaytestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPlaytestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPlaytestDlg::OnBtnOpenfile() 
{	
	char szFilter[]="MPEG4|*.mp4||";
	CFileDialog dlg(TRUE,"*.*","",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		szFilter);
	if(dlg.DoModal()==IDOK)
	{
        DWORD firstGMTTime = 0,filetime;
		char filename[MAX_PATH];
        CString lpstr;
		GetDlgItem(IDC_EDIT_FILE)->SetWindowText(dlg.GetPathName());
		GetDlgItem(IDC_BTN_PLAY)->EnableWindow(TRUE);
		OnBtnStop();
		GetDlgItem(IDC_EDIT_FILE)->GetWindowText(filename,sizeof(filename));
		m_hPlay = LCPLAYM4_OpenFile(filename, FALSE);
		if(m_hPlay == -1) 
		{
			AfxMessageBox("open file failed!\n");
			return;
		}
		LCPLAYM4_RegEventMsgWnd(m_hPlay,m_hWnd,PLAYEVENT_MSG);
        filetime = LCPLAYM4_GetFileTime(m_hPlay,&firstGMTTime);
        if(firstGMTTime > 0)
        {
            struct tm *localt;
            localt = localtime((time_t*)&firstGMTTime);
            lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",localt->tm_year + 1900,localt->tm_mon + 1,
                    localt->tm_mday,localt->tm_hour,localt->tm_min,localt->tm_sec);
    		GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
            firstGMTTime = firstGMTTime + (filetime / 1000);
            localt = localtime((time_t*)&firstGMTTime);
            lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",localt->tm_year + 1900,localt->tm_mon + 1,
                    localt->tm_mday,localt->tm_hour,localt->tm_min,localt->tm_sec);
    		GetDlgItem(IDC_FILETIME)->SetWindowText(lpstr);
        }
        else
        {
            struct tm *localt;
            lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",0,0,0,0,0,0);
    		GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
            firstGMTTime = (filetime / 1000);
            localt = gmtime((time_t*)&firstGMTTime);
            lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",00,00,00,localt->tm_hour,localt->tm_min,localt->tm_sec);
    		GetDlgItem(IDC_FILETIME)->SetWindowText(lpstr);
        }
		OnBtnPlay();
	}
}

void CPlaytestDlg::OnBtnPlay()
{
	DWORD playtime;
	if(m_hPlay == -1) 
	{
		return;
	}
	RECT rect;
	rect.left = rect.right = rect.top = rect.bottom = 10;

	playtime = LCPLAYM4_GetFileTime(m_hPlay) / 1000;
	
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetLineSize(1);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPageSize(1);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetRange(0,playtime);

	KILLTIMER(m_playtimer);
	m_playtimer = SetTimer(PLAYTIME_TIMER,1000,0);
	LCPLAYM4_Play(m_hPlay,GetDlgItem(IDC_STATIC_WND)->m_hWnd);
	LCPLAYM4_RefreshDisplay(m_hPlay,&rect);
	GetDlgItem(IDC_BTN_PLAY)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_PAUS)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STEP)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_FAST)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_SLOW)->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_AUDIO)->EnableWindow(LCPLAYM4_HaveAudio(m_hPlay));

	GetDlgItem(IDC_SLIDER_POS)->EnableWindow(TRUE);
	
	playtime = LCPLAYM4_GetPlayedTime(m_hPlay) / 1000;
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(playtime);

}

void CPlaytestDlg::OnBtnStop()
{
	char playtime[20]; 
	KILLTIMER(m_playtimer);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(0);
    sprintf(playtime,"%04d:%02d:%02d %02d:%02d:%02d",0,0,0,0,0,0);
    GetDlgItem(IDC_PLAYTIME)->SetWindowText(playtime);
	if(m_hPlay != -1)
	{
		LCPLAYM4_Stop(m_hPlay);
		m_hPlay = -1;
	}
	GetDlgItem(IDC_BTN_PLAY)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_PAUS)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_STEP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_FAST)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_SLOW)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_AUDIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_SLIDER_POS)->EnableWindow(FALSE);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(0);
	((CButton*)GetDlgItem(IDC_CHECK_AUDIO))->SetCheck(FALSE);
}

void CPlaytestDlg::OnDestroy() 
{
	OnBtnStop();
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
}

void CPlaytestDlg::OnBtnPaus() 
{
	LCPLAYM4_Pause(m_hPlay);
}

void CPlaytestDlg::OnTimer(UINT nIDEvent)
{	
	if(nIDEvent == PLAYTIME_TIMER)
	{
		if(!m_bsliderpos)
		{
            time_t gmttime;
            struct tm *localt;
            CString lpstr;
            DWORD timecc = LCPLAYM4_GetPlayedTime(m_hPlay),timeset;
            if((gmttime = LCPLAYM4_GetPlayedGMTTime(m_hPlay)) != 0)
            {
                localt = localtime((time_t*)&gmttime);
                lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",localt->tm_year + 1900,localt->tm_mon + 1,
                    localt->tm_mday,localt->tm_hour,localt->tm_min,localt->tm_sec);
                GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
            }
            else
            {
                timeset = (timecc / 1000);
                localt = gmtime((time_t*)&timeset);
                lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",00,00,00,localt->tm_hour,localt->tm_min,localt->tm_sec);
                GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
            }
            ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(timecc / 1000);
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CPlaytestDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if(((CScrollBar*)GetDlgItem(IDC_SLIDER_POS)) == pScrollBar)
	{
		if(nSBCode == SB_THUMBTRACK || nSBCode == SB_THUMBPOSITION)
		{
            DWORD firstGMTTime = 0;
            CString lpstr;
			DWORD slidpos;
			slidpos = ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->GetPos();
            LCPLAYM4_GetFileTime(m_hPlay,&firstGMTTime);
            if(firstGMTTime > 0)
            {
                struct tm *localt;
                firstGMTTime += slidpos;
                localt = localtime((time_t*)&firstGMTTime);
                    lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",localt->tm_year + 1900,localt->tm_mon + 1,
                    localt->tm_mday,localt->tm_hour,localt->tm_min,localt->tm_sec);
                GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
            }
            else
            {
                struct tm *localt;
                localt = gmtime((time_t*)&slidpos);
                    lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",00,00,00,localt->tm_hour,localt->tm_min,localt->tm_sec);
                GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
            }
			m_bsliderpos = TRUE;
		}
		else if(nSBCode == SB_ENDSCROLL)
		{
			DWORD slidpos;
			slidpos = ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->GetPos();
			LCPLAYM4_SetPlayedTime(m_hPlay,slidpos * 1000);
			m_bsliderpos = FALSE;
		}
	}
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CPlaytestDlg::OnBtnStep() 
{
	LCPLAYM4_OneByOne(m_hPlay);
}

void CPlaytestDlg::OnBtnFast() 
{
	LCPLAYM4_Fast(m_hPlay);
}

void CPlaytestDlg::OnBtnSlow() 
{
	LCPLAYM4_Slow(m_hPlay);
}

void CPlaytestDlg::OnPlayEvent(WPARAM wParam, LPARAM lParam)
{
    CString lpstr;
	if(wParam == 1) //play end
	{
		if(lParam == (LPARAM)m_hPlay)
		{
			KILLTIMER(m_playtimer);
			((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(0);
            lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",0,0,0,0,0,0);
            GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
		}
	}
}

void CPlaytestDlg::OnNetfile() 
{	
	CDlgNetFile dlg;
	dlg.DoModal();
}

void CPlaytestDlg::OnCheckAudio() 
{
	if(m_hPlay == -1) return;
	if(((CButton*)GetDlgItem(IDC_CHECK_AUDIO))->GetCheck())
	{
		LCPLAYM4_PlaySound(m_hPlay);
	}
	else
	{
		LCPLAYM4_StopSound(m_hPlay);
	}
}

void CPlaytestDlg::OnButton1() 
{
	LCPLAYM4_CaptureBmp(m_hPlay,"C:\\haha.bmp");
}

void CPlaytestDlg::OnCheckCutfile() 
{
	static BOOL m_bOnCutFile = FALSE;
	if(m_bOnCutFile)
	{
		m_bOnCutFile = FALSE;
		((CButton*)GetDlgItem(IDC_CHECK_CUTFILE))->SetCheck(FALSE);
		if(m_hPlay == -1) return;
		LCPLAYM4_SetFileCutEnd(m_hPlay);
	}
	else
	{
		if(m_hPlay == -1) return;
		if(!LCPLAYM4_SetFileCutStart(m_hPlay,"C:\\cut.mp4"))
		{
			AfxMessageBox("start cutting unsuccessfully!");
			return;
		}
		m_bOnCutFile = TRUE;
		((CButton*)GetDlgItem(IDC_CHECK_CUTFILE))->SetCheck(TRUE);
	}
}
