// Smart_SmartDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Smart_Smart.h"
#include "Smart_SmartDlg.h"
#include "DlgIntelligentSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CSmart_SmartDlg dialog

static void WINAPI s_messagecallback(LONG hHandle,int wParam,int lParam,void *context)
{
	CSmart_SmartDlg *pWnd = (CSmart_SmartDlg*)context;
	if(wParam == LAUMSG_LINKMSG)
	{
		pWnd->LinkProcess(lParam);
	}
	else
	{
		pWnd->AlarmProcess(wParam, lParam);
	}
}

CSmart_SmartDlg::CSmart_SmartDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSmart_SmartDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSmart_SmartDlg)
	m_sername = _T("8301V");
	m_url = _T("192.168.1.193");
	m_port = 3000;
	m_psw = _T("888888");
	m_user = _T("888888");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hchann = -1;
}

void CSmart_SmartDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSmart_SmartDlg)
	DDX_Control(pDX, IDC_STATIC_VIDEO, m_videownd);
	DDX_Control(pDX, IDC_LIST1, m_alarmlist);
	DDX_Text(pDX, IDC_EDIT_NAME, m_sername);
	DDX_Text(pDX, IDC_EDIT_IP, m_url);
	DDX_Text(pDX, IDC_EDIT_PORT, m_port);
	DDX_Text(pDX, IDC_EDIT_PSW, m_psw);
	DDX_Text(pDX, IDC_EDIT_USER, m_user);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSmart_SmartDlg, CDialog)
	//{{AFX_MSG_MAP(CSmart_SmartDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SET, OnBtnSet)
	ON_BN_CLICKED(IDC_BTN_PLAY, OnBtnPlay)
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSmart_SmartDlg message handlers

BOOL CSmart_SmartDlg::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
	m_alarmlist.InsertColumn(0, "时间", LVCFMT_LEFT, 150);
	m_alarmlist.InsertColumn(1, "描述", LVCFMT_LEFT, 366);
	m_alarmlist.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSmart_SmartDlg::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 CSmart_SmartDlg::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 CSmart_SmartDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSmart_SmartDlg::OnBtnSet() 
{
	// TODO: Add your control notification handler code here
	if (!CheckSerInfo())
		return;
	CDlgSmartSetup dlg;
	dlg.SetCameraLinkParam(m_sername, m_url, 0, m_port, m_user, m_psw);
	dlg.DoModal();
}

void CSmart_SmartDlg::OnBtnPlay() 
{
	// TODO: Add your control notification handler code here
	if (!CheckSerInfo())
		return;
	if (m_hchann <= 0)
	{
		CHANNEL_CLIENTINFO m_chinfo = {0};
		m_chinfo.m_buffnum    = 20;
		m_chinfo.m_ch         = 0;
		m_chinfo.m_hChMsgWnd  = NULL;
		m_chinfo.m_hVideohWnd = m_videownd.m_hWnd;
		m_chinfo.m_nChmsgid   = 0;
		m_chinfo.m_password   = (char*)m_psw.operator LPCTSTR();
		m_chinfo.m_playstart  = TRUE;
		m_chinfo.m_sername    = (char*)m_sername.operator LPCTSTR(); 
		m_chinfo.m_tranType   = 3;
		m_chinfo.m_useoverlay = FALSE;
		m_chinfo.m_username   = (char*)m_user.operator LPCTSTR();
		m_chinfo.nColorKey    = RGB(255,0,255);
		m_chinfo.context      = this;
		m_chinfo.m_messagecallback = s_messagecallback;
		m_hchann = VSNET_ClientStart((char*)m_url.operator LPCTSTR(),&m_chinfo,m_port, 0);
	}
	else
	{

	}
	GetDlgItem(IDC_BTN_PLAY)->EnableWindow(m_hchann <= 0);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(m_hchann > 0);
	GetDlgItem(IDC_BTN_SET)->EnableWindow(m_hchann > 0);
}

void CSmart_SmartDlg::OnBtnStop() 
{
	// TODO: Add your control notification handler code here
	if (m_hchann > 0)
	{
		VSNET_ClientStop(m_hchann);
		m_hchann = -1;
	}
	GetDlgItem(IDC_BTN_PLAY)->EnableWindow(m_hchann <= 0);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(m_hchann > 0);
	GetDlgItem(IDC_BTN_SET)->EnableWindow(m_hchann > 0);
}

BOOL CSmart_SmartDlg::CheckSerInfo()
{
	if (!UpdateData(TRUE))
		return FALSE;
	if (m_url.IsEmpty())
	{
		AfxMessageBox("请输入IP地址！");
		return FALSE;
	}
	if (m_port == 0)
	{
		AfxMessageBox("请输入端口！");
		return FALSE;
	}
	if (m_user.IsEmpty())
	{
		AfxMessageBox("请输入用户名!");
		return FALSE;
	}
	if (m_user.IsEmpty())
	{
		AfxMessageBox("请输入密码!");
		return FALSE;
	}
	return TRUE;
}

void CSmart_SmartDlg::LinkProcess(int lParam)
{
	if (lParam == 0)
	{
		TRACE("LinkProcess: 成功.\n");
	}
	else
	{
		switch (lParam)
		{
		case 1:
			TRACE("LinkProcess: 用户停止预览.\n");
			break;
		case 2:
			TRACE("LinkProcess: 与服务器的连接失败.\n");
			break;
		case 3:
			TRACE("LinkProcess: 服务器断开.\n");
			break;
		case 4:
			TRACE("LinkProcess: 绑定端口失败；在用多播传输输入时，如果多个服务器的多播端口相同，会出现该错误.\n");
			break;
		case 6:
			TRACE("LinkProcess: 连接域名服务器失败.\n");
			break;
		case -102:
			TRACE("LinkProcess: 用户名密码错误.\n");
			break;
		case -103:
			TRACE("LinkProcess: 系统用户满员，每个服务器最大用户数为40.\n");
			break;
		case -105:
			TRACE("LinkProcess: 通道用户满员，用TCP或UDP方式每个通道最多可以连接10个用户,整个服务器可以连接40个用户，对于多播方式连接，用户数量不受限则.\n");
			break;
		case -106:
			TRACE("LinkProcess: 服务器没有指定的通道.\n");
			break;
		default:
			TRACE("LinkProcess: 未知错误.\n");
		}
	}
}

void CSmart_SmartDlg::AlarmProcess(int wParam, int lParam)
{

#if 0
	if (wParam != LAUMSG_BLACKREPORT)
	{
		TRACE("其他报警\n");
	}
	else
	{
		CString str;
		CTime loctime = CTime::GetCurrentTime();
	}
#endif

	CString str;
	CTime loctime = CTime::GetCurrentTime();
	int nItem = m_alarmlist.InsertItem(m_alarmlist.GetItemCount(), loctime.Format("%Y-%m-%d %H:%M:%S"));
	if (wParam == LAUMSG_VAALARM_SINGLELINE)
	{
		str.Format("警戒线跨越");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_VAALARM_DOUBLELINE)
	{
		str.Format("多线跨越");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_VAALARM_REGIONENTER)
	{
		str.Format("区域入侵");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_VAALARM_REGIONLEFT)
	{
		str.Format("区域离开");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_VAALARM_REGIONWANDER)
	{
		str.Format("区域徘徊");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_BLACKREPORT)
	{
		str.Format("黑度分析");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_VIDEOMOTION)
	{
		str.Format("移动侦测报警");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_VIDEOLOST)
	{
		str.Format("视频丢失报警");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_ALARM)
	{
		str.Format("探头报警");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else if (wParam == LAUMSG_HIDEALARM)
	{
		str.Format("视频遮挡报警");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
	else
	{
		str.Format("其他报警");
		m_alarmlist.SetItemText(nItem, 1, str);
	}
}

void CSmart_SmartDlg::OnDestroy() 
{
	if (m_hchann > 0)
	{
		VSNET_ClientStop(m_hchann);
		m_hchann = -1;
	}
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}
