package com.platform.modules.weixin.token;

import java.util.Map;

import com.platform.common.utils.RedisUtils;
import com.platform.modules.weixin.kit.HttpKit;
import com.platform.modules.weixin.kit.ParaMap;
import com.platform.modules.weixin.kit.StringKit;

/**
 * 认证并获取 access_token API
 * http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token
 */
public class AccessTokenApi {

	private static String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
	
	private static AccessToken accessToken;
	
	public static String WX_ACCESS_TOKEN = "wx_access_token";
	
	public static String WX_ACCESS_TOKEN_END = "access_token";
	
	private String appId;
	
	private String appSecret;
	
	private RedisUtils redisUtils;
	

	public AccessTokenApi(String appId,String appSecret,RedisUtils redisUtils) {
		this.appId = appId;
		this.appSecret = appSecret;
		this.redisUtils = redisUtils;
	}
	
	public AccessToken getAccessToken(){
		
		if(accessToken != null && accessToken.isAvailable()){
			return accessToken;
		}
		refreshAccessToken();
		return accessToken;
	}
	
	public void refreshAccessToken(){
		accessToken = requestAccessToken();
	}
	
	private synchronized AccessToken requestAccessToken() {
		
		AccessToken result = null;
		for (int i = 0; i < 3; i++){
			Map<String, String> queryParas = ParaMap.create("appid", appId).put("secret", appSecret).getData();
			String json = HttpKit.get(url, queryParas);
			result = new AccessToken(json);
			
			if (result.isAvailable())
				break;
		}
		return result;
	}
	
	public String getAccessTokenFromRedis() {
		
		String result = redisUtils.get(WX_ACCESS_TOKEN);
		if(!StringKit.isBlank(result))
			return result;
		
		return refreshRedisAccessToken();
	}
	
	public String getAccessTokenFromRedis(String appId, String appSecret, String redisAppend) {
		
		String result = redisUtils.get(redisAppend + WX_ACCESS_TOKEN_END);
		if(!StringKit.isBlank(result))
			return result;
		
		return refreshRedisAccessToken( appId, appSecret, redisAppend);
	}
	
	public String refreshRedisAccessToken(){
		return requestAccessTokenToRedis();
	}
	
	public String refreshRedisAccessToken(String appId, String appSecret, String redisAppend){
		return requestAccessTokenToRedis(appId, appSecret, redisAppend);
	}
	
	private synchronized String requestAccessTokenToRedis(String appId, String appSecret, String redisAppend) {
		
		AccessToken result = null;
		for (int i = 0; i < 3; i++){
			Map<String, String> queryParas = ParaMap.create("appid", appId).put("secret", appSecret).getData();
			String json = HttpKit.get(url, queryParas);
			result = new AccessToken(json);
			System.out.println("refresh token");
			System.out.println(json);
			if (result.getExpiresin() != null && result.getExpiresin() > 0 && StringKit.notBlank(result.getAccessToken())) {
//				jedis.setex(redisAppend + WX_ACCESS_TOKEN_END, result.getExpiresin() / 2, result.getAccessToken());
				redisUtils.set(redisAppend + WX_ACCESS_TOKEN_END, result.getAccessToken(), result.getExpiresin() / 2);
				return result.getAccessToken();
			}
		}
		
		return null;
	}
	
	private synchronized String requestAccessTokenToRedis() {
		
		AccessToken result = null;
		for (int i = 0; i < 3; i++){
			Map<String, String> queryParas = ParaMap.create("appid", appId).put("secret", appSecret).getData();
			String json = HttpKit.get(url, queryParas);
			result = new AccessToken(json);
			System.out.println("refresh token");
			System.out.println(json);
			if (result.getExpiresin() != null && result.getExpiresin() > 0 && StringKit.notBlank(result.getAccessToken())) {
//				jedis.setex(WX_ACCESS_TOKEN, result.getExpiresin() / 2, result.getAccessToken());
				redisUtils.set(WX_ACCESS_TOKEN, result.getAccessToken(), result.getExpiresin() / 2);
				return result.getAccessToken();
			}
		}
		
		return null;
	}
	
	public static void main(String[] args) {
//		Map<String, String> queryParas = ParaMap.create("appid","wxde8efd6a63b205a1").put("secret", "d4624c36b6795d1d99dcf0547af5443d").getData();
//		String json = HttpKit.get(url, queryParas);
//		System.out.println(json);
//		JSONObject jsonObject = JSONObject.fromObject(json);
//		String data = jsonObject.getString("access_token");
//		String menu = HttpKit.get("https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+data);
//		System.out.println(menu);
//		ParaMap pm = ParaMap.create("access_token",data).put("openid", "oiRXb0wpV_juyJDaL9vnjgUpo8mM").put("lang", "zh_CN");
//		return new ApiResult(HttpKit.get(getUserInfo, pm.getData()));
//		ApiResult result = new ApiResult(HttpKit.get("https://api.weixin.qq.com/cgi-bin/user/info", pm.getData()));
//		if(result.isSucceed()){
//			System.out.println(result);
//		} else {
//			System.out.println(result.getErrorMsg());
//		}
//		ApiResult result = MediaApi.batchget("news", 0, 2);
//		String result = AccessTokenApi.getAccessTokenFromRedis();
//		System.out.println(result);
	}
}
