package com.yonge.cooleshow.auth.middleware.wechat.handler;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import com.microsvc.toolkit.middleware.wechat.handler.ContactUSHandler;
import com.microsvc.toolkit.middleware.wechat.handler.LocationHandler;
import com.microsvc.toolkit.middleware.wechat.handler.NullHandler;
import com.microsvc.toolkit.middleware.wechat.handler.ScanHandler;
import com.microsvc.toolkit.middleware.wechat.handler.SubscribeHandler;
import com.microsvc.toolkit.middleware.wechat.handler.UnsubscribeHandler;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;

/**
 * 微信消息路由配置
 * Created by Eric.Shang on 6/3/17.
 */
public final class WxMessageRouteEvent {

    // 联系我们事件配置
    private static final String EVENT_KEY_CONTACT_US = "WX_CONTACT_US";

    private WxMessageRouteEvent() {
    }

    /**
     * 微信消息路由处理中心
     * @param wxMpService 公众号实例
     * @return WxMpMessageRouter
     */
    public static WxMpMessageRouter router(WxMpService wxMpService) {
        return new WxMpMessageRouter(wxMpService)
                .rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SCAN).handler(new ScanHandler()).end()
                .rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(new SubscribeHandler()).end()
                .rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE).handler(new UnsubscribeHandler()).end()
                .rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.LOCATION).handler(new LocationHandler()).end()
                .rule().async(false).msgType(WxConsts.XmlMsgType.LOCATION).handler(new LocationHandler()).end()
                .rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.CLICK).eventKey(EVENT_KEY_CONTACT_US).handler(new ContactUSHandler()).end()
                .rule().async(false).handler(new NullHandler()).end();
    }

    /**
     * 小程序消息路由处理中心
     * @param wxMaService wxMaService
     * @return WxMaMessageRouter
     */
    public static WxMaMessageRouter maRouter(WxMaService wxMaService) {
        return new WxMaMessageRouter(wxMaService).rule().end();
    }
}
