package me.zhengjie.utils;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@Component
@RequiredArgsConstructor
public class WxUtil {

    private final RedisUtils redisUtils;

    /** TokenURL */
    private final static String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    /** TicketUrl */
    private final static String GET_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";

    /** APP_ID */
    private final static String APP_ID = "wxd64360a4b8c50006";

    /** APP_SECRET */
    private final static String APP_SECRET = "b758e45c89162542610509dafd9db7c3";


    /**
     * 获取token，默认7200秒过期，所以存redis7200秒取一次
     *
     * @return token
     */
    public Object getToken() {
        String redisKey = "Tencent:AccessToken";
        Object token  = redisUtils.get(redisKey);
        if(ObjectUtils.isEmpty(token)){
            token = token();
            //设置缓存 2小时
            redisUtils.set(redisKey,token, 7200, TimeUnit.SECONDS);
            log.info("result:redisToken {}",redisUtils.get(redisKey));
        }
        return token;
    }


    public String token(){
        String result = HttpRequest.get(GET_TOKEN_URL+"&appid="+APP_ID+"&secret="+APP_SECRET).execute().body();
        log.info("result:token "+result);
        if (!StringUtils.isNotEmpty(result)) {
            return null;
        }
        JSONObject object = JSONObject.parseObject(result);
        return ObjectUtils.isEmpty(object) ? null : object.getString("access_token");
    }

    //getResponseJson();
    private JSONObject getResponseJson(HttpResponse response) throws IOException {
        JSONObject json = null;
        HttpEntity entity = response.getEntity();
        if(entity!=null){
            String result = EntityUtils.toString(entity,"UTF-8");
            json = JSONObject.parseObject(result);
        }
        if(ObjectUtils.isEmpty(json)){
            return null;
        }
        return json;
    }

    /**
     * 获取ticket，默认7200秒过期，所以存redis7200秒取一次
     *
     * @return ticket
     */
    public Object getTicket() {
        String ticketKey = "Tencent:JsapiTicket";
        Object ticket  = redisUtils.get(ticketKey);
        if(ObjectUtils.isEmpty(ticket)){
            ticket = ticket();
            //设置缓存 2小时
            redisUtils.set(ticketKey,ticket, 7200, TimeUnit.SECONDS);
        }
        return ticket;
    }


    public String ticket(){
        String result = HttpRequest.get(GET_TICKET_URL+"&access_token="+getToken().toString()).execute().body();
        log.info("result:ticket"+result);
        if (!StringUtils.isNotEmpty(result)) {
            return null;
        }
        JSONObject object = JSONObject.parseObject(result);
        return ObjectUtils.isEmpty(object) ? null : object.getString("ticket");
    }

}
