package com.ym.mec.student.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONPath;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.redis.service.RedisCache;
import com.ym.mec.util.http.HttpUtil;
import com.ym.mec.util.string.ValueUtil;

@RequestMapping("wechat")
@Api(tags = "微信")
@RestController
public class WechatController extends BaseController {

	private String appId = "wx80f175c0eb6836e9";

	private String secret = "a9d779747dba9f4e82f19882debe3e93";
	
	private final static String WX_ACCESS_TOKEN = "WX_ACCESS_TOKEN";

    @Autowired
    private RedisCache<String, Object> redisCache;

	@ApiOperation("获取签名")
	@GetMapping(value = "/getSignature")
	public Object getSignature(String url) throws Exception {
		
		url = URLDecoder.decode(url);

		String accessToken = getAccessToken(appId, secret);

		String ticket = getTicket(accessToken);

		String noncestr = UUID.randomUUID().toString();
		String timestamp = System.currentTimeMillis() + "";

		SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
		parameterMap.put("noncestr", noncestr);
		parameterMap.put("jsapi_ticket", ticket);
		parameterMap.put("timestamp", timestamp);
		parameterMap.put("url", url);

		String str = ValueUtil.joinKeyValue(parameterMap, "", "", "&", false, "");

		String signature = DigestUtils.sha1Hex(str);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("appId", appId);
		result.put("timestamp", timestamp);
		result.put("nonceStr", noncestr);
		result.put("signature", signature);

		return succeed(result);
	}

	private String getAccessToken(String appid, String secret) throws IOException {

		if (redisCache.exists(WX_ACCESS_TOKEN)) {
			if (redisCache.get(WX_ACCESS_TOKEN) != null) {
				if (StringUtils.isNotEmpty(redisCache.get(WX_ACCESS_TOKEN).toString())) {
					return redisCache.get(WX_ACCESS_TOKEN).toString();
				}
			}
		}
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("grant_type", "client_credential");
		parameterMap.put("appid", appid);
		parameterMap.put("secret", secret);

		String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", parameterMap);
		String accessToken = (String) JSONPath.extract(result, "$.access_token");
		
		redisCache.put(WX_ACCESS_TOKEN, accessToken, 7100);

		return accessToken;
	}

	private String getTicket(String accessToken) throws IOException {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("access_token", accessToken);
		parameterMap.put("type", "jsapi");
		String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket", parameterMap);

		return (String) JSONPath.extract(result, "$.ticket");
	}
	
}
