package com.hc.model;

import com.orhanobut.logger.Logger;

import org.junit.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() {
        assertEquals(4, 2 + 2);
    }

    @Test
    public void getIP() {
        System.out.println(getAuthority("http://www.baidu.com/system/verList"));
        System.out.println(getAuthority("http://WWW.BAIDU.COM/TESE/verList"));
        System.out.println(getAuthority("http://blog.csdn.net/weasleyqi/article/details/7912647", true));
        System.out.println(getAuthority("https://segmentfault.com/q/1010000000703645"));
        System.out.println(getAuthority("http://www.cnblogs.com/afarmer/archive/2011/08/29/2158860.html"));
        System.out.println(getAuthority("http://127.0.0.1:9040/123/1", false));
        System.out.println(getAuthority("127.0.0.11:9040/123/1", true));
        System.out.println("1:" + getIP("http://127.0.0.1:9040/system/verList", true));
        System.out.println("2:"
                + getIP("http://127.0.0.1:9040/system/verList?loginName=1&password=AD07FB25AA2D3A9F96EE12F25E0BE902", true));
        System.out.println("3:" + getIP("http://127.0.0.1:9040/", true));
    }

    private String getAuthority(String url, boolean needProtocol) {
        try {
            URL u = new URL(url);
            Logger.d("当前域名：$s", u.getProtocol());
            return needProtocol ? u.getProtocol() + "://" + u.getAuthority() : u.getAuthority();
        } catch (MalformedURLException e) {
            Logger.e(e.getCause(), e.getMessage());
            return getIP(url, needProtocol);
        }
    }

    private String getAuthority(String url) {
        return getAuthority(url, false);
    }


    private String getIP(String url) {
        return getIP(url, false);
    }

    private String getIP(String url, boolean needProtocol) {
        //使用正则表达式过滤，
        String re = "((http|ftp|https)://)(([a-zA-Z0-9._-]+)|([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))(([a-zA-Z]{2,6})|(:[0-9]{1,4})?)";
        String str = "";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(re);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(url);
        //若url==http://127.0.0.1:9040或www.baidu.com的，正则表达式表示匹配
        if (matcher.matches()) {
            str = url;
        } else {
            String[] split2 = url.split(re);
            if (split2.length > 1) {
                str = url.substring(0, url.length() - split2[1].length());
                if (!needProtocol) {
                    String[] split3 = str.split("//");
                    if (split3.length > 1) {
                        str = split3[1];
                    }
                }
            } else {
                str = split2[0];
            }
        }
        return str;
    }
}