package com.mortals.xhx.tts.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.UUID;

/**
 * @author ZYW
 * @date 2024-07-24 9:20
 */

public class MacAddressReader {
    public static String getMacAddress() {
        try {
            Process process = Runtime.getRuntime().exec("ip link");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("link/ether")) {
                    String macAddress = line.split(" ")[1];
                    macAddress = macAddress.trim();
                    macAddress = macAddress.replace(":", "");
                    return macAddress;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getUUID();
    }

    public static String getUUID(){
        String uuid = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader("/sys/class/dmi/id/product_uuid"));
            try {
                uuid = reader.readLine();
                System.out.println("Device UUID: " + uuid);
            } finally {
                reader.close();
            }
            return uuid;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "123456";
    }


    /**
     * 本机ip
     * @return
     */
    public static String getInternetIp(){
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            String ipAddress = inetAddress.getHostAddress();
            return ipAddress;
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取局域网ip
     * @return
     */
    public static String getLocalIp(){
        try {
            // 获取本机的所有网络接口
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                // 确保网络接口已启用
                if (networkInterface.isUp()) {
                    // 确保不是虚拟网络接口
                    if (!networkInterface.isLoopback()) {
                        // 遍历接口的所有IP地址
                        Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                        while (addresses.hasMoreElements()) {
                            InetAddress inetAddress = addresses.nextElement();
                            // 确保是IPv4地址
                            if (inetAddress instanceof java.net.Inet4Address) {
                                return  inetAddress.getHostAddress();
                            }
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "127.0.0.1";
    }


    public static void main(String[] args) {
        System.out.println("MAC Address: " + getMacAddress());
    }
}
