package com.miaxis.common.config;

import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;


public class WxPayConfigImpl implements WXPayConfig {
    public static String url = "你设置的回调接口";
    private static WxPayConfigImpl wxPayConfig;
    private byte[] certData = null;


//    @Resource
//    private WxpayConfig wxpayConfigBean;

    @Value("${app.appid}")
    private String appid;

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

    public WxPayConfigImpl() {
        try {
            //这个证书的位置不是瞎鸡儿填的，你要在这个路径真的有一个证书
            // InputStream is = new FileInputStream("F:\\twzd\\twzd\\twzd-admin\\src\\main\\resources\\wechatpay\\apiclient_cert.p12");
            InputStream is = new FileInputStream(new ClassPathResource("wechatpay/apiclient_cert.p12").getFile());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] bs = new byte[1024];
            int cnt = -1;
            while ((cnt = is.read(bs)) != -1) {
                baos.write(bs, 0, cnt);
            }
            is.close();
            certData = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getAppID() {
        return "wx67ca1b8c9816ef28";
    }

    @Override
    public String getMchID() {
        return "1615410794";
    }

    @Override
    public String getKey() {
        return "12345678123456781234567812345678";
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        // TODO Auto-generated method stub
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        // TODO Auto-generated method stub
        return 10000;
    }


    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getNotifyUrl(){
        return url;
    }
}
