using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;

namespace RyHardWare
{
    /// <summary>
    /// 网络操作类
    /// </summary>
    public class Network
    {
        ///<summary>
        /// 通过NetworkInterface读取网卡Mac
        ///</summary>
        ///<returns></returns>
        public static string GetMacs()
        {
            string macs = "";
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                macs += ni.GetPhysicalAddress().ToString()+"|";
            }
            return macs;
        }
        ///<summary>
        /// 通过NetworkInterface读取网卡Mac
        ///</summary>
        ///<returns></returns>
        public static List<string> GetMacByNetworkInterface()
        {
            List<string> macs = new List<string>();
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                macs.Add(ni.GetPhysicalAddress().ToString());
            }
            return macs;
        }
        /// <summary>
        /// 根据mac字符串来获取最终的mac地址,可以是各种MAC地址格式,非法的内容将返回null
        /// </summary>
        /// <param name="_mac"></param>
        /// <returns></returns>
        public static byte[] GetMac(string _mac)
        {
            try
            {
                if (_mac.Length == 12)
                {
                    #region 无任何分隔符的MAC地址
                    byte[] mac = new byte[6];
                    for (int z = 0; z < 6; z++)
                    {
                        mac[z] =
                            byte.Parse(_mac.Substring(z * 2, 2),
                            NumberStyles.HexNumber);
                    }
                    return mac;
                    #endregion
                }
                else if (_mac.Length == 17)
                {
                    #region 带分隔符的MAC地址
                    byte[] mac = new byte[6];
                    for (int z = 0; z < 6; z++)
                    {
                        mac[z] =
                            byte.Parse(_mac.Substring(z * 3, 2),
                            NumberStyles.HexNumber);
                    }
                    return mac;
                    #endregion
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 远程唤醒
        /// </summary>
        /// <param name="_ip"></param>
        /// <param name="_mac"></param>
        /// <returns></returns>
        public static int WakeUp(string _ip, string _mac)
        {
            try
            {
                if (_ip == "127.0.0.1")
                {
                    _ip = "192.168.1.1";
                }
                byte[] mac = GetMac(_mac);
                if (mac == null)
                {
                    return -2;
                }
                IPAddress ipe = IPAddress.Parse(_ip);
                UdpClient client = new UdpClient();
                byte[] ipbyte = ipe.GetAddressBytes();
                ipbyte[ipbyte.Length - 1] = 255;
                IPAddress ip = new IPAddress(ipbyte);
                client.Connect(ip, 7);
                byte[] packet = new byte[17 * 6];

                for (int i = 0; i < 6; i++)
                    packet[i] = 0xFF;

                for (int i = 1; i <= 16; i++)
                    for (int j = 0; j < 6; j++)
                        packet[i * 6 + j] = mac[j];

                int result = client.Send(packet, packet.Length);
                if (result != packet.Length)
                {
                    return 0;
                }
                return 1;
            }
            catch { return -1; }
        }
        /// <summary>
        /// 远程唤醒
        /// </summary>
        /// <param name="_hostname"></param>
        /// <param name="port"></param>
        /// <param name="_mac"></param>
        /// <returns></returns>
        public static int WakeUpByHostname(string _hostname,int port, string _mac)
        {
            try
            {
                byte[] mac = GetMac(_mac);
                if (mac == null)
                {
                    return -2;
                }
                UdpClient client = new UdpClient();
                client.Connect(_hostname, port);
                byte[] packet = new byte[17 * 6];

                for (int i = 0; i < 6; i++)
                    packet[i] = 0xFF;

                for (int i = 1; i <= 16; i++)
                    for (int j = 0; j < 6; j++)
                        packet[i * 6 + j] = mac[j];

                int result = client.Send(packet, packet.Length);
                if (result != packet.Length)
                {
                    return 0;
                }
                return 1;
            }
            catch { return -1; }
        }
    }
}
