using NVRCsharpDemo;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainVideoDataDispose
{
    public partial class text : Form
    {
        public int m_lUserID = -1;
        public int m_lGetFaceParamCfgHandle = -1;

        private CHCNetSDK.REALDATACALLBACK RealData = null;
        public CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo;
        public CHCNetSDK.NET_DVR_DEVICEINFO_V40 tDeviceInfo;
        public CHCNetSDK.NET_DVR_IPPARACFG_V40 m_struIpParaCfgV40;
        public CHCNetSDK.NET_DVR_STREAM_MODE m_struStreamMode;
        public CHCNetSDK.NET_DVR_IPCHANINFO m_struChanInfo;
        public CHCNetSDK.NET_DVR_PU_STREAM_URL m_struStreamURL;
        public CHCNetSDK.NET_DVR_IPCHANINFO_V40 m_struChanInfoV40; 
        public CHCNetSDK.NET_DVR_USER_LOGIN_INFO pLoginInfo;
        public CHCNetSDK.LOGINRESULTCALLBACK LoginCallBack; 
        public CHCNetSDK.RemoteConfigCallback remoteConfigCallback; 
        //private PlayCtrl.DECCBFUN m_fDisplayFun = null;
        public delegate void MyDebugInfo(string str);

        private Label labelLogin;
        public text()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool m_bInitSDK = CHCNetSDK.NET_DVR_Init();
            string DVRIPAddress = "10.111.67.23"; //设备IP地址或者域名 Device IP
            Int16 DVRPortNumber = Int16.Parse("8000");//设备服务端口号 Device Port
            string DVRUserName = "admin";//设备登录用户名 User name to login
            string DVRPassword = "Admin12345";//设备登录密码 Password to login

            //设备IP地址或者域名
            byte[] byIP = System.Text.Encoding.Default.GetBytes(DVRIPAddress);
            pLoginInfo.sDeviceAddress = new byte[129];
            byIP.CopyTo(pLoginInfo.sDeviceAddress, 0);

            //设备用户名
            byte[] byUserName = System.Text.Encoding.Default.GetBytes(DVRUserName);
            pLoginInfo.sUserName = new byte[64];
            byUserName.CopyTo(pLoginInfo.sUserName, 0);

            //设备密码
            byte[] byPassword = System.Text.Encoding.Default.GetBytes(DVRPassword);
            pLoginInfo.sPassword = new byte[64];
            byPassword.CopyTo(pLoginInfo.sPassword, 0);

            pLoginInfo.wPort = 8000;

            if (LoginCallBack == null)
            {
                LoginCallBack = new CHCNetSDK.LOGINRESULTCALLBACK(cbLoginCallBack);//注册回调函数                    
            }
            pLoginInfo.cbLoginResult = LoginCallBack;
            pLoginInfo.bUseAsynLogin = false;

            //登录设备 Login the device
            //m_lUserID = CHCNetSDK.NET_DVR_Login_V40(ref pLoginInfo, ref tDeviceInfo);
            m_lUserID = CHCNetSDK.NET_DVR_Login_V40(ref pLoginInfo, ref tDeviceInfo);
            if (m_lUserID < 0)
            {
                uint iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                string str = "NET_DVR_Login_V30 failed, error code= " + iLastErr; //登录失败，输出错误号 Failed to login and output the error code
                textinfo.Text = str;
                return;
            }
            else
            {
                //登录成功
                //停止远程配置
                if (m_lGetFaceParamCfgHandle != -1)
                {
                    CHCNetSDK.NET_DVR_StopRemoteConfig(m_lGetFaceParamCfgHandle);
                }

                //清除表格信息
                //listViewFaceConfig.Items.Clear();
                //LpArr.Clear();

                ////定义查询信息对象
                //CHCNetSDK.NET_DVR_FACE_PARAM_COND struCond = new CHCNetSDK.NET_DVR_FACE_PARAM_COND();
                //struCond.Init();
                //struCond.dwSize = (uint)Marshal.SizeOf(struCond);
                //struCond.dwFaceNum = (uint)IndexOfListview;
                //struCond.byFaceID = Convert.ToByte(textBoxFaceID.Text.ToString());
                //StrToByteArray(ref struCond.byCardNo, textBoxAssociatedCardNo.Text);
                //GetCardReaderIndex(ref struCond.byEnableCardReader);

                //int dwSize = Marshal.SizeOf(struCond);
                //IntPtr ptrStruCond = Marshal.AllocHGlobal(dwSize);
                //Marshal.StructureToPtr(struCond, ptrStruCond, true);
                //g_fGetFaceParamCallback = new CHCNetSDK.RemoteConfigCallback(ProcessGetFaceParam);
                //m_lGetFaceParamCfgHandle = CHCNetSDK.NET_DVR_StartRemoteConfig(m_lServerID, CHCNetSDK.NET_DVR_GET_FACE_PARAM_CFG, ptrStruCond, dwSize, g_fGetFaceParamCallback, this.Handle);

                //Marshal.FreeHGlobal(ptrStruCond);
            }

            
            return;

        }

        public void cbLoginCallBack(int lUserID, int dwResult, IntPtr lpDeviceInfo, IntPtr pUser)
        {
            string strLoginCallBack = "登录设备，lUserID：" + lUserID + "，dwResult：" + dwResult;

            if (dwResult == 0)
            {
                uint iErrCode = CHCNetSDK.NET_DVR_GetLastError();
                strLoginCallBack = strLoginCallBack + "，错误号:" + iErrCode;
            }

            //下面代码注释掉也会崩溃
            if (InvokeRequired)
            {
                object[] paras = new object[2];
                paras[0] = strLoginCallBack;
                paras[1] = lpDeviceInfo;
                
            }
            else
            {
                //创建该控件的主线程直接更新信息列表 
               
            }

        }
    }
}
