/**
 * 
 */
package org.springblade.core.license;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

import org.springblade.core.license.model.LicenseCheckModel;

/**
 * @author wookvn
 *
 */
public interface IServerInfo {

	/**
	 * 获取CPU序列号
	 * @return
	 * @throws Exception
	 */
	public String getCPUSerial() throws Exception;
	
	/**
	 * 获取主板序列号
	 * @return
	 * @throws Exception
	 */
	public String getMainBoardSerial() throws Exception;
	
	/**
	 * 
	 * @return
	 * @throws Exception 
	 */
	default LicenseCheckModel getServerInfo() throws Exception {
		LicenseCheckModel licenseCheckModel = new LicenseCheckModel();
		licenseCheckModel.setCpuSerial(this.getCPUSerial());
		licenseCheckModel.setMainBoardSerial(this.getMainBoardSerial());
		licenseCheckModel.setIpAddresses(this.getIpAddress());
		licenseCheckModel.setMacAddresses(this.getMacAddress());
		return licenseCheckModel;
	}
	
	/**
	 * 获取mac地址
	 * @return
	 * @throws Exception
	 */
	default List<String> getMacAddress() throws Exception {
		List<String> result = null;
        //1. 获取所有网络接口
        List<InetAddress> inetAddresses = getLocalAllInetAddress();
        if(inetAddresses != null && inetAddresses.size() > 0){
            //2. 获取所有网络接口的Mac地址
            result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().map(String::trim).collect(Collectors.toList());
        }
        return result;
	}
	
	/**
	 * 获取IP地址
	 * @return
	 * @throws Exception
	 */
	default List<String> getIpAddress() throws Exception{
		List<String> result = null;
        //获取所有网络接口
        List<InetAddress> inetAddresses = getLocalAllInetAddress();
        if(inetAddresses != null && inetAddresses.size() > 0){
            result = inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(item -> item.trim().toLowerCase()).collect(Collectors.toList());
        }
        return result;
	}
	
	/**
	 * 获取当前服务器所有符合条件的InetAddress
	 * @return
	 * @throws Exception
	 */
	default List<InetAddress> getLocalAllInetAddress() throws Exception {
        List<InetAddress> result = new ArrayList<>(4);
        // 遍历所有的网络接口
        for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
            NetworkInterface iface = (NetworkInterface) networkInterfaces.nextElement();
            // 在所有的接口下再遍历IP
            for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                InetAddress inetAddr = (InetAddress) inetAddresses.nextElement();
                //排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址
                if(!inetAddr.isLoopbackAddress() /*&& !inetAddr.isSiteLocalAddress()*/
                        && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()){
                    result.add(inetAddr);
                }
            }
        }
        return result;
    }
	
	/**
	 * 获取某个网络接口的Mac地址
	 * @param inetAddr
	 * @return
	 */
	default String getMacByInetAddress(InetAddress inetAddr){
        try {
            byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();
            StringBuffer stringBuffer = new StringBuffer();
            for(int i=0;i<mac.length;i++){
                if(i != 0) {
                    stringBuffer.append("-");
                }
                //将十六进制byte转化为字符串
                String temp = Integer.toHexString(mac[i] & 0xff);
                if(temp.length() == 1){
                    stringBuffer.append("0" + temp);
                }else{
                    stringBuffer.append(temp);
                }
            }
            return stringBuffer.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}
