package com.nmgs.util;

import org.springframework.util.ClassUtils;

import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.Enumeration;
import java.util.Properties;

public class PathUtil {
    public static String TomcatPath = System.getProperty("catalina.home");
    public static String webappsPath = "";
    public static String applicationPath = "";
    public static String applicationTextPath = "";
    public static String projectApplicationPath;
    public static String webPath = "";
    public static String websockifyPath = "";
    public static String noVNCPath = "";
    public static boolean isWin = false;

    static {
        try {
            projectApplicationPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "static/application.properties";
            websockifyPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "static/websockify";
            noVNCPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "static/noVNC";
            webPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "static";
        } catch (UnsupportedEncodingException e) {
            projectApplicationPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/application.properties";
            webPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static";
            websockifyPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/websockify";
            noVNCPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/noVNC";
        }
    }

    public static String uploadPath = "";
    public static String uploadWarPath = "";
    public static String downloadWarPath = "";
    public static String hostAddress;
    public static String WebServiceWarPath;
    public static String tomcatUserXMLPath;
    public static String webName = "/noVNC";

    static {

        Properties props = System.getProperties();
        String os = props.getProperty("os.name").toLowerCase();
        if (os.startsWith("win")) {
            isWin = true;
//            TomcatPath = System.getProperty("user.dir").substring(0, System.getProperty("user.dir").lastIndexOf('\\'));
            webappsPath = TomcatPath + "\\webapps";
            // uploadPath = TomcatPath + "\\integralFile";
            uploadWarPath = TomcatPath + "\\taskWarFiles\\uploadWarFile";
            downloadWarPath = TomcatPath + "\\taskWarFiles\\downloadWarFile";
            webPath = webPath.substring(1, webPath.length()) + webName;
        } else {
            webappsPath = TomcatPath + "/webapps";
            // uploadPath = TomcatPath + "/integralFile";
            uploadWarPath = TomcatPath + "/taskWarFiles/uploadWarFile";
            downloadWarPath = TomcatPath + "/taskWarFiles/downloadWarFile";
            webPath = webPath + webName;
        }
        uploadPath = webappsPath + "/integralFile";
        applicationPath = webappsPath + "/application.properties";
        applicationTextPath = webappsPath + "/applicationText.properties";
        WebServiceWarPath = TomcatPath + "/WebServiceWar";
        tomcatUserXMLPath = TomcatPath + "/conf/tomcat-users.xml";

        //            hostAddress = Inet4Address.getLocalHost().getHostAddress();
        hostAddress = String.valueOf(getInet4Address());
        if(isWin){
            if (hostAddress.charAt(0) == '/' || hostAddress.charAt(0) == '\\') {
                hostAddress = hostAddress.substring(1);
            }
            if (applicationPath.charAt(0) == '/' || applicationPath.charAt(0) == '\\') {
                applicationPath = applicationPath.substring(1);
            }
            if (applicationTextPath.charAt(0) == '/' || applicationTextPath.charAt(0) == '\\') {
                applicationTextPath = applicationTextPath.substring(1);
            }
            if (projectApplicationPath.charAt(0) == '/' || projectApplicationPath.charAt(0) == '\\') {
                projectApplicationPath = projectApplicationPath.substring(1);
            }
            if (noVNCPath.charAt(0) == '/' || noVNCPath.charAt(0) == '\\') {
                noVNCPath = noVNCPath.substring(1);
            }
            if (websockifyPath.charAt(0) == '/' || websockifyPath.charAt(0) == '\\') {
                websockifyPath = websockifyPath.substring(1);
            }
        }
        /*
       获取本机网内地址
        */
    }

    public static InetAddress getInet4Address() {
        try {
            //获取所有网络接口
            Enumeration<NetworkInterface> allNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
            //遍历所有网络接口
            for (; allNetworkInterfaces.hasMoreElements(); ) {
                NetworkInterface networkInterface = allNetworkInterfaces.nextElement();
                //如果此网络接口为 回环接口 或者 虚拟接口(子接口) 或者 未启用 或者 描述中包含VM
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp() || networkInterface.getDisplayName().contains("VM")) {
                    //继续下次循环
                    continue;
                }
                //如果不是Intel与Realtek的网卡
//                if(!(networkInterface.getDisplayName().contains("Intel"))&&!(networkInterface.getDisplayName().contains("Realtek"))){
//                         //继续下次循环
//                            continue;
//                }
                //遍历此接口下的所有IP（因为包括子网掩码各种信息）
                for (Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); inetAddressEnumeration.hasMoreElements(); ) {
                    InetAddress inetAddress = inetAddressEnumeration.nextElement();
                    //如果此IP不为空
                    if (inetAddress != null) {
                        //如果此IP为IPV4 则返回
                        if (inetAddress instanceof Inet4Address) {
                            return inetAddress;
                        }
                        /*
                       // -------这样判断IPV4更快----------
                        if(inetAddress.getAddress().length==4){
                            return inetAddress;
                        }

                         */

                    }
                }


            }
            return null;

        } catch (SocketException e) {
            e.printStackTrace();
            return null;
        }
    }
}
