package com.axis.innovators.box.tools;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * 获取用户计算机信息
 * @author tzdwindows 7
 */
public class SystemInfoUtil {
    private static final Logger logger = LogManager.getLogger(SystemInfoUtil.class);

    /**
     * 获取操作系统架构
     */
    public static String getOSArch() {
        return System.getProperty("os.arch");
    }

    /**
     * 获取操作系统名称
     */
    public static String getOSName() {
        return System.getProperty("os.name");
    }

    /**
     * 获取 CPU 信息
     */
    public static String getCPUInfo() {
        try {
            if (System.getProperty("os.name").toLowerCase().contains("win")) {
                // Windows 系统
                Process process = Runtime.getRuntime().exec("wmic cpu get name");
                return getString(process);
            } else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                // Linux 系统
                Process process = Runtime.getRuntime().exec("lscpu");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains("Model name")) {
                        return line.split(":")[1].trim();
                    }
                }
            }
        } catch (Exception e) {
            logger.error("Failed to get CPU info", e);
        }
        return "Unknown CPU";
    }

    /**
     * 获取 GPU 信息
     */
    public static String getGPUInfo() {
        try {
            if (System.getProperty("os.name").toLowerCase().contains("win")) {
                // Windows 系统
                Process process = Runtime.getRuntime().exec("wmic path win32_videocontroller get name");
                return getString(process);
            } else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                // Linux 系统
                Process process = Runtime.getRuntime().exec("lspci | grep VGA");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = reader.readLine();
                if (line != null) {
                    return line.split(":")[2].trim();
                }
            }
        } catch (Exception e) {
            logger.error("Failed to get GPU info", e);
        }
        return "Unknown GPU";
    }

    private static String getString(Process process) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        List<String> gpuInfo = new ArrayList<>();
        while ((line = reader.readLine()) != null) {
            if (!line.trim().isEmpty() && !line.contains("Name")) {
                gpuInfo.add(line.trim());
            }
        }
        return String.join(", ", gpuInfo);
    }
}
