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 AddGroup : Form
    {
        public NetSDK.NET_SDK_USER_MANAGE_INFO m_userlist;
        public int m_lUserID = 0;
        public AddGroup()
        {
            InitializeComponent();
        }
        public void Init()
        {
            textBoxGroup.Text = "";
            textBoxMemo.Text = "";
            listView1.Items.Clear();
            for (int i = 0; i < m_userlist.rigthNum; i++)
            {
                ListViewItem rightitem = new ListViewItem(new string[] { (i + 1).ToString(), new string(m_userlist.rightList[i].name) });
                rightitem.Checked = true;
                listView1.Items.Add(rightitem);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NetSDK.NET_SDK_USER_GROUP_INFO groupinfo = new NetSDK.NET_SDK_USER_GROUP_INFO();
             if(textBoxGroup.Text=="")
            {
                MessageBox.Show("Group name cannot be empty");
                return;
            }
             groupinfo.name = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
             textBoxGroup.Text.ToCharArray().CopyTo(groupinfo.name, 0);
             groupinfo.memo = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
             textBoxMemo.Text.ToCharArray().CopyTo(groupinfo.memo, 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++;
                 }
             }
             groupinfo.rigthNum = nIndex;
             groupinfo.rights = strRight;
             IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_GROUP_INFO)));
             Marshal.StructureToPtr(groupinfo, pt, false);
             int nRet = NetSDK.NET_SDK_SetDevConfig(m_lUserID, (uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_ADD_GROUP, -1, pt, Marshal.SizeOf(typeof(NetSDK.NET_SDK_USER_GROUP_INFO)), 2000);
             if (nRet > 0)
             {
                 MessageBox.Show("Added successfully");
                 Close();
             }
             else
             {
                 MessageBox.Show("add failed");
             }
        }
    }
}
