package com.yingying.tourist.common;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class UrlUtils {

	public static String buildUrlNormal(String url,Map<String,String> params) throws Exception{
		try {
			StringBuilder sb = new StringBuilder();
			sb.append(url);
			if (params != null && !params.isEmpty()) {
				sb.append("?");
				for (Entry<String, String> entry : params.entrySet()) {
					String key = entry.getKey();
					String value = entry.getValue();
					sb.append(key);
					sb.append("=");
					sb.append(value);
					sb.append("&");
				}
			}
			return sb.toString();
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public static String buildUrl(String url,Map<String,String> params) throws Exception{
		try {
			StringBuilder sb = new StringBuilder();
			sb.append(url);
			if (params != null && !params.isEmpty()) {
				sb.append("?");
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				for (Entry<String, String> entry : params.entrySet()) {
					String key = entry.getKey();
					String value = entry.getValue();
					nameValuePairs.add(new BasicNameValuePair(key, value));
				}
				sb.append(EntityUtils.toString(new UrlEncodedFormEntity(
						nameValuePairs)));
			}
			return sb.toString();
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public static String buildParams(Map<String,String> params) throws Exception{
		try {
			if (params != null && !params.isEmpty()) {
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				for (Entry<String, String> entry : params.entrySet()) {
					String key = entry.getKey();
					String value = entry.getValue();
					nameValuePairs.add(new BasicNameValuePair(key, value));
				}
				return EntityUtils.toString(new UrlEncodedFormEntity(
						nameValuePairs));
			}
			return "";
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public static String getRequestPayload(HttpServletRequest req) {
		StringBuilder sb = new StringBuilder();
		if (req.getContentType() != null && req.getContentType().contains("application/json")) {
			try {
				BufferedReader reader = req.getReader();
				char[] buff = new char[1024];
				int len;
				while ((len = reader.read(buff)) != -1) {
					sb.append(buff, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	@SuppressWarnings("rawtypes")
	public static String getRequestParams(HttpServletRequest req) {
		StringBuilder sb = new StringBuilder();
		Enumeration parameterNames = req.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String name = (String) parameterNames.nextElement();
			String[] values = req.getParameterValues(name);
			sb.append(name).append("=");
			if (values != null) {
				for (String v : values) {
					sb.append(v).append("&");
				}
			}
		}
		return sb.toString();
	}
	
}
