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 ModifyUser : Form
    {
        public NetSDK.NET_SDK_USER_MANAGE_INFO m_userlist;
        public int m_lUserID = 0;
        public ModifyUser()
        {
            InitializeComponent();
        }
        public void Init()
        {
            comboBoxName.Items.Clear();
            comboBoxGroud.Items.Clear();
            for (int i = 0; i < m_userlist.userNum; i++)
            {
                comboBoxName.Items.Insert(i,new string(m_userlist.userList[i].name));
                comboBoxName.SelectedIndex = 0;
            }
            for (int i=0;i<m_userlist.groupNum;i++)
            {
                comboBoxGroud.Items.Add(new string(m_userlist.groupList[i].name));
            }
            update();
        }
        public void update()
        {
            int nselete = comboBoxName.SelectedIndex;
            textBoxMemo.Text =new string( m_userlist.userList[nselete].memo);
            textBoxName.Text = new string(m_userlist.userList[nselete].name);
            comboBoxGroud.SelectedIndex = comboBoxGroud.Items.IndexOf(new string(m_userlist.userList[nselete].groupname));
            int nGroupSelete = comboBoxGroud.SelectedIndex;
            if(nGroupSelete<0)
            {
                return;
            }
            listView1.Items.Clear();
            for (int i = 0; i < m_userlist.groupList[nGroupSelete].rigthNum; i++)
            {
                ListViewItem rightitem = new ListViewItem(new string[] { (i + 1).ToString(), new string( m_userlist.groupList[nGroupSelete].rights.Skip(i * NetSDK.SDK_NET_MAX_USER_LENGTH).Take(NetSDK.SDK_NET_MAX_USER_LENGTH).ToArray()) });
                rightitem.Checked = false;
                listView1.Items.Add(rightitem);
            }
            for(int i=0;i<m_userlist.userList[nselete].rigthNum;i++)
            {
                string strRight = new string(m_userlist.userList[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;
                    }
                }
            }
            checkBoxShareable.Checked = false;
            checkBoxShareable.Checked = m_userlist.userList[nselete].shareable!='\0';
        }

        private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e)
        {
            update();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            NetSDK.NET_SDK_CONF_MODIFYUSER ModifyUserinfo = new NetSDK.NET_SDK_CONF_MODIFYUSER();
            ModifyUserinfo.User = m_userlist.userList[comboBoxName.SelectedIndex];
            ModifyUserinfo.UserName = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
            ModifyUserinfo.User.name = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
            ModifyUserinfo.User.memo = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
            ModifyUserinfo.User.groupname = new char[NetSDK.SDK_NET_MAX_USER_LENGTH];
            m_userlist.userList[comboBoxName.SelectedIndex].name.CopyTo(ModifyUserinfo.UserName, 0);
            textBoxName.Text.ToCharArray().CopyTo(ModifyUserinfo.User.name, 0);
            textBoxMemo.Text.ToCharArray().CopyTo(ModifyUserinfo.User.memo, 0);
            comboBoxGroud.Text.ToCharArray().CopyTo(ModifyUserinfo.User.groupname,0);
            ModifyUserinfo.User.shareable=checkBoxShareable.Checked?'1':'\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++;
		        }
	        }
            ModifyUserinfo.User.rigthNum = nIndex;
            ModifyUserinfo.User.rights = strRight;
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_CONF_MODIFYUSER)));
            Marshal.StructureToPtr(ModifyUserinfo, pt, false);
            int nRet = NetSDK.NET_SDK_SetDevConfig(m_lUserID,(uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_MODIFY_USER, -1, pt, Marshal.SizeOf(typeof(NetSDK.NET_SDK_CONF_MODIFYUSER)), 2000);
            if(nRet>0)
            {
                MessageBox.Show("Successfully modified");
                m_userlist.userList[comboBoxName.SelectedIndex]=ModifyUserinfo.User;
            }
            else
            {
                MessageBox.Show("fail to edit");
            }
        }
    }
}
