package com.iformall.service.wechat;

import com.iformall.config.WechatOpenProperties;
import com.iformall.config.WechatRedisProperies;
import com.iformall.mapper.WxAuthorizerInfoMapper;
import com.iformall.mapper.WxComponentVerifyTicketMapper;
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;

/**
 * Stormeye WU
 */
@Service
@EnableConfigurationProperties({WechatOpenProperties.class, WechatRedisProperies.class})
public class FmOpenService extends WxOpenServiceImpl {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private WechatOpenProperties wechatProperties;
    @Autowired
    private WechatRedisProperies redisProperies;

    @Autowired
    private WxComponentVerifyTicketMapper componentVerifyTicketMapper;

    @Autowired
    private WxAuthorizerInfoMapper authorizerInfoMapper;

    private static JedisPool pool;
    private WxOpenMessageRouter wxOpenMessageRouter;

    @PostConstruct
    public void init() {
        FmOpenInRedisDBConfigStorage configStorage = new FmOpenInRedisDBConfigStorage(getJedisPool(), componentVerifyTicketMapper, authorizerInfoMapper);
        configStorage.setComponentAppId(wechatProperties.getComponentAppId());
        configStorage.setComponentAppSecret(wechatProperties.getComponentSecret());
        configStorage.setComponentToken(wechatProperties.getComponentToken());
        configStorage.setComponentAesKey(wechatProperties.getComponentAesKey());
        setWxOpenConfigStorage(configStorage);
        wxOpenMessageRouter = new WxOpenMessageRouter(this);
        wxOpenMessageRouter.rule().handler((wxMpXmlMessage, map, wxMpService, wxSessionManager) -> {
            logger.info("\n接收到 {} 公众号请求消息，内容：{}", wxMpService.getWxMpConfigStorage().getAppId(), wxMpXmlMessage);
            return null;
        }).next();
    }

    public WxOpenMessageRouter getWxOpenMessageRouter() {
        return wxOpenMessageRouter;
    }

    private JedisPool getJedisPool() {
        if (pool == null) {
            synchronized (FmOpenService.class) {
                if (pool == null) {
                    pool = new JedisPool(redisProperies, redisProperies.getHost(),
                            redisProperies.getPort(), redisProperies.getConnectionTimeout(),
                            redisProperies.getSoTimeout(), redisProperies.getPassword(),
                            redisProperies.getDatabase(), redisProperies.getClientName(),
                            redisProperies.isSsl(), redisProperies.getSslSocketFactory(),
                            redisProperies.getSslParameters(), redisProperies.getHostnameVerifier());
                }
            }
        }
        return pool;
    }
}
