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 Password : Form
    {
        public NetSDK.NET_SDK_USER_MANAGE_INFO m_userlist;
        public int m_lUserID = 0;
        public Password()
        {
            InitializeComponent();
        }

        private void comboBoxMane_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxOld.Text = "";
            textBoxNew.Text = "";
            textBoxConfirm.Text = "";
        }
        public void Init()
        {
            comboBoxName.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;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            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;
            }
            byte[] outOld = new byte[NetSDK.SDK_NET_MAX_USER_LENGTH];
            byte[] inOld = new byte[NetSDK.SDK_NET_MAX_USER_LENGTH];
            inOld = System.Text.Encoding.Default.GetBytes(textBoxOld.Text);
            NetSDK.NET_SDK_MD5Encrypt(outOld, inOld);
            if (System.Text.Encoding.Default.GetString(outOld)!=new string(m_userlist.userList[comboBoxName.SelectedIndex].passWord))
            {
                MessageBox.Show("The original password is wrong");
            }
            else
            {
                if(textBoxNew.Text!=textBoxConfirm.Text)
                {
                    MessageBox.Show("Inconsistent input password");
                }
                else
                {
                    byte[] outNew = new byte[NetSDK.SDK_NET_MAX_USER_LENGTH];
                    byte[] inNew = new byte[NetSDK.SDK_NET_MAX_USER_LENGTH];
                    inNew = System.Text.Encoding.Default.GetBytes(textBoxNew.Text);
                    if (nEncrypt == NetSDK.NET_SDK_PASSWORDENCRYPT.PASSWORD_PLAIN)
                    {
                        outNew = inNew;
                    }
                    else if (nEncrypt == NetSDK.NET_SDK_PASSWORDENCRYPT.PASSWORD_MD5)
                    {
                        NetSDK.NET_SDK_MD5Encrypt(outNew, inNew);
                    }
                    NetSDK.NET_SDK_CONF_MODIFY_PSW MoifyPsw= new NetSDK.NET_SDK_CONF_MODIFY_PSW();
                    MoifyPsw.sUserName = m_userlist.userList[comboBoxName.SelectedIndex].name;
                    MoifyPsw.Password = m_userlist.userList[comboBoxName.SelectedIndex].passWord;
                    MoifyPsw.iEncryptType = (int)nEncrypt;
                    MoifyPsw.NewPassword = outNew;
                    IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetSDK.NET_SDK_CONF_MODIFY_PSW)));
                    Marshal.StructureToPtr(MoifyPsw, pt, false);
                    nRet = NetSDK.NET_SDK_SetDevConfig(m_lUserID, (uint)NetSDK.NET_SDK_CONFIG_TYPE.NET_SDK_CONFIG_MODIFY_PSW, -1, pt, Marshal.SizeOf(typeof(NetSDK.NET_SDK_CONF_MODIFY_PSW)), 2000);
                    if (nRet > 0)
                    {
                        MessageBox.Show("Successfully modified");
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("fail to edit");
                    }
                }
            }
        }
    }
}
