// DlgRTSPViewer.cpp : 구현 파일입니다.
//

#include "stdafx.h"
#include "NZSDK_Sample.h"
#include "DlgRTSPViewer.h"


// CDlgRTSPViewer 대화 상자입니다.

#pragma comment(lib, "NZLiveDisplay.lib")

IMPLEMENT_DYNAMIC(CDlgRTSPViewer, CDialog)

CDlgRTSPViewer::CDlgRTSPViewer(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgRTSPViewer::IDD, pParent)
{
	m_running = FALSE;
	m_display = NULL;
}

CDlgRTSPViewer::~CDlgRTSPViewer()
{
	if( m_display )
	{
		NZLD_Command( m_display, NZLD_CMD_CLOSE_VIDEO, NULL, NULL, NULL, NULL );
		NZLD_Delete( m_display );
		m_display = NULL;
	}
}

BOOL CDlgRTSPViewer::Run( LPCTSTR addr, LPCTSTR port, LPCTSTR url, LPCTSTR id, LPCTSTR pw )
{
	m_addr = addr;
	m_port = port;
	m_url = url;
	m_id = id;
	m_pw = pw;

	m_rtsp_full_url.Format( _T("rtsp://%s:%s@%s:%s/%s"), m_id, m_pw, m_addr, m_port, m_url  );
	GetDlgItem(IDC_STATIC_RTSP_URL)->SetWindowText(m_rtsp_full_url);

	m_running = TRUE;
	return TRUE;
}

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


BEGIN_MESSAGE_MAP(CDlgRTSPViewer, CDialog)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_RBUTTONDBLCLK()
	ON_BN_CLICKED(IDC_BUTTON_RUN, OnBnClickedBtnRun)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnBnClickedBtnStop)
END_MESSAGE_MAP()


// CDlgRTSPViewer 메시지 처리기입니다.
BOOL CDlgRTSPViewer::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  여기에 추가 초기화 작업을 추가합니다.

	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow( FALSE );

	// Create Live Display instance.
	m_display = NZLD_Create( );
	if( NULL == m_display  )
	{
		TRACE( _T("NZLD_Create failed..\r\n") );
		return FALSE;
	}

	// Open Live Display with HWND parent.
	NZLD_Command( m_display, NZLD_CMD_OPEN, (DWORD_PTR)GetDlgItem(IDC_STATIC_DISPLAY)->m_hWnd, NULL, NULL, NULL );

	// Disable Window.
	NZLD_Command( m_display, NZLD_CMD_ENABLE, (DWORD_PTR)FALSE, NULL, NULL, NULL );

	TCHAR path[512] = { 0, };
	int length = ::GetModuleFileName(0, path, sizeof(path));
	if (length > 0)
	{
		char drive[MAX_PATH] = { 0, };
		char dir[MAX_PATH] = { 0, };
		char file[MAX_PATH] = { 0, };
		char ext[MAX_PATH] = { 0, };
		_splitpath(path, drive, dir, file, ext);
		CString logo;
		logo.Format(_T("%s\\%s\\logo.jpg"), drive, dir);	// place your a vendor log image to the install path.
		NZLD_Command(m_display, NZLD_CMD_SET_LOGO_IMAGE, (DWORD_PTR)(LPCSTR)logo, NULL, NULL, NULL);
	}

	// Set User OSD Font
	//NZLD_Command(m_display, NZLD_CMD_SET_OSD_SETTING, (DWORD_PTR)(LPCSTR)"Arial", 20, 0x0000FF00, 0x00000000);
	NZLD_Command(m_display, NZLD_CMD_SET_OSD_SETTING, (DWORD_PTR)(LPCSTR)"Arial", 15, 0x00FFFFFF, 0x00000000);

	// Set User OSD
	NZLD_Command(m_display, NZLD_CMD_SET_OSD_TEXT, (DWORD_PTR)(LPCSTR)"OSD - 1", 0, NULL, NULL);
	//NZLD_Command(m_display, NZLD_CMD_SET_OSD_TEXT, (DWORD_PTR)(LPCSTR)"OSD - 2", 1, NULL, NULL);
	//NZLD_Command(m_display, NZLD_CMD_SET_OSD_TEXT, (DWORD_PTR)(LPCSTR)"OSD - 3", 2, NULL, NULL);
	//NZLD_Command(m_display, NZLD_CMD_SET_OSD_TEXT, (DWORD_PTR)(LPCSTR)"OSD - 4", 3, NULL, NULL);

	// Enable/Disable PTZ screen
	NZLD_Command(m_display, NZLD_CMD_ENABLE_SCREEN_PTZ, 1, 0, NULL, NULL);
	//NZLD_Command(m_display, NZLD_CMD_ENABLE_SCREEN_PTZ, 0, 0, NULL, NULL);

	return TRUE;  // return TRUE unless you set the focus to a control
}

void CDlgRTSPViewer::OnLButtonDown(UINT nFlags, CPoint point)
{
	TRACE( _T("OnLButtonDown\r\n") );
}

void CDlgRTSPViewer::OnLButtonUp(UINT nFlags, CPoint point)
{
	TRACE( _T("OnLButtonUp\r\n") );
}

void CDlgRTSPViewer::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	TRACE( _T("OnLButtonDblClk\r\n") );
}

void CDlgRTSPViewer::OnRButtonDown(UINT nFlags, CPoint point)
{
	TRACE( _T("OnRButtonDown\r\n") );
}

void CDlgRTSPViewer::OnRButtonUp(UINT nFlags, CPoint point)
{
	TRACE( _T("OnRButtonUp\r\n") );
}

void CDlgRTSPViewer::OnRButtonDblClk(UINT nFlags, CPoint point)
{
	TRACE( _T("OnRButtonDblClk\r\n") );
}

void CDlgRTSPViewer::OnBnClickedBtnRun()
{
	// Enable Window
	NZLD_Command(m_display, NZLD_CMD_ENABLE, (DWORD_PTR)TRUE, NULL, NULL, NULL);

	// Display the 'connecting' osd until video is connected.
	NZLD_Command(m_display, NZLD_CMD_CONNECTING, NULL, NULL, NULL, NULL);

	// Connect video
	NZLD_DEV_INFO dev = { 0, };	// must be filled with '0'
	strncpy_s( dev.addr, sizeof(dev.addr), m_addr, m_addr.GetLength() );
	strncpy_s( dev.url, sizeof(dev.url), m_url, m_url.GetLength() );
	strncpy_s( dev.id, sizeof(dev.id), m_id, m_id.GetLength() );
	strncpy_s( dev.pw, sizeof(dev.pw), m_pw, m_pw.GetLength() );
	dev.port = atoi( m_port );
	if (!NZLD_Command(m_display, NZLD_CMD_OPEN_VIDEO, (DWORD_PTR)(&dev), NULL, NULL, NULL))
		TRACE( _T("NZLD_CMD_OPEN_VIDEO failed..\r\n") );

	GetDlgItem(IDC_BUTTON_RUN)->EnableWindow( FALSE );
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow( TRUE );
}

void CDlgRTSPViewer::OnBnClickedBtnStop()
{
	// Disconnect Video
	NZLD_Command(m_display, NZLD_CMD_CLOSE_VIDEO, NULL, NULL, NULL, NULL);

	// Disable Window
	NZLD_Command(m_display, NZLD_CMD_ENABLE, (DWORD_PTR)FALSE, NULL, NULL, NULL);

	GetDlgItem(IDC_BUTTON_RUN)->EnableWindow( TRUE );
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow( FALSE );
}

void CDlgRTSPViewer::OnCancel()
{
	// TODO: 여기에 특수화된 코드를 추가 및/또는 기본 클래스를 호출합니다.

	OnBnClickedBtnStop( );
	Sleep( 100 );
	m_running = FALSE;
	CDialog::OnCancel();
}
