package com.inkasso.factoring.common.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.inkasso.factoring.common.domain.TiandituResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Component
public class AddressResolutionUtil {


    private static final  String key = "1043545f705a22eb342ed6e7cd924380";

    private static final String  LOCATION_2_NAME = "https://api.tianditu.gov.cn/geocoder";


    private static final String NAME_2_LOCATION = "http://api.tianditu.gov.cn/geocoder";

    @Autowired
    private RestTemplate restTemplate;
	/**
     * 解析地址
     * @author lin
     * @param address
     * @return
     */
//    public  String addressResolution(String address){
//        String substring = address.substring(0, 3);
//        if(substring.contains("北京市")){
//            return "北京市";
//        }
//        if(substring.contains("上海市")){
//            return "上海市";
//        }
//        if(substring.contains("天津市")){
//            return "天津市";
//        }
//        if(substring.contains("重庆市")){
//            return "重庆市";
//        }
//        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
//        Matcher m=Pattern.compile(regex).matcher(address);
//        String province=null,city=null,county=null,town=null,village=null;
//        Map<String,String> row= new HashMap<String,String>();
//        while(m.find()){
//            province=m.group("province");
//            row.put("province", province==null?"":province.trim());
//            city=m.group("city");
//            row.put("city", city==null?"":city.trim());
//            county=m.group("county");
//            row.put("county", county==null?"":county.trim());
//            town=m.group("town");
//            row.put("town", town==null?"":town.trim());
//            village=m.group("village");
//            row.put("village", village==null?"":village.trim());
//        }
//        return row.get("city");
//    }


    /**
     * 天地图接口 通过 坐标系 获取 地理位置
     *{
     * 	"result": {
     * 		"formatted_address": "北京市西城区西什库大街19号院",
     * 		"location": {
     * 			"lon": 116.37304,
     * 			"lat": 39.92594
     *                },
     * 		"addressComponent": {
     * 			"address": "西什库大街19号院",
     * 			"city": "北京市",
     * 			"county_code": "156110102",
     * 			"nation": "中国",
     * 			"poi_position": "西南",
     * 			"county": "西城区",
     * 			"city_code": "156110000",
     * 			"address_position": "西南",
     * 			"poi": "西什库大街19号院",
     * 			"province_code": "156110000",
     * 			"province": "北京市",
     * 			"road": "大红罗厂街",
     * 			"road_distance": 49,
     * 			"poi_distance": 21,
     * 			"address_distance": 21
     *        }* 	},
     * 	"msg": "ok",
     * 	"status": "0"
     * }
     *
     */
    public String  location2Name(Double lon,Double lat){
        //{'lon':116.37304,'lat':39.92594,'ver':1}
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("lon",lon);
        jsonObject.put("lat",lat);
        jsonObject.put("ver",1);

        Map<String,Object> map = new HashMap<>();
        map.put("tk",key);
        map.put("postStr",jsonObject.toJSONString());
        String url = LOCATION_2_NAME+"?postStr={postStr}&type=geocode&tk={tk}";

        String forObject = restTemplate.getForObject(url, String.class, map);
        JSONObject result = JSONObject.parseObject(forObject);
        String city = result.getJSONObject("result").getJSONObject("addressComponent").getString("city");
        return city;
    }

    /**
     * 地理位置解析 出 坐标
     * @param name
     *{
     * 	"msg": "ok",
     * 	"location": {
     * 		"score": 100,
     * 		"level": "区县及以上级行政区划",
     * 		"lon": 108.93949,
     * 		"lat": 34.266345,
     * 		"keyWord": "西安市"
     *        },
     * 	"searchVersion": "6.4.6V",
     * 	"status": "0"
     * }
     * @return
     */
    public TiandituResult name2Location(String name){
        //?ds={"keyWord":"延庆区北京市延庆区延庆镇莲花池村前街50夕阳红养老院"}&tk=您的密钥
        JSONObject object = new JSONObject();
        object.put("keyWord",name);

        Map<String,Object> map = new HashMap<>();
        map.put("ds",object.toJSONString());
        map.put("tk",key);

        String url = NAME_2_LOCATION+"?ds={ds}&tk={tk}";
        String forObject = restTemplate.getForObject(url, String.class, map);
        JSONObject result = JSONObject.parseObject(forObject);
        JSONObject location = result.getJSONObject("location");
        TiandituResult tiandituResult = JSON.toJavaObject(location, TiandituResult.class);
        return tiandituResult;
    }

}
