package com.yami.shop.mp.comment.api.config;

import com.yami.shop.config.WxConfig;
import com.yami.shop.mp.comment.api.handler.MenuHandler;
import com.yami.shop.mp.comment.api.handler.MsgHandler;
import com.yami.shop.mp.comment.api.handler.SubscribeHandler;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * 微信路由的配置
 * @author LGH
 */
@Component
@AllArgsConstructor
public class WxMpMessageRouterConfig {

    private final MenuHandler menuHandler;
    private final MsgHandler msgHandler;
    private final SubscribeHandler subscribeHandler;
    private final WxConfig wxConfig;

    public WxMpMessageRouter getMessageRouter() {
        WxMpMessageRouter newRouter = new WxMpMessageRouter(wxConfig.getWxMpService());

        // 自定义菜单事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.CLICK).handler(this.menuHandler).end();

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

        // 默认
        newRouter.rule().async(false).handler(this.msgHandler).end();

        return newRouter;
    }

}
