package com.liquidnet.service.sweet.config;

import com.liquidnet.service.sweet.handler.*;
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;

    private static String appidTest;
    private static String secretTest;

    private static String appidActivity;
    private static String secretActivity;

    @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;
    }

    @Value("${liquidnet.wechat.service.test.appid}")
    public void setAppidTest(String appidTest) {
        WechatMpConfigure.appidTest = appidTest;
    }
    @Value("${liquidnet.wechat.service.test.secret}")
    public void setSecretTest(String secretTest) {
        WechatMpConfigure.secretTest = secretTest;
    }

    @Value("${liquidnet.wechat.service.zhengzaiActivity.appid}")
    public void setAppidActivity(String appidActivity) {
        WechatMpConfigure.appidActivity = appidActivity;
    }
    @Value("${liquidnet.wechat.service.zhengzaiActivity.secret}")
    public void setSecretActivity(String secretActivity) {
        WechatMpConfigure.secretActivity = secretActivity;
    }

    private WxMpService wxMpZhengzaiService;
    private WxMpService wxMpModernskyService;
    private WxMpService wxMpTestService;
    private WxMpService wxMpActivityService;

    private WxMpMessageRouter wxMpZhengzaiMessageRouter;
    private WxMpMessageRouter wxMpModernskyMessageRouter;
    private WxMpMessageRouter wxMpTestMessageRouter;
    private WxMpMessageRouter wxMpActivityMessageRouter;
    private WxMessageInMemoryDuplicateChecker wxMessageInMemoryDuplicateChecker;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SubscribeHandler subscribeHandler;
    @Autowired
    private UnsubscribeHandler unsubscribeHandler;
    @Autowired
    private StrawberryPosterHandler posterHandler;
    @Autowired
    private TextMsgHandler textMsgHandler;
    @Autowired
    private MaoDengImageHandler maodengImageHandler;

    @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);
            }
        };
        wxMpTestService = new WxMpServiceImpl() {
            {
                RedisTemplateWxRedisOps redisTemplateWxRedisOps = new RedisTemplateWxRedisOps(stringRedisTemplate);
                WxMpRedisConfigImpl wxMpDefaultConfig = new WxMpRedisConfigImpl(redisTemplateWxRedisOps, "sweet:accessToken:service:test");

                wxMpDefaultConfig.setAppId(appidTest);
                wxMpDefaultConfig.setSecret(secretTest);

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

                wxMpDefaultConfig.setAppId(appidActivity);
                wxMpDefaultConfig.setSecret(secretActivity);

                this.setWxMpConfigStorage(wxMpDefaultConfig);
            }
        };

        wxMpZhengzaiMessageRouter = new WxMpMessageRouter(wxMpZhengzaiService);
        wxMpModernskyMessageRouter = new WxMpMessageRouter(wxMpModernskyService);
        wxMpTestMessageRouter = new WxMpMessageRouter(wxMpTestService);
        wxMpActivityMessageRouter = new WxMpMessageRouter(wxMpActivityService);

        wxMessageInMemoryDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
    }

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

    public WxMpMessageRouter getWxMpMessageRouter(Integer pnum) {
        switch (pnum) {
            case 1:
                return wxMpZhengzaiMessageRouter;
            case 2:
                return wxMpModernskyMessageRouter;
            case 3:
                return wxMpTestMessageRouter;
            case 4:
                return wxMpActivityMessageRouter;
        }
        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();

        // 2021草莓音乐节海报活动菜单点击事件
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.CLICK)
                .eventKey("V1001_GOOD")
                .handler(posterHandler)
                .end();

        // 2021草莓音乐节海报活动 文本消息
        router.rule().async(false).msgType(WxConsts.XmlMsgType.TEXT)
                .handler(textMsgHandler)
                .end();

        // 猫登活动 图片
        router.rule().async(false).msgType(WxConsts.XmlMsgType.IMAGE)
                .handler(maodengImageHandler)
                .end();

        return router;
    }
}
