package com.zhangmeng.model.base.baseUtil;

import com.alibaba.fastjson.JSON;
import com.zhangmeng.model.dto.baidu.AddressToLatAndLng;
import com.zhangmeng.model.dto.baidu.BaiduAreaSearch;
import com.zhangmeng.model.dto.baidu.DirectionLite;
import com.zhangmeng.model.dto.baidu.Weather;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.zhangmeng.model.dto.baidu.*;

/**
 * @author zhangmeng
 * @version 1.0
 * @date 2021年6月10日11:21:22
 */
@Component
public class BaiduApiUtil {

    private static final String ak = "zFaoh4EormEhjSAloFhWccPcNhF4WZQo";

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 圆形区域检索 (搜索周围)
     * <p>
     * http://api.map.baidu.com/place/v2/search?query=银行&location=39.915,116.404&radius=2000&output=xml&ak=您的密钥
     *
     * @param queryList 关键字
     * @param lng       经度
     * @param lat       纬度
     * @param radius    圆形
     * @return BaiduAreaSearch
     */
    public BaiduAreaSearch circular_area_search(String queryList, String lng, String lat, String radius) {
        StringBuilder stringBuilder = new StringBuilder();
        if (CommonUtil.isNotNull(queryList)) {
            String[] split = queryList.trim().split(",");
            if (split.length > 0) {
                for (int i = 0; i < split.length; i++) {
                    if (i == split.length - 1) {
                        stringBuilder.append(split[i]);
                    } else {
                        if (!split[i].equals("")) {
                            stringBuilder.append(split[i]).append("$");
                        }
                    }
                }
            }
        }
        String query_str = stringBuilder.toString();
        String url = "http://api.map.baidu.com/place/v2/search?query=" + query_str + "&location=" + lng + "," + lat + "&radius=" + radius + "&output=json&ak=" + ak;  //GET请求
        String json = open_url_get(url);
        return JSON.parseObject(json, BaiduAreaSearch.class);
    }

    /**
     * 地址转经纬度
     * http://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation //GET请求
     *
     * @param address 地址
     * @return AddressToLatAndLng
     */
    public AddressToLatAndLng address_to_lat_lng(String address) {
        String url = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + ak;
        String json = open_url_get(url);
        return JSON.parseObject(json, AddressToLatAndLng.class);
    }

    /**
     * 经纬度转地址
     *
     * @param lat 纬度
     * @param lng 经度
     * @return LatAndLngToAddress
     */
    public LatAndLngToAddress lat_lng_to_address(String lat, String lng) {
        String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=" + ak + "&output=json&coordtype=wgs84ll&location=" + lat + "," + lng;
        String json = open_url_get(url);
        return JSON.parseObject(json, LatAndLngToAddress.class);
    }

    /**
     * 天气
     *
     * @param location 地址
     * @return Weather
     */
    public Weather.HeWeather6Bean weather(String location) {
        String url = "https://free-api.heweather.net/s6/weather/now?location=" + location + "&key=" + ak;
        Weather weather = JSON.parseObject(open_url_get(url), Weather.class);
        return weather.getHeWeather6().get(0);
    }

    /**
     * 驾车路线规划
     *
     * @param origin      起点 经纬度
     * @param destination 终点 经纬度
     */
    public DirectionLite directionLite(String origin, String destination) {
        String url = "http://api.map.baidu.com/directionlite/v1/driving?origin=" + origin + "&destination=" + destination + "&ak=" + ak;
        return JSON.parseObject(open_url_get(url), DirectionLite.class);
    }

    public DirectionLite addressTodirectionLite(String beginAddress, String endAddress) {
        AddressToLatAndLng var1 = this.address_to_lat_lng(beginAddress);
        AddressToLatAndLng var2 = this.address_to_lat_lng(endAddress);

        String origin = null;
        String destination = null;
        //开始的纬度,经度
        if (var1 != null) {
            String lng = String.valueOf(var1.getResult().getLocation().getLng());
            String lat = String.valueOf(var1.getResult().getLocation().getLat());
            origin = lat + "," + lng;

        }
        //终点的纬度,经度
        if (var2 != null) {
            String lng = String.valueOf(var2.getResult().getLocation().getLng());
            String lat = String.valueOf(var2.getResult().getLocation().getLat());
            destination = lat + "," + lng;
        }

        if (origin != null && destination != null) {
            return this.directionLite(origin,destination);
        }

        return null;
    }

    private String open_url_get(String url) {
        return this.restTemplate.getForObject(url, String.class);
    }
}
