using NetSDKCS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BeltScaleSystem
{
    public partial class Form1 : Form
    {
        IntPtr _LoginID = IntPtr.Zero;
        NET_DEVICEINFO_Ex _DeviceInfo = new NET_DEVICEINFO_Ex();
        IntPtr _PlayID = IntPtr.Zero;
        IntPtr _AttachID = IntPtr.Zero;
        fDisConnectCallBack _DisConnectCallBack;
        fHaveReConnectCallBack _ReConnectCallBack;
        public Form1()
        {
            InitializeComponent();
            try
            {
                _DisConnectCallBack = new fDisConnectCallBack(DisConnectCallBack);
                _ReConnectCallBack = new fHaveReConnectCallBack(ReConnectCallBack);
                NETClient.Init(_DisConnectCallBack, IntPtr.Zero, null);
                NETClient.SetAutoReconnect(_ReConnectCallBack, IntPtr.Zero);
                //   _AnalyzerDataCallBack = new fAnalyzerDataCallBack(AnalyzerDataCallBack);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //    Process.GetCurrentProcess().Kill();
                return;
            }
        }

        void DisConnectCallBack(IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
        {
            this.BeginInvoke(new Action(() =>
            {
                this.Text = "FaceOpenDoorDemo(人脸开门Demo)---offline(离线)";
            }));
        }

        void ReConnectCallBack(IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
        {
            this.BeginInvoke(new Action(() =>
            {
                this.Text = "FaceOpenDoorDemo(人脸开门Demo)---online(在线)";
            }));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (IntPtr.Zero == _LoginID)
            {
                ushort port = 0;
                try
                {
                    port = Convert.ToUInt16(37777);
                }
                catch
                {
                    MessageBox.Show("The port value must be 1 -65535(端口值必须是1-65535)");
                    return;
                }
                _LoginID = NETClient.LoginWithHighLevelSecurity("192.168.184.202", port, "admin", "admin12345", EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref _DeviceInfo);
                if (IntPtr.Zero == _LoginID)
                {
                    MessageBox.Show(NETClient.GetLastError());
                    return;
                }
                for (int i = 0; i < _DeviceInfo.nChanNum; i++)
                {
                    this.comboBox_channel.Items.Add(i + 1);
                }
                //某些型号的设备，比如“BSC”门禁主机，是没有通道的，也不支持监视和订阅
                if (this.comboBox_channel.Items.Count > 0)
                {
                    this.comboBox_channel.SelectedIndex = 0;
                    this.comboBox_channel.Enabled = true;
                }
            }
            else
            {
                NETClient.Logout(_LoginID);
                _LoginID = IntPtr.Zero;

                _AttachID = IntPtr.Zero;

                _PlayID = IntPtr.Zero;
                this.pictureBox1.Refresh();


            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (IntPtr.Zero == _PlayID)
            {
                _PlayID = NETClient.RealPlay(_LoginID, comboBox_channel.SelectedIndex, this.pictureBox1.Handle);
                if (IntPtr.Zero == _PlayID)
                {
                    MessageBox.Show(NETClient.GetLastError());
                    return;
                }
                bool ret = NETClient.RenderPrivateData(_PlayID, true);
                if (!ret)
                {
                    MessageBox.Show(NETClient.GetLastError());
                    return;
                }
            }
            else
            {
                NETClient.RenderPrivateData(_PlayID, false);
                NETClient.StopRealPlay(_PlayID);
                _PlayID = IntPtr.Zero;
                this.pictureBox1.Refresh();
            }
        }
    }
}
