package com.younike.common.wxpay.config;

import com.younike.common.redis.ICached;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

@Service
public class Configure {

    @Resource
    ICached cached;

    private final static String WX_ACCESS_TOKEN = "wx_access_token";

    private static String token = null;
    private static Date tokenTime = null;
    private static String jsapiTicket = null;
    private static Date jsapiTicketTime = null;

    public static String MD5 = "MD5";
    public static String EMPTY = "";
    public static String SUCCESS = "SUCCESS";
    public static String HEX_FORMAT = "%02x";
    public static String TRADE_TYPE = "JSAPI";
    public static String MIDDLE_LINE = "-";
    public static String CHARTSET_UTF8 = "UTF-8";

    public static String NOTIFY_SUCCESS = "<xml>\n<return_code><![CDATA[SUCCESS]]></return_code>\n<return_msg><![CDATA[OK]]></return_msg>\n</xml>";
    public static String NOTIFY_FAIL = "<xml>\n<return_code><![CDATA[FAIL]]></return_code>\n<return_msg><![CDATA[ERROR]]></return_msg>\n</xml>";

    private static String key = "API密钥(32位，在微信商户平台下的API安全栏下)";
    // 微信分配的公众号ID（开通公众号之后可以获取到）
    private static String appID = "XXXXXXXXXXXXXX";
    private static String appSecret = "XXXXXXXXXXXXXX";
    // 微信支付分配的商户号ID（开通公众号的微信支付功能之后可以获取到）
    private static String mchID = "1349019501";
    // 机器IP
    private static String ip = "127.0.0.1";

    // 以下是几个API的路径：
    //  access_token API
    public static String TOKEN_API = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    // 临时票据API
    public static String TICKET_API = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
    // 微信OPENID API
    public static String OPENID_API = "https://api.weixin.qq.com/sns/oauth2/access_token";

    public static void setKey(String key) {
        Configure.key = key;
    }

    public static void setAppID(String appID) {
        Configure.appID = appID;
    }

    public static void setMchID(String mchID) {
        Configure.mchID = mchID;
    }

    public static void setIp(String ip) {
        Configure.ip = ip;
    }

    public static String getKey(){
        return key;
    }

    public static String getAppid(){
        return appID;
    }

    public static String getMchid(){
        return mchID;
    }

    public static String getIP(){
        return ip;
    }

    public static String getAppSecret() {
        return appSecret;
    }

    public static void setAppSecret(String appSecret) {
        Configure.appSecret = appSecret;
    }

    public String getToken() {
        return cached.getString(Configure.WX_ACCESS_TOKEN);
    }

    public void setToken(String token) {
        cached.set(Configure.WX_ACCESS_TOKEN, token, 1800);
        Configure.token = token;
        Configure.tokenTime = new Date();
    }

    public static String getJsapiTicket() {
        return jsapiTicket;
    }

    public static void setJsapiTicket(String jsapiTicket) {
        Configure.jsapiTicket = jsapiTicket;
        Configure.jsapiTicketTime = new Date();
    }

    public boolean checkToken() {
        return cached.exists(Configure.WX_ACCESS_TOKEN);
    }

    public void clearToken() {
        cached.delete(Configure.WX_ACCESS_TOKEN);
        token = null;
    }

    public static boolean checkJsapiTicket() {
        if (!StringUtils.isEmpty(Configure.jsapiTicket)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(jsapiTicketTime);
            calendar.add(Calendar.SECOND, 7200);
            return calendar.before(new Date());
        }
        return true;
    }
}
