package com.platform.modules.weixin.token;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

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;

/**
 * 认证并获取 jsapi_ticket API
 * https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi 
 */
public class JsTicketApi {
	
	private static String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
	
	private static JsTicket jsTicket;
	
	public static String WX_JS_TICKET = "wx_js_ticket";
	
	public static String WX_JS_TICKET_END = "js_ticket";
	
	private String appId;
	
	private String appSecret;
	
	private RedisUtils redisUtils;
	

	public JsTicketApi(String appId,String appSecret,RedisUtils redisUtils) {
		this.appId = appId;
		this.appSecret = appSecret;
		this.redisUtils = redisUtils;
	}
	public JsTicket getJsTicket() {
		if (jsTicket != null && jsTicket.isAvailable())
			return jsTicket;
		
		refreshJsTicket();
		return jsTicket;
	}
	
	public String getJsTicketFromRedis() {
		String result = redisUtils.get(WX_JS_TICKET);
		if(!StringKit.isBlank(result))
			return result;
		
		return refreshRedisJsTicket();
	}
	
	public String getJsTicketFromRedis(String appId, String appSecret, String redisAppend) {
		
		String result = redisUtils.get(redisAppend + WX_JS_TICKET_END);
		if(!StringKit.isBlank(result))
			return result;
		
		result = refreshRedisJsTicket(appId, appSecret, redisAppend);
		
		return result;
	}
	
	public String refreshRedisJsTicket(){
		return requestJsTicketToRedis();
	}
	
	public String refreshRedisJsTicket(String appId, String appSecret, String redisAppend){
		return requestJsTicketToRedis(appId, appSecret, redisAppend);
	}
	
	private synchronized String requestJsTicketToRedis() {
		
		JsTicket result = null;
		for (int i = 0; i < 3; i++){
			AccessTokenApi accessTokenApi = new AccessTokenApi(appId, appSecret, redisUtils);
			String access_token = accessTokenApi.getAccessTokenFromRedis();
			if(StringUtils.isBlank(access_token)){
				continue;
			}
			Map<String, String> queryParas = ParaMap.create("access_token", access_token).put("type", "jsapi").getData();
			String json = HttpKit.get(url, queryParas);
			result = new JsTicket(json);
			System.out.println("refresh jsTicket");
			if (result.getExpiresIn() != null && result.getExpiresIn() > 0 && StringKit.notBlank(result.getTicket())) {
//				jedis.setex(WX_JS_TICKET, result.getExpiresIn() / 2, result.getTicket());
				redisUtils.set(WX_JS_TICKET, result.getTicket(), result.getExpiresIn() / 2);
				return result.getTicket();
			}
		}
		return null;
	}
	
	private synchronized String requestJsTicketToRedis(String appId, String appSecret, String redisAppend) {
		
		JsTicket result = null;
		for (int i = 0; i < 3; i++){
			AccessTokenApi accessTokenApi = new AccessTokenApi(appId, appSecret, redisUtils);
			String access_token = accessTokenApi.getAccessTokenFromRedis();
			Map<String, String> queryParas = ParaMap.create("access_token", access_token).put("type", "jsapi").getData();
			String json = HttpKit.get(url, queryParas);
			result = new JsTicket(json);
			if (result.getExpiresIn() != null && result.getExpiresIn() > 0 && StringKit.notBlank(result.getTicket())) {
				redisUtils.set(redisAppend + WX_JS_TICKET_END, result.getTicket(), result.getExpiresIn() / 2);
				return result.getTicket();
			}
		}
		return null;
	}

	public void refreshJsTicket() {
		jsTicket = requestJsTicket();
	}
	
	private synchronized JsTicket requestJsTicket() {
		JsTicket result = null;
		for (int i=0; i<3; i++) {
			
			AccessTokenApi accessTokenApi = new AccessTokenApi(appId, appSecret, redisUtils);
			String access_token = accessTokenApi.getAccessTokenFromRedis();
			Map<String, String> queryParas = ParaMap.create("access_token", access_token).put("type", "jsapi").getData();
			String json = HttpKit.get(url, queryParas);
			result = new JsTicket(json);
			
			if (result.isAvailable())
				break;
		}
		return result;
	}
	public static void main(String[] args) {
//		String result = JsTicketApi.getJsTicketFromRedis();
//		System.out.println(result);
	}
}
