package com.ematou.wxservice.mp.router;

import com.alibaba.fastjson.JSON;
import com.ematou.wxservice.common.constant.WeChatConstant;
import com.ematou.wxservice.mp.handler.*;
import com.ematou.wxservice.mp.message.WeChatMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 根据消息类型找到对应的处理类
 * @author lhm
 * @version 1.0
 * 2021-05-11 16:23
 */
@Component
public class WeChatMessageHandlerRouter {

    private static final Map<String, WeChatMessageHandler> map = new ConcurrentHashMap<>();

    public WeChatMessageHandlerRouter(
//            WeChatTextMessageHandler weChatTextMessageHandler,
            WeChatClickEventMessageHandler weChatClickEventMessageHandler,
            WeChatViewEventMessageHandler weChatViewEventMessageHandler,
            WeChatScancodePushMessageHandler weChatScancodePushMessageHandler,
            WeChatNewsMessageHandler weChatNewsMessageHandler,
            WeChatSubscribeEventHandler weChatSubscribeEventHandler,
            WeChatScannHandler weChatScannHandler,
            WeChatUnsubscribeHandler weChatUnsubscribeHandler,
            WeChatImageMessageHandler weChatImageMessageHandler
    ){
//        map.put(WeChatConstant.XmlMsgType.TEXT, weChatTextMessageHandler);
        map.put(WeChatConstant.EventType.CLICK.toLowerCase(), weChatClickEventMessageHandler);
        map.put(WeChatConstant.EventType.VIEW.toLowerCase(), weChatViewEventMessageHandler);
        map.put(WeChatConstant.XmlMsgType.NEWS, weChatNewsMessageHandler);
        map.put(WeChatConstant.EventType.SUBSCRIBE, weChatSubscribeEventHandler);
        map.put(WeChatConstant.EventType.SCAN, weChatScannHandler);
        map.put(WeChatConstant.EventType.SCANCODE_PUSH, weChatScancodePushMessageHandler);
        map.put(WeChatConstant.EventType.UNSUBSCRIBE, weChatUnsubscribeHandler);
        map.put(WeChatConstant.EventType.MEDIA_ID, weChatImageMessageHandler);
    }

    public static WeChatMessageHandler router(WeChatMessage weChatMessage){

        String msgType = weChatMessage.getMsgType();
        // 如果是事件类型
        if (WeChatConstant.XmlMsgType.EVENT.equalsIgnoreCase(msgType)) {
            return map.get(weChatMessage.getEvent().toLowerCase());
        }

        return map.get(msgType);
    }

}
