package com.hai.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class WxMaConfiguration {

    private static WxMaService maService;
    private static WxMaService maQyxService;

    public static WxMaService getMaService() {
        if (maService == null) {
            throw new IllegalArgumentException(String.format("未找到对应的配置，请核实！"));
        }

        return maService;
    }

    public static WxMaService getMaService(Integer type) {
        if (maService == null || maQyxService == null) {
            throw new IllegalArgumentException(String.format("未找到对应的配置，请核实！"));
        }

        // type 1：嗨森逛小程序参数 2：黔印象小程序参数
        if (type == 1) {
            return maService;
        }

        return maQyxService;
    }

    @PostConstruct
    public void init() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(CommonSysConst.getSysConfig().getWxAppId());
        config.setSecret(CommonSysConst.getSysConfig().getWxAppSecret());

        maService = new WxMaServiceImpl();
        maService.setWxMaConfig(config);

        WxMaDefaultConfigImpl qyxConfig = new WxMaDefaultConfigImpl();
        qyxConfig.setAppid(CommonSysConst.getSysConfig().getWxQyxAppId());
        qyxConfig.setSecret(CommonSysConst.getSysConfig().getWxQyxAppSecret());

        maQyxService = new WxMaServiceImpl();
        maQyxService.setWxMaConfig(qyxConfig);
    }

}
