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 ModifyGroup : Form
    {
        public NetSDK.NET_SDK_USER_MANAGE_INFO m_userlist;
        public int m_lUserID = 0;
        public ModifyGroup()
        {
            InitializeComponent();
        }
        public void Init()
        {
            comboBoxGroupName.Items.Clear();
            listView1.Items.Clear();
            for (int i = 0; i < m_userlist.groupNum; i++)
            {
                comboBoxGroupName.Items.Insert(i, new string(m_userlist.groupList[i].name));
                comboBoxGroupName.SelectedIndex = 0;
            }
            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 = false;
                listView1.Items.Add(rightitem);
            }
            update();
        }
        public void update()
        {
            int nselete = comboBoxGroupName.SelectedIndex;
            textBoxMemo.Text = new string(m_userlist.groupList[nselete].memo);
            textBoxGroup.Text = new string(m_userlist.groupList[nselete].name);
            for (int j = 0; j < listView1.Items.Count; j++)
            {
                listView1.Items[j].Checked = false;
            }
            for (int i = 0; i < m_userlist.groupList[nselete].rigthNum; i++)
            {
                string strRight = new string(m_userlist.groupList[nselete].rights.Skip(i * NetSDK.SDK_NET_MAX_USER_LENGTH).Take(NetSDK.SDK_NET_MAX_USER_LENGTH).ToArray());
                for (int j = 0; j < listView1.Items.Count; j++)
                {
                    if (strRight == listView1.Items[j].SubItems[1].Text)
                    {
                        listView1.Items[j].Checked = true;
                        break;
                    }
                }
            }
        }

        private void comboBoxGroupName_SelectedIndexChanged(object sender, EventArgs e)
        {
            update();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            NetSDK.NET_SDK_CONF_MODIFYGROUP ModifyGroupinfo = new NetSDK.NET_SDK_CONF_MODIFYGROUP();
            ModifyGroupinfo.Group = m_userlist.groupList[comboBoxGroupName.SelectedIndex];
            ModifyGroupinfo.GroupName = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
            ModifyGroupinfo.Group.name = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
            ModifyGroupinfo.Group.memo = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
            m_userlist.groupList[comboBoxGroupName.SelectedIndex].name.CopyTo(ModifyGroupinfo.GroupName, 0);
            textBoxGroup.Text.ToCharArray().CopyTo(ModifyGroupinfo.Group.name, 0);
            textBoxMemo.Text.ToCharArray().CopyTo(ModifyGroupinfo.Group.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++;
                }
            }
            ModifyGroupinfo.Group.rigthNum = nIndex;
            ModifyGroupinfo.Group.rights = strRight;
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_CONF_MODIFYUSER)));
            Marshal.StructureToPtr(ModifyGroupinfo, pt, false);
            int nRet = NetSDK.NET_SDK_SetDevConfig(m_lUserID, (uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_MODIFY_GROUP, -1, pt, Marshal.SizeOf(typeof(NetSDK.NET_SDK_CONF_MODIFYGROUP)), 2000);
            if (nRet > 0)
            {
                MessageBox.Show("Successfully modified");
                m_userlist.groupList[comboBoxGroupName.SelectedIndex] = ModifyGroupinfo.Group;
            }
            else
            {
                MessageBox.Show("fail to edit");
            }
        }
    }
}
