package com.mortals.xhx.tts.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ZYW
 * @date 2024-08-22 11:10
 */

public class ReadMacAdrss {
    public static final boolean isWindows = System.getProperty("os.name")
            .startsWith("Win");

    public static final boolean isLinux = System.getProperty("os.name").equals(
            "Linux");

    public static final boolean isAIX = System.getProperty("os.name").equals(
            "AIX");

    public static final boolean isSolaris = (System.getProperty("os.name")
            .equals("Solaris")) ||
            (System.getProperty("os.name").equals("SunOS"));

    public static final boolean isHPUX = System.getProperty("os.name").equals(
            "HP-UX");
    public static final String WindowsCmd = "cmd.exe /c ipconfig/all";
    public static final String LinuxCmd = "ifconfig -a";
    public static final String AIXCmd = "netstat -ia";
    public static final String SolarisCmd = "/sbin/ifconfig -a";
    public static final String HPUXCmd = "/usr/sbin/lanscan";
    private static String mPhysicalAddress = "";

    private static int mPhysicalMacNumber = 0;

    private  static boolean isInit = false;

    public static String getMacAddress()
    {
        try
        {
            Process process = Runtime.getRuntime().exec(getMacCommand());
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            Pattern macPattern = Pattern.compile(getRegStr());
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if ("".equals(line))
                    continue;
                Matcher macMatcher = macPattern.matcher(line);
                boolean result = macMatcher.find();
                if (result) {
                    mPhysicalMacNumber += 1;
                    String mac = macMatcher.group(0);
                    mac = mac.replace(':', '-');
                    if (isHPUX)
                    {
                        mac = dealMac(mac);
                        return mac;
                    }
                    if ("".equals(mPhysicalAddress)) {
                        mPhysicalAddress = mac;
                        return mac;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        isInit = true;

        return "未找到mac";
    }

    public static String getMacCommand()
    {
        if (isWindows) {
            return "cmd.exe /c ipconfig/all";
        }
        if (isLinux) {
            return "ifconfig -a";
        }
        if (isAIX) {
            return "netstat -ia";
        }
        if (isSolaris) {
            return "/sbin/ifconfig -a";
        }
        if (isHPUX) {
            return "/usr/sbin/lanscan";
        }
        return "";
    }

    public static String getRegStr()
    {
        if (isWindows) {
            return "([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}";
        }
        if (isLinux) {
            return "([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}";
        }
        if (isAIX) {
            return "([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}";
        }
        if (isSolaris) {
            return "([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}";
        }
        if (isHPUX)
        {
            return "0x[0-9a-zA-Z]{12}";
        }
        return "";
    }


    public static String dealMac(String mac) {
        mac = mac.substring(2);
        StringBuffer sAdd = new StringBuffer();
        for (int i = 0; i < 12; ++i) {
            sAdd.append(mac.charAt(i));
            if (i % 2 == 1) {
                sAdd.append("-");
            }
        }
        return sAdd.substring(0, sAdd.length() - 1);
    }
}
