package com.github.sd4324530.fastweixin.api.config;

import com.github.sd4324530.fastweixin.api.response.GetJsApiTicketResponse;
import com.github.sd4324530.fastweixin.api.response.GetTokenResponse;
import com.github.sd4324530.fastweixin.util.JSONUtil;
import com.github.sd4324530.fastweixin.util.NetWorkCenter;

import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * API配置类，项目中请保证其为单例
 *
 * @author peiyu
 * @since 1.2
 */
public final class ApiConfig {

    public static final  AtomicBoolean refreshing = new AtomicBoolean(false);
    private static final Logger        LOG        = LoggerFactory.getLogger(ApiConfig.class);
    private final String appid;
    private final String secret;
    private       String accessToken;
    private       String jsApiTicket;
    private      boolean enableJsApi = false;

    /**
     * 构造方法一，实现同时获取access_token。不启用jsApi
     *
     * @param appid  公众号appid
     * @param secret 公众号secret
     */
    public ApiConfig(String appid, String secret) {
        this.appid = appid;
        this.secret = secret;
        init();
    }

    /**
     * 构造方法二，实现同时获取access_token，启用jsApi
     *
     * @param appid  公众号appid
     * @param secret 公众号secret
     */
    public ApiConfig(String appid, String secret, boolean enableJsApi) {
        this.appid = appid;
        this.secret = secret;
        this.enableJsApi = enableJsApi;
        init();
    }

    public String getAppid() {
        return appid;
    }

    public String getSecret() {
        return secret;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getJsApiTicket() {
        return jsApiTicket;
    }

    public void setJsApiTicket(String jsApiTicket) {
        this.jsApiTicket = jsApiTicket;
    }

    public boolean isEnableJsApi() {
        return enableJsApi;
    }

    public void setEnableJsApi(boolean enableJsApi) {
        this.enableJsApi = enableJsApi;
    }

    /**
     * 初始化微信配置，即第一次获取access_token
     */
    public void init() {
        LOG.debug("开始第一次初始化access_token........");
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + this.appid + "&secret=" + this.secret;
        NetWorkCenter.get(url, null, new NetWorkCenter.ResponseCallback() {
            @Override
            public void onResponse(int resultCode, String resultJson) {
                if (HttpStatus.SC_OK == resultCode) {
                    GetTokenResponse response = JSONUtil.toBean(resultJson, GetTokenResponse.class);
                    LOG.debug("获取access_token:{}", response.getAccessToken());
                    ApiConfig.this.accessToken = response.getAccessToken();
                }
            }
        });

        if(enableJsApi){
            LOG.debug("开始第一次初始化 jsapi_ticket........");
            String url2 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken  + "&type=jsapi" ;
            NetWorkCenter.get(url2, null, new NetWorkCenter.ResponseCallback() {
                @Override
                public void onResponse(int resultCode, String resultJson) {
                    if (HttpStatus.SC_OK == resultCode) {
                        GetJsApiTicketResponse response = JSONUtil.toBean(resultJson, GetJsApiTicketResponse.class);
                        LOG.debug("获取jsapi_ticket:{}", response.getTicket() );
                        ApiConfig.this.jsApiTicket = response.getTicket();
                    }
                }
            });
        }
    }
}

@@ -305,17 +305,16 @@
 Center;%0A
-%0A
 import o
@@ -548,86 +548,8 @@
  %7B%0A%0A
-    public static final  AtomicBoolean refreshing = new AtomicBoolean(false);%0A
     
@@ -634,24 +634,102 @@
 fig.class);%0A
+    public final         AtomicBoolean refreshing = new AtomicBoolean(false);%0A
     private 
@@ -737,24 +737,25 @@
 inal String 
+ 
 appid;%0A    p
@@ -770,24 +770,25 @@
 inal String 
+ 
 secret;%0A    
@@ -804,24 +804,25 @@
      String 
+ 
 accessToken;
@@ -843,24 +843,25 @@
      String 
+ 
 jsApiTicket;
@@ -878,16 +878,17 @@
 te      
+ 
 boolean 
@@ -902,16 +902,8 @@
 sApi
- = false
 ;%0A%0A 
@@ -1094,67 +1094,29 @@
 this
-.
+(
 appid
- = appid;%0A        this.secret = secret;%0A        init(
+, secret, false
 );%0A 
@@ -2879,16 +2879,17 @@
       if
+ 
 (enableJ
@@ -2893,16 +2893,17 @@
 leJsApi)
+ 
 %7B%0A      
@@ -3059,17 +3059,16 @@
 ssToken 
- 
 + %22&type
@@ -3074,17 +3074,16 @@
 e=jsapi%22
- 
 ;%0A      
@@ -3509,17 +3509,16 @@
 Ticket()
- 
 );%0A     

优化配置类，刷新变量不再静态，实现多公众号多config时刷新冲突