package com.qunzhixinxi.hnqz.admin.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static com.qunzhixinxi.hnqz.admin.util.HttpRequest.sendGet;
import static com.qunzhixinxi.hnqz.admin.util.HttpRequest.sendPost;

public class MapUtil {

	private static String key = "8ef7f492add60ae866606e6928ed2576";

	/**
	 * 阿里云api 根据经纬度获取地址
	 *
	 * @param log
	 * @param lat
	 * @return
	 */
	public static String getAdd(String log, String lat) {
		StringBuffer s = new StringBuffer();
		s.append("key=").append(key).append("&location=").append(log).append(",").append(lat);
		String res = sendPost("http://restapi.amap.com/v3/geocode/regeo", s.toString());
		System.out.print(res);
		JSONObject jsonObject = JSONObject.parseObject(res);
		JSONObject jsonObject1 = jsonObject.getJSONObject("regeocode");
		String add = jsonObject1.get("formatted_address").toString();
		return add;
	}

	/**
	 * 阿里云api 根据经纬度获取所在城市
	 *
	 * @param log
	 * @param lat
	 * @return
	 */
	public static String getCity(String log, String lat) {
		// log 大 lat 小
		// 参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)
		String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010";
		String res = "";
		try {
			URL url = new URL(urlString);
			java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream
					(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				res += line + "\n";
			}
			in.close();
			JSONObject jsonObject = JSONObject.parseObject(res);
			JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));
			JSONObject j_2 = JSONObject.parseObject(jsonArray.get(0).toString());
			String allAdd = j_2.getString("admName");
			String arr[] = allAdd.split(",");
			res = arr[1];
		} catch (Exception e) {
			System.out.print("error in wapaction,and e is " + e.getMessage());
		}
		System.out.print(res);
		return res;
	}

	/**
	 * 高德api 根据地址获取经纬度
	 *
	 * @param name
	 * @return
	 */
	public static JSONObject getLatAndLogByName(String name) {
		StringBuffer s = new StringBuffer();
		s.append("key=" + key + "&address=" + name);
		String res = sendPost("http://restapi.amap.com/v3/geocode/geo", s.toString());
		JSONObject jsonObject = JSONObject.parseObject(res);
		JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("geocodes"));
		if(null==jsonArray||jsonArray.size()==0)
		{
			return null;
		}
		JSONObject location = (JSONObject) jsonArray.get(0);
//		String add = location.get("location").toString();
		return location;
	}

	/**
	 * 高德api 根据地址获取经纬度
	 *
	 * @param name
	 * @return
	 */
	public static String getAddByAMAP(String log, String lat) {
		StringBuffer s = new StringBuffer();
		s.append("key=").append(key).append("&location=").append(log).append(",").append(lat);
		String res = sendPost("http://restapi.amap.com/v3/geocode/regeo", s.toString());
		JSONObject jsonObject = JSONObject.parseObject(res);
		JSONObject jsonObject1 = jsonObject.getJSONObject("regeocode");
		String add = jsonObject1.get("formatted_address").toString();
		return add;
	}


	/**
	 * 高德api 坐标转换---转换至高德经纬度
	 *
	 * @param name
	 * @return
	 */
	public static String convertLocations(String log, String lat, String type) {
		StringBuffer s = new StringBuffer();
		s.append("key=").append(key).append("&locations=").append(log).append(",").append(lat).append("&coordsys=");
		if (type == null) {
			s.append("gps");
		} else {
			s.append(type);
		}
		String res = sendPost("http://restapi.amap.com/v3/assistant/coordinate/convert", s.toString());
		System.out.print(res);
		JSONObject jsonObject = JSONObject.parseObject(res);
		String add = jsonObject.get("locations").toString();
		return add;
	}


	public static String getAddByName(String name) {
		// log 大 lat 小
		// 参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)
		String urlString = "http://gc.ditu.aliyun.com/geocoding?a=" + name;
		String res = "";
		try {
			URL url = new URL(urlString);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				res += line + "\n";
			}
			in.close();
			JSONObject jsonObject = JSONObject.parseObject(res);
			String lon = jsonObject.getString("lon");
			String lat = jsonObject.getString("lat");
			System.err.println(jsonObject);
			res = getNearbyAdd(lon, lat);
		} catch (Exception e) {
			System.out.print("error in wapaction,and e is " + e.getMessage());
			e.printStackTrace();
		}
		return res;
	}

	public static String getNearbyAdd(String log, String lat) {

		String add = sendGet("http://ditu.amap.com/service/regeo", "longitude=" + log + "&latitude=" + lat +
				"&type=010");

		System.out.print(add);

		return add;
	}

	/**
	 * 高德api 关键字模糊查询
	 *
	 * @param keyWord
	 * @param city
	 * @return
	 */
	public static String getKeywordsAddByLbs(String keyWord, String city) {
		StringBuffer s = new StringBuffer();
		s.append("key=" + key + "&keywords=");
		if (keyWord.contains(" ")) {
			String[] str = keyWord.split(" ");
			for (int i = 0; i < str.length; i++) {
				if (i == 0) {
					s.append(str[i]);
				} else {
					s.append("+" + str[i]);
				}
			}
		} else {
			s.append(keyWord);
		}
		s.append("&city=" + city);
		s.append("offset=10&page=1");
		String around = sendPost("http://restapi.amap.com/v3/place/text", s.toString());
		System.out.print(around);
		return around;
	}
	/**
	 * 高德api 经纬度/关键字 附近地标建筑及地点查询
	 *
	 * @param log
	 * @param lat
	 * @param keyWord
	 * @return
	 */
	public static String getAroundAddByLbs(String log, String lat, String keyWord) {
		String around = sendPost("http://restapi.amap.com/v3/place/around",
				"key=" + key + "&location=" + log + "," + lat + "&keywords=" + keyWord +
						"&radius=2000&offset=10&page=1");
		System.out.print(around);
		return around;
	}
//
//	public static void main(String args [] )
//	{
//		JSONObject jsonObject=getLatAndLogByName("绩溪县北大街82号");
//		System.out.println(jsonObject.toJSONString());
//	}
}
