package com.jpsoft.shinestar.cphandler;

import com.jpsoft.shinestar.cpbuilder.TextBuilder;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

    @Override
    public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
                                    WxSessionManager sessionManager) {
        final String msgType = wxMessage.getMsgType();
        if (msgType == null) {
            // 如果msgType没有，就自己根据具体报文内容做处理
         //   return new TextBuilder().build("success", wxMessage, cpService);
            logger.warn("安装测试"+wxMessage);
            Map map = wxMessage.getAllFieldsMap();
            String authCode = (String) map.get("AuthCode");
            return new TextBuilder().build("success", wxMessage, cpService);
        }

        if (!msgType.equals(WxConsts.XmlMsgType.EVENT)) {
            //TODO 可以选择将消息保存到本地
        }

        //TODO 组装回复消息
        String content = "收到信息内容：" + wxMessage;

        return new TextBuilder().build(content, wxMessage, cpService);

    }

}
