package cc.smtweb.framework.core.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 〈IP〉工具类
 *
 * @author kevin
 * @since 1.0.0
 */
public class IpAddrUtil {

    private IpAddrUtil() {
    }

    //指定字符串是否工具类
    public static boolean isIpAddress(String ipAddr) {
        if (StringUtils.isEmpty(ipAddr)) {
            return false;
        } else {
            String regTxt =
                    "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b";
            return regMatch(ipAddr, regTxt);
        }
    }

    //正则匹配校验
    private static boolean regMatch(String source, String regTxt) {
        Pattern pattern = Pattern.compile(regTxt);
        Matcher matcher = pattern.matcher(source);
        return matcher.matches();
    }

    //ip转long
    public static long ip2Long(String ipAddr) {
        long[] ip = new long[4];
        int position1 = ipAddr.indexOf(".");
        int position2 = ipAddr.indexOf(".", position1 + 1);
        int position3 = ipAddr.indexOf(".", position2 + 1);
        ip[0] = Long.parseLong(ipAddr.substring(0, position1));
        ip[1] = Long.parseLong(ipAddr.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(ipAddr.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(ipAddr.substring(position3 + 1));
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }

    //long转ip格式
    public static String long2IP(long ipAddr) {
        return (ipAddr >>> 24) +
                "." +
                ((ipAddr & 16777215L) >>> 16) +
                "." +
                ((ipAddr & 65535L) >>> 8) +
                "." +
                (ipAddr & 255L);
    }

    //获取当前请求ip，返回long格式
    public static long getLongIp(HttpServletRequest request) {
        try {
            String ipAddr = getIpAddr(request);
            return StringUtils.isBlank(ipAddr) ? 0L: ip2Long(ipAddr);
        } catch (Exception var2) {
            return 0L;
        }
    }

    //获取当前请求的ip，返回ip格式
    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return "";
        } else {
            String ip = request.getHeader("X-Forwarded-For");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }

            return ip;
        }
    }
}