package com.cyksj.service.wechat;



import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.IoKit;
import com.cyksj.common.util.J11HttpC;
import com.cyksj.common.util.Jsons;
import com.cyksj.model.dto.MiniProgramSession;
import com.cyksj.model.dto.WeChatAccessToken;

import java.net.http.HttpResponse;

/**
 * @author valor.
 * @date 2019/12/3 20:44
 */
public class MiniProgramOperator implements MiniProgramOps {

//    private static final Logger log = LoggerFactory.getLogger(MiniProgramOperator.class);

    private MiniProgramOperator() { }

    private static volatile MiniProgramOperator instance;

    public static MiniProgramOperator getInstance() {
        if (null == instance) {
            synchronized (MiniProgramOperator.class) {
                if (null == instance) {
                    instance = new MiniProgramOperator();
                }
            }
        }
        return instance;
    }

    @Override
    public MiniProgramSession code2session(String appid, String secret, String code) throws Exception {
        // GET https://api.weixin.qq.com/sns/jscode2session?appid={APPID}&secret={SECRET}&js_code={JSCODE}&grant_type=authorization_code
        String url = "https://api.weixin.qq.com/sns/jscode2session" +
                "?appid=" + appid +
                "&secret=" + secret +
                "&js_code=" + code +
                "&grant_type=authorization_code";
        HttpResponse<byte[]> res =
                J11HttpC.custom()
                        .ofGet()
                        .url(url)
                        .send(HttpResponse.BodyHandlers.ofByteArray());

        if (200 != res.statusCode()) {
            throw BusinessRuntimeException.getInstance("获取用户信息失败: " + IoKit.toString(res.body()));
        }

        MiniProgramSession session = Jsons.parseObject(res.body(), MiniProgramSession.class);
        if (null == session) {
            throw BusinessRuntimeException.getInstance("获取用户信息失败: " + IoKit.toString(res.body()));
        }

        final Integer errcode = session.getErrcode();
        if (null != errcode && 0 != errcode) {
            throw BusinessRuntimeException.getInstance("获取用户信息失败: " + session.getErrmsg() + "[" + errcode + "]");
        }
        return session;
    }

    @Override
    public WeChatAccessToken access_token(String appid, String secret) throws Exception {
        // GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}
        String url = "https://api.weixin.qq.com/cgi-bin/token" +
                "?appid=" + appid +
                "&secret=" + secret +
                "&grant_type=client_credential";
        HttpResponse<byte[]> res =
                J11HttpC.custom()
                        .ofGet()
                        .url(url)
                        .send(HttpResponse.BodyHandlers.ofByteArray());

        if (200 != res.statusCode()) {
            throw BusinessRuntimeException.getInstance("获取access_token失败: " + IoKit.toString(res.body()));
        }

        WeChatAccessToken token = Jsons.parseObject(res.body(), WeChatAccessToken.class);
        if (null == token) {
            throw BusinessRuntimeException.getInstance("获取access_token失败: " + IoKit.toString(res.body()));
        }

        final Integer errcode = token.getErrcode();
        if (null != errcode && 0 != errcode) {
            throw BusinessRuntimeException.getInstance("获取access_token失败: " + token.getErrmsg() + "[" + errcode + "]");
        }
        return token;
    }
}
