package com.seer.teach.mp.handler;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import com.seer.teach.mp.entity.MpActivityEntity;
import com.seer.teach.mp.entity.MpAgentActivityParticipantEntity;
import com.seer.teach.mp.service.IMpActivityService;
import com.seer.teach.mp.service.IMpAgentActivityRelationService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;

/**
 * 用户扫码处理（已关注后的处理）
 */
@Component
@Slf4j
public class ScanHandler extends AbstractHandler implements WxMpMessageHandler {

    @Autowired
    private IMpAgentActivityRelationService mpAgentActivityRelationService;

    @Autowired
    private IMpActivityService mpActivityService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
        log.info("[handle][扫码处理，内容：{}]", wxMessage);
        String eventKey = wxMessage.getEventKey();
        log.info("[handle][用户({})] 获取用户二维码信息:[{}]", wxMessage.getFromUser(), eventKey);
        if(isAgentActivityScanEvent(wxMessage)){
            return buildAgentActivityOutMessage(wxMessage);
        }
        return null;
    }
}