package com.gree.mall.manager.config.wx;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.aliyuncs.utils.StringUtils;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.google.common.collect.Maps;
import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
import com.gree.mall.manager.plus.service.AdminCompanyWechatService;
import com.gree.mall.manager.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Configuration
public class WxConfiguration {

    //微信小程序API key = companyWechatId
    public static Map<String, WxMaService> wxMaService = Maps.newConcurrentMap();
    //微信支付服务 key = companyWechatId
    public static Map<String, WxPayService> wxPayServices = Maps.newConcurrentMap();
    //发货模板  key = companyWechatId
    public static Map<String, String> temFhNotice = Maps.newConcurrentMap();

    @Value("${wechat.payment.notifyUrl}")
    private String notifyUrl;
    @Value("${wechat.appid}")
    private String wechatAppid;
    @Value("${wechat.mch.id}")
    private String wechatMchId;
    @Value("${wechat.mch.key}")
    private String wechatMchKey;
    @Value("${wechat.sub.appid}")
    private String wechatSubAppid;
    @Value("${wechat.sub.secret}")
    private String wechatSubSecret;
    @Value("${wechat.keyPath}")
    private String keyPath;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AdminCompanyWechatService adminCompanyWechatService;

    @PostConstruct
    public void initWxServices() {
        //查询企业微信配置
        List<AdminCompanyWechat> list = adminCompanyWechatService.list();
        reloadWxConfig(list, redisUtil, keyPath, notifyUrl);
        //初始化微信小程序服务
        initWxMaService(redisUtil, wechatSubAppid, wechatSubSecret);
    }

    public static void reloadWxConfig(List<AdminCompanyWechat> list, RedisUtil redisUtil, String keyPath, String notifyUrl) {

        wxMaService.clear();
        wxPayServices.clear();

        for (AdminCompanyWechat companyWechat : list) {
            if (StringUtils.isEmpty(companyWechat.getMchId()) || StringUtils.isEmpty(companyWechat.getSubAppId()) || StringUtils.isEmpty(companyWechat.getSubSecret())) {
                continue;
            }
            //初始化微信支付服务
            initWxPayService(companyWechat, keyPath, notifyUrl);
        }
    }

    /**
     * 初始化微信支付服务
     *
     * @param companyWechat
     */
    private static void initWxPayService(AdminCompanyWechat companyWechat, String keyPath, String notifyUrl) {

        String path = keyPath.toLowerCase();
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(companyWechat.getAppId());
        wxPayConfig.setMchId(companyWechat.getMchId());
        wxPayConfig.setMchKey(companyWechat.getMchKey());
        wxPayConfig.setSubAppId(companyWechat.getSubAppId());
        wxPayConfig.setSubMchId(companyWechat.getSubMchId());
        wxPayConfig.setNotifyUrl(notifyUrl);
        wxPayConfig.setKeyPath(path);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        wxPayServices.put(companyWechat.getCompanyWechatId(), wxPayService);
    }

    /**
     * 初始化小程序服务
     */
    private static void initWxMaService(RedisUtil redisUtil, String wechatSubAppid, String wechatSubSecret) {
        WxMaDefaultConfigImpl config = new WxMaLettuceRedisConfig(redisUtil);
        // 使用上面的配置时，需要同时引入jedis-lock的依赖，否则会报类无法找到的异常
        config.setAppid(wechatSubAppid);
        config.setSecret(wechatSubSecret);
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        wxMaService.put(wechatSubAppid, service);
    }


}
