package com.liquidnet.service.sweet.config;

import com.liquidnet.service.sweet.handler.SubscribeHandler;
import com.liquidnet.service.sweet.handler.UnsubscribeHandler;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;

@Configuration
public class WechatMpConfigure {
    private static String appidZhengzai;
    private static String secretZhengzai;
    private static String tokenZhengzai;
    private static String aeskeyZhengzai;

    private static String appidModernsky;
    private static String secretModernsky;

    @Value("${liquidnet.wechat.service.zhengzai.appid}")
    public void setAppidZhengzai(String appidZhengzai) { WechatMpConfigure.appidZhengzai = appidZhengzai; }
    @Value("${liquidnet.wechat.service.zhengzai.secret}")
    public void setSecretZhengzai(String secretZhengzai) {
        WechatMpConfigure.secretZhengzai = secretZhengzai;
    }
    @Value("${liquidnet.wechat.service.zhengzai.token}")
    public void setTokenZhengzai(String tokenZhengzai) {
        WechatMpConfigure.tokenZhengzai = tokenZhengzai;
    }
    @Value("${liquidnet.wechat.service.zhengzai.aeskey}")
    public void setAeskeyZhengzai(String aeskeyZhengzai) {
        WechatMpConfigure.aeskeyZhengzai = aeskeyZhengzai;
    }

    @Value("${liquidnet.wechat.service.modernsky.appid}")
    public void setAppidModernsky(String appidModernsky) {
        WechatMpConfigure.appidModernsky = appidModernsky;
    }
    @Value("${liquidnet.wechat.service.modernsky.secret}")
    public void setSecretModernsky(String secretModernsky) {
        WechatMpConfigure.secretModernsky = secretModernsky;
    }

    private WxMpService wxMpZhengzaiService;
    private WxMpService wxMpModernskyService;

    private WxMpMessageRouter wxMpZhengzaiMessageRouter;
    private WxMpMessageRouter wxMpModernskyMessageRouter;
    private WxMessageInMemoryDuplicateChecker wxMessageInMemoryDuplicateChecker;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SubscribeHandler subscribeHandler;
    @Autowired
    private UnsubscribeHandler unsubscribeHandler;

    @PostConstruct
    public void init() {
        wxMpZhengzaiService = new WxMpServiceImpl() {
            {
                RedisTemplateWxRedisOps redisTemplateWxRedisOps = new RedisTemplateWxRedisOps(stringRedisTemplate);
                WxMpRedisConfigImpl wxMpDefaultConfig = new WxMpRedisConfigImpl(redisTemplateWxRedisOps, "sweet:accessToken:service:zhengzai");

                wxMpDefaultConfig.setAppId(appidZhengzai);
                wxMpDefaultConfig.setSecret(secretZhengzai);
                wxMpDefaultConfig.setToken(tokenZhengzai);
                wxMpDefaultConfig.setAesKey(aeskeyZhengzai);

                this.setWxMpConfigStorage(wxMpDefaultConfig);
            }
        };
        wxMpModernskyService = new WxMpServiceImpl() {
            {
                RedisTemplateWxRedisOps redisTemplateWxRedisOps = new RedisTemplateWxRedisOps(stringRedisTemplate);
                WxMpRedisConfigImpl wxMpDefaultConfig = new WxMpRedisConfigImpl(redisTemplateWxRedisOps, "sweet:accessToken:service:modernsky");

                wxMpDefaultConfig.setAppId(appidModernsky);
                wxMpDefaultConfig.setSecret(secretModernsky);

                this.setWxMpConfigStorage(wxMpDefaultConfig);
            }
        };

        wxMpZhengzaiMessageRouter = new WxMpMessageRouter(wxMpZhengzaiService);
        wxMpModernskyMessageRouter = new WxMpMessageRouter(wxMpModernskyService);

        wxMessageInMemoryDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
    }

    public WxMpService getWxMpService(Integer pnum) {
        switch (pnum) {
            case 1:
                return wxMpZhengzaiService;
            case 2:
                return wxMpModernskyService;
        }
        return null;
    }

    public WxMpMessageRouter getWxMpMessageRouter(Integer pnum) {
        switch (pnum) {
            case 1:
                return wxMpZhengzaiMessageRouter;
            case 2:
                return wxMpModernskyMessageRouter;
        }
        return null;
    }

    public WxMpMessageRouter messageRouter(Integer type) {
        WxMpMessageRouter router = getWxMpMessageRouter(type);
        // 消息去重
        router.setMessageDuplicateChecker(wxMessageInMemoryDuplicateChecker);

        // 关注事件
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE)
                .handler(subscribeHandler)
                .end();

        // 取消关注事件
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(unsubscribeHandler)
                .end();

        return router;
    }
}
