package com.xingxi.api.configuration;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@AllArgsConstructor
@Configuration
@EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfiguration {

    private final WxMpProperties properties;

    @Bean
    public WxMpService wxMpService() {

        WxMpService service = new WxMpServiceImpl();
        WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
        wxMpDefaultConfig.setAppId(properties.getAppId());
        wxMpDefaultConfig.setSecret(properties.getSecret());

        Map<String, WxMpConfigStorage> configMap = Maps.newHashMap();
        configMap.put(properties.getAppId(),wxMpDefaultConfig);
        service.setMultiConfigStorages(configMap);

        return service;
    }
}
