package com.sino.publicclass.util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;

public class DecodeAddress {
	private static Log log = LogFactory.getLog(DecodeAddress.class);
	
	public static String getAddressName(double lon,double lat){
		String address = "";
		String a_k = "c82012c4404abebdb9d8f2978db2a37d81cdfde1ee06475c4a0261f8cce311c9e6e78d14bf99d397";
		String urlString = null;
		String spXml = null;
		String postParam = null;
		try {
			if (lon>0 && lon<180 && lat>0 && lat<180) {
				spXml = "<?xml version=\"1.0\" encoding=\"GB2312\"?><spatial_request method=\"searchPoint\"><x>"
						+ String.valueOf(lon)
						+ "</x><y>"
						+ String.valueOf(lat)
						+ "</y><poiNumber>10</poiNumber><range>300</range><pattern>0</pattern><roadLevel>0</roadLevel></spatial_request>";

				spXml = URLEncoder.encode(spXml, "GB2312");
				postParam = "highLight=false&config=SPAS&ver=2.0&resType=json&enc=GB2312&a_k="+ a_k + "&spatialXml=" + spXml;

				urlString = "http://search1.mapabc.com/sisserver";

				URL url = new URL(urlString);
				URLConnection urlc = url.openConnection();
				HttpURLConnection httpConnection = (HttpURLConnection) urlc;
				httpConnection.setRequestMethod("POST");
				httpConnection.setDoInput(true);
				httpConnection.setDoOutput(true);
				httpConnection.connect();

				OutputStreamWriter osw = new OutputStreamWriter(
						new BufferedOutputStream(
								httpConnection.getOutputStream()));
				osw.write(postParam);
				osw.flush();
				osw.close();

				BufferedReader br = new BufferedReader(
						new InputStreamReader(
								httpConnection.getInputStream()));
				String line = null;
				StringBuffer sb = new StringBuffer("");
				while ((line = br.readLine()) != null)
					sb.append(line);
				httpConnection.disconnect();
               //System.out.println(sb.toString());
				address = parseMessage(sb.toString());
			}
		} catch (Exception ee) {
			log.error("exception", ee);
		}
		return address;
	}
	
	private static String parseMessage(String message){
		String addressName = "";
		Map<String,String> crossMap = null;
		Map<String,String> roadMap = null;
		Map<String,String> poiMap = null;
		
		Map map = (Map) JSON.parse(message);
		Map spatialMap=(Map)map.get("SpatialBean");
		List crossList=(List)spatialMap.get("crossPoiList");
		if(crossList.size()>0){
			crossMap=new HashMap<String,String>();
			String crossName=(String)((Map)crossList.get(0)).get("name");
			crossMap.put("name", crossName);
		}
		
		List roadList = (List)spatialMap.get("roadList");
		if(roadList.size()>0){
			roadMap = new HashMap<String,String>();
			String roadName=(String)((Map)roadList.get(0)).get("name");
			roadMap.put("name", roadName);
		}
		
		List poiList = (List)spatialMap.get("poiList");
		if(poiList.size()>0){
			Map pMap = (Map)poiList.get(0);
			String poiName=(String)pMap.get("name");
			String address = (String)pMap.get("address");
			String tel = (String)pMap.get("tel");
			poiMap = new HashMap<String,String>();
			poiMap.put("name", poiName);
			poiMap.put("address", address);
			poiMap.put("tel", tel);
		}
		
		Map provinceMap=(Map)spatialMap.get("Province");
		String provinceName = (String)provinceMap.get("name");
		Map areaMap=(Map)spatialMap.get("District");
		String areaName = (String)areaMap.get("name");
		
		if(poiMap!=null && !"".equals(poiMap.get("name")) ){
			addressName = poiMap.get("address")+" " +poiMap.get("name");
		}else if(crossMap!=null && !"".equals(crossMap.get("name"))){
			addressName = crossMap.get("name");
		}else if(roadMap!=null && !"".equals(roadMap.get("name"))){
			addressName = roadMap.get("name");
		}
        return provinceName+areaName+addressName;
	}
	

	
	public static void main(String[] args){
		//SpatialInfoCenter.getSpatialInfo("121.4614627778", "31.2294658333");
		String info = DecodeAddress.getAddressName(121.4614627778, 31.2294658333);
		 java.net.URLEncoder.encode("");
		System.out.println("info="+info);
	}

}
