package com.palmnest.application.core.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Encoded;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.wsdl.http.UrlEncoded;
import org.apache.log4j.Logger;

import com.palmnest.application.bean.RoleFloorTypeInfo;

public class HttpMesssageKit {

	private static final String DEFAULT_CHARSET = "UTF-8";
	private static       Logger LOGGER          = Logger.getLogger(HttpMesssageKit.class);
	private static       String smsServer       = PropertiesKit.readPropertiesValue("smsSunMei_server", "wechat.properties");
	private static       String password        = PropertiesKit.readPropertiesValue("smsSunMei_password", "wechat.properties");
	private static       String username        = PropertiesKit.readPropertiesValue("smsSunMei_username", "wechat.properties");

	/**
	 * 发送短信方法
	 *
	 * @param userMobile
	 * @param content
	 * @return
	 */
	public static String smsSend(String userMobile, String content) {
		String result = "";
		try {
			String data = MD5.sign(password, "", DEFAULT_CHARSET);
			String postSend = MD5.sign(username, data, DEFAULT_CHARSET);
			StringBuffer params = new StringBuffer();
			params.append("username=" + username);
			params.append("&password=" + postSend);
			params.append("&mobile=" + userMobile);
			content = content + "【尚美集团】";
			params.append("&content=" + content);
			result = HttpKit.Post(params.toString(), smsServer);
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
		return result;
	}


	public static void main(String[] args) {
//    	String mobile="13924650526,13066855659";
//    	HttpMesssageKit kit=new HttpMesssageKit();
//    	String result=kit.smsSend(mobile, "您好，本人发送一份测试数据给您，请您注意查收！");
//    	System.out.println("输出结果："+result);
//    	String path="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe1bf4e6c174f6707&redirect_uri=http%3A%2F%2Fsm.avic-s.com%2Fsunmei_realty%2Fwechat%2FfrontSecurity%2Fauth%3FredirectUrl%3Dhttp%253A%252F%252Fsm.avic-s.com%252Fwechat%252Fdist%252Findex.html&response_type=code&scope=snsapi_base&state=5#wechat_redirect";
//    	String url=kit.setPathByEncode(path, "sm.avic-s.com/sunmei_realty","5", "wxe1bf4e6c174f6707","sm.avic-s.com");
//    	String url=kit.setPathByEncode(path, "wx.palmnest.com/sunmei_realty","1", "wxffe70d6b42696497");

//    	System.out.println("\n"+url);
//    	String url=kit.getPathByAPI_SECRET("");
//    	System.out.println(url);
		Map<String, RoleFloorTypeInfo> map = new HashMap<String, RoleFloorTypeInfo>();
		RoleFloorTypeInfo info = new RoleFloorTypeInfo();
		List<Object> list1 = new ArrayList<Object>();
		list1.add("1");
		list1.add("2");
		list1.add("3");
		info.setFloorOrRepairType(list1);
		info.setType(1);
		map.put("9060", info);
		RoleFloorTypeInfo info2 = new RoleFloorTypeInfo();
		List<Object> list2 = new ArrayList<Object>();
		list2.add("4");
		list2.add("5");
		list2.add("6");
		info2.setType(1);
		info2.setFloorOrRepairType(list2);
		map.put("9070", info2);
		RoleFloorTypeInfo info3 = new RoleFloorTypeInfo();
		List<Object> list3 = new ArrayList<Object>();
		list3.add("269");
		list3.add("272");
		info3.setFloorOrRepairType(list3);
		info3.setType(2);
		map.put("9062", info3);
		System.out.println(map);

		JSONObject jsonObject = JSONObject.fromObject(map);
		System.out.println(jsonObject);
	}


	public static String getPathByAPI_SECRET(String user_code, String user_mobile) {
		String path = "http://wxapp.etong-online.com/epn/admin/api/cjqt_user_login.php";
		String project_code = "PJ1805251017042636";
		String API_SECRET = "NelMEk2txvs4WEmp";
		if (StringUtils.isEmpty(user_code)) {
			user_code = "user_codeisEmpty";
		}
		String token = "";
		long time = System.currentTimeMillis();
		String time_stamp = String.valueOf(time);
		token = MD5.sign(project_code + user_code + time_stamp + API_SECRET, "", DEFAULT_CHARSET);
		StringBuilder sb = new StringBuilder();
		sb.append("?project_code=" + project_code);
		sb.append("&user_code=" + user_code);
		sb.append("&user_mobile=" + user_mobile);
		sb.append("&time_stamp=" + time_stamp);
		sb.append("&token=" + token);
		String url = path + sb.toString();
		return url;

	}
    
    /*public static String setPathByEncode(String path,String pathEnd,String state,String appId){
    	StringBuilder str=new StringBuilder("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri=");
    	String pathUrl="";
    	String str1="&redirect_uri=";
    	String str2="&response_type=";
    	int index=path.indexOf(str1);
    	int index2=path.indexOf(str2);
    	path=path.substring(index+(str1.length()),index2);
    	System.out.println(path);
    	try {
			String url=URLDecoder.decode(URLDecoder.decode(path, "UTF-8"),"UTF-8");
			String url1=url.substring(("http://".length()));
			url1.indexOf("/", url1.indexOf("/")+1);
			String path1=url1.substring(0,url1.indexOf("/", url1.indexOf("/")+1));
			//path1="sm.avic-s.com/sunmei_realty";
			System.out.println("path1:==="+path1);
			System.out.println("url1==="+url1);
			String path3="http://"+pathEnd+url1.substring(path1.length());
			System.out.println("path3:"==path3);
			pathUrl=URLEncoder.encode(path3,"UTF-8");
			System.out.println(pathUrl);
			str.append(pathUrl);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	str.append("&response_type=code&scope=snsapi_base&state="+state+"#wechat_redirect");
    	return str.toString();
    }*/

	public static String setPathByEncode(String path, String pathEnd, String state, String appId, String end) {
		StringBuilder str = new StringBuilder("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=");
		String url2 = "";
		String str1 = "&redirect_uri=";
		String str2 = "&response_type=";
		int index = path.indexOf(str1);
		int index2 = path.indexOf(str2);
		path = path.substring(index + (str1.length()), index2);
		System.out.println(path);
		try {
			String url = URLDecoder.decode(URLDecoder.decode(path, "UTF-8"), "UTF-8");
			String url1 = url.substring(("http://".length()));
			//url1.indexOf("/", url1.indexOf("/")+1);
			System.out.println("url1:" + url1);
			///wechat/postoffice/index.html 最重要一部分 其他都拼
			//可以加一个方法直接传 url2After
			String url2After = "/avicfile" + url1.substring(url1.lastIndexOf(end) + end.length(), url1.length());
			System.out.println(url2After);
			String redirectUrl = URLEncoder.encode("http://" + pathEnd.split("/")[0] + url2After, "UTF-8");
			url2 = "http://" + pathEnd + "/wechat/frontSecurity/auth?redirectUrl=" + redirectUrl;
			url2 = URLEncoder.encode(url2, "UTF-8");

			str.append(url2);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url3 = "&response_type=code&scope=snsapi_base&state=" + state + "#wechat_redirect";
		str.append(url3);
		return str.toString();
	}

}
