package netmanager.lib;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Properties;

import com.cm.smartgate.config.NetManagementConfig;
import com.sun.jna.Platform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// 平台操作类
@Component
public class PlatUtils {

    @Autowired
    private static NetManagementConfig netManagementConfig;

    public PlatUtils() {
    }

    // 获取系统前缀表示（小写）
    public static String sdk_plat_getOsPrefix() {
        String strArch = System.getProperty("os.arch").toLowerCase();
        final String strName = System.getProperty("os.name");
        String strOsPrefix;

        int nPlatform = Platform.getOSType();
        if (Platform.WINDOWS == nPlatform) {
            if ("i386".equals(strArch)) {
                strArch = "x86";
            } else if ("x86_64".equals(strArch) || "amd64".equals(strArch)) {
                strArch = "x64";
            }
            strOsPrefix = "win-" + strArch;
        } else if (Platform.LINUX == nPlatform) {
            if ("x86".equals(strArch)) {
                strArch = "x86";
            } else if ("x86_64".equals(strArch) || "amd64".equals(strArch)) {
                strArch = "x64";
            }
            strOsPrefix = "linux-" + strArch;
        } else {
            strOsPrefix = strName.toLowerCase();
            if ("x86".equals(strArch)) {
                strArch = "x86";
            } else if ("x86_64".equals(strArch)) {
                strArch = "x64";
            }

            int nSpace = strOsPrefix.indexOf(" ");
            if (nSpace != -1) {
                strOsPrefix = strOsPrefix.substring(0, nSpace);
            }
            strOsPrefix += "-" + strArch;
        }

        System.out.println(strOsPrefix);
        return strOsPrefix;
    }

    // 读取当前目录下的路径配置文件
    public static String sdk_plat_getCfgProperty(String configPath, String key) {
        Properties properties = new Properties();
//        String strRootPath = System.getProperty("user.dir").replace("\\", "/");
        String strRootPath = configPath.replace("\\", "/");
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(strRootPath + "/config.properties");
            properties.load(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return properties.getProperty(key);
    }

    // 获取系统类型名称
    public static String sdk_plat_getOsTypeName() {
        String strOsName = "";
        String strOsPrefix = sdk_plat_getOsPrefix();
        if (strOsPrefix.startsWith("win-x86") || strOsPrefix.startsWith("win-x64")) {
            strOsName = "win";
        } else if (strOsPrefix.startsWith("linux-x86") || strOsPrefix.startsWith("linux-x64")) {
            strOsName = "linux";
        } else {
            strOsName = "unknown";
        }

        return strOsName;
    }

    // 获取加载库名称
    public static String sdk_plat_getLoadLibrary(String configPath, String strLibrary) {
        String strOsPrefix = sdk_plat_getOsPrefix();
        String strLoadLibraryDir = "";
        String strLibraryName = "";
        if (strOsPrefix.startsWith("win-x86")) {
            strLoadLibraryDir = sdk_plat_getCfgProperty(configPath,"win-x86");
            strLibraryName = strLibrary + ".dll";
        } else if (strOsPrefix.startsWith("win-x64")) {
            strLoadLibraryDir = sdk_plat_getCfgProperty(configPath,"win-x64");
            strLibraryName = strLibrary + ".dll";
        } else if (strOsPrefix.startsWith("linux-x86")) {
            strLoadLibraryDir = sdk_plat_getCfgProperty(configPath,"linux-x86");
            strLibraryName = "lib" + strLibrary + ".so";
        } else if (strOsPrefix.startsWith("linux-x64")) {
            strLoadLibraryDir = sdk_plat_getCfgProperty(configPath, "linux-x64");
            strLibraryName = "lib" + strLibrary + ".so";
        }

        String strResult = configPath + strLoadLibraryDir + strLibraryName;
        System.out.println(strResult);
        return strResult;
    }

    // 获取当前时间
    public static String sdk_plat_getDate(String format) {
        SimpleDateFormat simpleDate = null;
        if (null == format || format.isEmpty()) {
            simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            simpleDate = new SimpleDateFormat(format);
        }
        return simpleDate.format(new java.util.Date());
    }

}
