package com.chinaweal.aiccs.common.util;

import com.chinaweal.aiccs.common.constant.Constant;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 解析工具
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2020/8/7 16:41
 **/
public abstract class AnalysisUtils {

    /**
     * 通过地址获取广州辖区
     * @param address 地址
     * @return  辖区名称
     * @author lroyia
     * @since  2021年5月25日 11:02:18
     */
    public static String getRegionGZ(String address){
//        for (String s : Constant.GZ_All_REGION) {
//            if(address.contains(s)){
//                return s;
//            }
//        }
        return null;
    }

    /**
     * 通过详细地址获取街道名称
     * @param address 解析
     * @param area 地区信息
     * @return 街道名
     */
    public static String getStreetNameByAddress(String address, String area){
        if(StringUtils.isBlank(address) || StringUtils.isBlank(area)) return null;

        int indexOfRegion = address.indexOf(area);
        if(indexOfRegion > -1){
            // 如果存在，直接截掉地区信息及其之前的信息
            address = address.substring(indexOfRegion + area.length());
        }else{
            // 如果不存在，去掉“区”字再判断一次
            String tempRegion = area.replace("区", "");
            indexOfRegion = address.indexOf(tempRegion);
            if(indexOfRegion > -1){
                // 如果存在，直接截掉地区信息及其之前的信息
                address = address.substring(indexOfRegion + tempRegion.length());
            }else{
                // 如果不存在，直接解析失败
                return null;
            }
        }
        return getStreetNameByAddress(address);
    }

    /**
     * 匹配xxx省?xxx市xxx区?xxx镇?xxx村?的正则表达式
     */
    public static final String NM_PROVINCE_CITY_AREA_REGEX = "(?<province>[^省]+省|.+自治区)?(?<city>[^市]+市|.+自治州)?(?<county>[^县]+县|[^区]+区|.+局)?(?<town>[^镇]{1,4}镇)?";

    /**
     * 通过详细地址提取街道名
     * @param address 详细地址
     * @return 街道名
     */
    public static String getStreetNameByAddress(String address){
        if(StringUtils.isBlank(address)) return null;

        // 1、先去除xxx省?xxx市xxx区?xxx镇?xxx村?的信息
        address = address.replaceFirst(NM_PROVINCE_CITY_AREA_REGEX, "");

        // 2、取街字和路字的索引
        int jieIndex = address.indexOf("街"), luIndex = address.indexOf("路");

        String street;
        if(jieIndex > -1 && luIndex > -1){
            if(jieIndex > luIndex){
                // xxx路xxx街 取 xxx路为结果
                street = address.substring(0, luIndex+1);
            }else{
                // xxx街xxx路 取 xxx路为结果
                street = address.substring(jieIndex + 1, luIndex + 1);
            }
        }else if(jieIndex > -1){
            // 有街字 取 xxx街
            street = address.substring(0, jieIndex+1);
        }else if(luIndex > -1){
            // 有路字 取 xxx路
            street = address.substring(0, luIndex + 1);
        }else {
            // 判定 xxx大道的路名
            int daoIndex = address.indexOf("道");
            int xiangIndex = address.indexOf("巷");
            int parkIndex = address.indexOf("公园");
            int park1Index = address.indexOf("园");
            int postIndex = address.lastIndexOf("岗");
            int teamIndex = address.indexOf("队");
            int siteIndex = address.indexOf("站");
            int agencyIndex = address.indexOf("社");
            int villageIndex = address.indexOf("村");
            int industrialIndex = address.indexOf("工业区");
            int industrial1Index = address.indexOf("工业园");
            int industrial2Index = address.indexOf("砖厂");
            int marketIndex = address.indexOf("市场");
            int hotelIndex = address.indexOf("酒店");
            int hotel1Index = address.indexOf("宾馆");
            int market1Index = address.indexOf("城");
            int townIndex = address.indexOf("镇");
            if(daoIndex > 0){
                char position = daoIndex+1 < address.length() ? address.charAt(daoIndex+1) : '-';
                if(position == '东' || position == '南' || position == '西' || position == '北' || position == '中'){
                    street = address.substring(0, daoIndex + 2);
                }else{
                    street = address.substring(0, daoIndex + 1);
                }
            }else if(xiangIndex > 0){
                street = address.substring(0, xiangIndex + 1);
            }else if(parkIndex > 0) {
                char position = parkIndex + 2 < address.length() ? address.charAt(parkIndex + 2) : '-';
                if (position == '东' || position == '南' || position == '西' || position == '北' || position == '中' || position == '前') {
                    street = address.substring(0, parkIndex + 3);
                } else {
                    street = address.substring(0, parkIndex + 2);
                }
            }else if(postIndex > 0) {
                street = address.substring(0, postIndex + 1);
            }else if(park1Index > 0) {
                street = address.substring(0, park1Index + 1);
            }else if(teamIndex > 0) {
                street = address.substring(0, teamIndex + 1);
            }else if(siteIndex > 0){
                street = address.substring(0, siteIndex + 1);
            }else if(agencyIndex > 0){
                street = address.substring(0, agencyIndex + 1);
            }else if(villageIndex > 0){
                street = address.substring(0, villageIndex + 1);
            }else if(industrialIndex > 0){
                street = address.substring(0, industrialIndex + 3);
            }else if(industrial1Index > 0){
                street = address.substring(0, industrial1Index + 3);
            }else if(industrial2Index > 0){
                street = address.substring(0, industrial2Index + 2);
            }else if(marketIndex > 0){
                street = address.substring(0, marketIndex + 2);
            }else if(hotelIndex > 0){
                street = address.substring(0, hotelIndex + 2);
            }else if(hotel1Index > 0){
                street = address.substring(0, hotel1Index + 2);
            }else if(market1Index > 0){
                street = address.substring(0, market1Index + 1);
            }else if(townIndex > 0){
                street = address.substring(0, townIndex + 1);
            }else{
                for (String each : SPECIAL_STREET) {
                    int indexSpec = address.indexOf(each);
                    if(indexSpec > -1){
                        return address.substring(0, indexSpec + each.length());
                    }
                }
                return null;
            }
        }
        return street;
    }

    public static final Set<String> SPECIAL_STREET = new LinkedHashSet<>();
    static{
        // 越秀
        SPECIAL_STREET.add("南胜东里");
        SPECIAL_STREET.add("五眼桥");
        SPECIAL_STREET.add("牛乳基");
        SPECIAL_STREET.add("杨箕外");
        SPECIAL_STREET.add("文明门");
        SPECIAL_STREET.add("东沙角");
        SPECIAL_STREET.add("文化里");
        SPECIAL_STREET.add("青龙坊");
        SPECIAL_STREET.add("世光里");
        SPECIAL_STREET.add("万年里");
        SPECIAL_STREET.add("太华坊");
        SPECIAL_STREET.add("丽水坊");
        SPECIAL_STREET.add("三府前");
        // 天河区
        SPECIAL_STREET.add("珠江新城");
        SPECIAL_STREET.add("背坪拾排");
        SPECIAL_STREET.add("丁木塘");
        SPECIAL_STREET.add("北燕里");
        SPECIAL_STREET.add("天乐径");
        // 南沙区
        SPECIAL_STREET.add("坦头乡");
        // 黄埔区
        SPECIAL_STREET.add("大沙地东");
        SPECIAL_STREET.add("下沙寮尾");
        SPECIAL_STREET.add("大沙西");
        SPECIAL_STREET.add("双沙");
        // 海珠区
        SPECIAL_STREET.add("晓园北");
        SPECIAL_STREET.add("赤沙");
        // 荔湾区
        SPECIAL_STREET.add("万善南");
        SPECIAL_STREET.add("乐华菜地");
        SPECIAL_STREET.add("上陈塘");
        SPECIAL_STREET.add("和茂里");
        SPECIAL_STREET.add("七甫水脚");
        SPECIAL_STREET.add("七甫");
        SPECIAL_STREET.add("丰厚里");
        SPECIAL_STREET.add("德美里");
        SPECIAL_STREET.add("祥金里");
        SPECIAL_STREET.add("云津里");
    }
}
