package epson.common;

import android.os.Build;

import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;

public class IPAddressUtils {
    private static final String IPADDRESS6_EXP = "((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$";
    private static final String IPADDRESS_EXP = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";

    public static boolean isIPv4Address(String str) {
        if (str == null) {
            return false;
        }
        return str.matches(IPADDRESS_EXP);
    }

    public static boolean isIPv6Address(String str) {
        if (str == null) {
            return false;
        }
        return str.matches(IPADDRESS6_EXP);
    }

    public static String convertforAndroid(String str) {
        int indexOf;
        int i;
        if (str == null) {
            return null;
        }
        if (isIPv4Address(str) || !isIPv6Address(str) || (indexOf = str.indexOf("%")) == -1) {
            return str;
        }
        String substring = str.substring(0, indexOf);
        String substring2 = str.substring(indexOf + 1);
        if (!substring2.matches("\\d+")) {
            try {
                NetworkInterface byName = NetworkInterface.getByName(substring2);
                if (byName != null) {
                    if (Build.VERSION.SDK_INT < 19) {
                        try {
                            i = ((Integer) NetworkInterface.class.getMethod("getIndex", new Class[0]).invoke(byName, new Object[0])).intValue();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        i = byName.getIndex();
                    }
                }
            } catch (SocketException e2) {
                e2.printStackTrace();
            }
            i = -1;
        } else {
            i = Integer.getInteger(substring2, -1).intValue();
        }
        if (i != -1) {
            substring2 = String.format("%02d", new Object[]{Integer.valueOf(i)});
        }
        return substring + "%" + substring2;
    }

    public static String buildURL(String str, String str2, int i, String str3) {
        try {
            return new URL(str, convertforAndroid(str2), i, str3).toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return str + "://" + str2 + ":" + i + str3;
        }
    }

    public static String buildURL(String str, String str2, String str3) {
        if ("https".equalsIgnoreCase(str)) {
            return buildURL(str, str2, 443, str3);
        }
        return buildURL(str, str2, 80, str3);
    }
}
