package com.zhiwei.weixin.util;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhiwei.weixin.http.HttpRequest;
import com.zhiwei.weixin.pojo.AccessToken;
import com.zhiwei.weixin.pojo.Menu;
import com.zhiwei.weixin.pojo.TicketToken;

/**
 * 公众平台通用接口工具类
 * 
 * @author liuyq
 * @date 2013-08-09
 */
public class WeixinUtil {
	private static Logger log = LoggerFactory.getLogger(WeixinUtil.class);
	// 获取access_token的接口地址（GET） 限200（次/天）
	public final static String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	
	//获取js接口所需要的jsapi_ticket的接口地址
	public final static String jsapi_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
	
	//获取卡卷的token
	public final static String js_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=wx_card";
	
	// 菜单创建（POST） 限100（次/天）
	public final static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	
	
	/**
	 * 获取access_token
	 * 
	 * @param appid
	 *            凭证
	 * @param appsecret
	 *            密钥
	 * @return
	 */
	public static AccessToken getAccessToken(String appid, String appsecret) {
		AccessToken accessToken = null;

		String requestUrl = access_token_url.replace("APPID", appid).replace(
				"APPSECRET", appsecret);
		
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "GET", null);
		// 如果请求成功
		if (null != jsonObject) {
			accessToken = new AccessToken();
			accessToken.setToken(jsonObject.getString("access_token"));
			accessToken.setExpiresIn(System.currentTimeMillis());
		}
		return accessToken;
	}
	
	/**
	 * @deprecated:获取jsapi_ticket
	 * @param AccessToken at
	 * @retuen
	 * 
	 * **/
	public static TicketToken getJSTicket(String accessToken)
	{
		String requestUrl = jsapi_ticket_url.replace("ACCESS_TOKEN", accessToken);
		
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "GET", null);
		// 如果请求成功
		if (null != jsonObject) 
		{
			System.out.println("getJSTicket:"+jsonObject.getString("ticket"));
			TicketToken ticket = new TicketToken();
			ticket.setJsticket(jsonObject.getString("ticket"));
			ticket.setExpiresIn(System.currentTimeMillis());
			return ticket;
		}
		
		return null;
	}
	

	/**
	 * 创建菜单
	 * 
	 * @param menu
	 *            菜单实例
	 * @param accessToken
	 *            有效的access_token
	 * @return 0表示成功，其他值表示失败
	 */
	public static int createMenu(Menu menu, String accessToken) {
		int result = 0;

		// 拼装创建菜单的url
		String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
		// 将菜单对象转换成json字符串
		String jsonMenu = JSONObject.fromObject(menu).toString();
		System.out.println(jsonMenu);
		// 调用接口创建菜单
		JSONObject jsonObject = HttpRequest.httpRequest(url, "POST", jsonMenu);

		if (null != jsonObject) {
			if (0 != jsonObject.getInt("errcode")) {
				result = jsonObject.getInt("errcode");
				log.error("创建菜单失败 errcode:{} errmsg:{}",
						jsonObject.getInt("errcode"),
						jsonObject.getString("errmsg"));
			}
		}
		return result;
	}
	
	
	/**
	 * 获取js_ticket
	 * @return String 
	 * */
	public static TicketToken getTicket(String accessToken)
	{
		String requestUrl = js_ticket_url.replace("ACCESS_TOKEN", accessToken);
		
		JSONObject jsonObject = HttpRequest.httpRequest(requestUrl, "GET", null);
		// 如果请求成功
		if (null != jsonObject) 
		{
			System.out.println("getJSTicket:"+jsonObject.getString("ticket"));
			TicketToken ticket = new TicketToken();
			ticket.setJsticket(jsonObject.getString("ticket"));
			ticket.setExpiresIn(System.currentTimeMillis());
			return ticket;
		}
		
		return null;
	}
}
