using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TSDome
{
    public partial class AddUser : Form
    {
        public NetSDK.NET_SDK_USER_MANAGE_INFO m_userlist;
        public int m_lUserID = 0;
        public AddUser()
        {
            InitializeComponent();
        }
        public void Init()
        {
            comboBoxGroup.Items.Clear();
            for (int i = 0; i < m_userlist.groupNum; i++)
            {
                comboBoxGroup.Items.Insert(i, new string(m_userlist.groupList[i].name));
            }
            if (comboBoxGroup.Items.Count > 0)
            {
                comboBoxGroup.SelectedIndex = 0;
            }
            textBoxMemo.Text = "";
            textBoxName.Text = "";
            textBoxPassword.Text = "";
            textBoxConfirm.Text = "";
            checkBoxShareable.Checked = false;
            update();
        }
        public void update()
        {
            int select = comboBoxGroup.SelectedIndex;
            if(select<0)
            {
                return;
            }
            listView1.Items.Clear();
            for (int i = 0; i < m_userlist.groupList[select].rigthNum; i++)
            {
                ListViewItem rightitem = new ListViewItem(new string[] { (i + 1).ToString(), new string(m_userlist.groupList[select].rights.Skip(i * NetSDK.SDK_NET_MAX_USER_LENGTH).Take(NetSDK.SDK_NET_MAX_USER_LENGTH).ToArray()) });
                rightitem.Checked = true;
                listView1.Items.Add(rightitem);
            }
        }
        private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            update();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            NetSDK.NET_SDK_USER_INFO userinfo = new NetSDK.NET_SDK_USER_INFO();
            if(textBoxName.Text=="")
            {
                MessageBox.Show("Username can not be empty");
                return;
            }
            else if(textBoxPassword.Text!=textBoxConfirm.Text)
            {
                MessageBox.Show("Inconsistent password");
                return;
            }
            else
            {
                userinfo.name=new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
                textBoxName.Text.ToCharArray().CopyTo(userinfo.name, 0);
                if(checkBoxShareable.Checked)
                {
                    userinfo.shareable = '1';
                }
                else
                {
                    userinfo.shareable = '\0';
                }
                userinfo.memo = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
                textBoxMemo.Text.ToCharArray().CopyTo(userinfo.memo, 0);
                NetSDK.NET_SDK_DEVICEINFO device = new NetSDK.NET_SDK_DEVICEINFO();
                IntPtr pdevice = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_CONF_MODIFY_PSW)));
                uint lpBytesReturned = new uint();
                int nRet = NetSDK.NET_SDK_GetDevConfig(m_lUserID, (uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_SYSINFO, -1, pdevice, (uint)Marshal.SizeOf(typeof(NetSDK.NET_SDK_DEVICEINFO)), ref lpBytesReturned, 2000);
                if (nRet > 0)
                {
                    device = (NetSDK.NET_SDK_DEVICEINFO)Marshal.PtrToStructure(pdevice, typeof(NetSDK.NET_SDK_DEVICEINFO));
                }
                else
                {
                    return;
                }
                NetSDK.NET_SDK_PASSWORDENCRYPT nEncrypt;
                if (device.iDeviceType == (uint)NetSDK.NET_SDK_DEVICE_TYPE.NET_SDK_DEVICE_TYPE_IPC)
                {
                    nEncrypt = NetSDK.NET_SDK_PASSWORDENCRYPT.PASSWORD_PLAIN;
                }
                else
                {
                    nEncrypt = NetSDK.NET_SDK_PASSWORDENCRYPT.PASSWORD_MD5;
                }
                if (device.tmBuildTime.year * 10000 + device.tmBuildTime.month * 100 + device.tmBuildTime.day < 20170802)
                {
                    nEncrypt = NetSDK.NET_SDK_PASSWORDENCRYPT.PASSWORD_MD5;
                }
                userinfo.iEncryptType = (int)nEncrypt;
                userinfo.passWord = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
                textBoxPassword.Text.ToCharArray().CopyTo(userinfo.passWord, 0);
                userinfo.groupname = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
                comboBoxGroup.Text.ToCharArray().CopyTo(userinfo.groupname, 0);
                int nIndex = 0;
                int nSize = listView1.Items.Count;
                char[] strRight = new char[NetSDK.SDK_NET_MAX_RIGTH_NUM * NetSDK.SDK_NET_MAX_USER_LENGTH];
                for (int i = 0; i < nSize; ++i)
                {
                    ListViewItem item = listView1.Items[i];

                    if (item.Checked)
                    {
                        char[] strName = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
                        strName = item.SubItems[1].Text.ToCharArray();
                        strName.CopyTo(strRight, NetSDK.SDK_NET_MAX_USER_LENGTH * nIndex);
                        nIndex++;
                    }
                }
                userinfo.rigthNum = nIndex;
                userinfo.rights = strRight;
                IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_INFO)));
                Marshal.StructureToPtr(userinfo, pt, false);
                nRet = NetSDK.NET_SDK_SetDevConfig(m_lUserID, (uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_ADD_USER, -1, pt, Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_INFO)), 2000);
                if (nRet > 0)
                {
                    MessageBox.Show("Added successfully");
                    Close();
                }
                else
                {
                    MessageBox.Show("Added successfully");
                }
            }
        }
    }
}
