/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

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

import cn.hutool.core.util.StrUtil;
import com.yami.shop.common.util.Json;
import com.yami.shop.mp.comment.model.WeixinKeywordresponse;
import com.yami.shop.mp.comment.service.WeixinKeywordresponseService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.api.WxConsts.KefuMsgType;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
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.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author LGH
 */
@Component
@AllArgsConstructor
public class MsgHandler extends AbstractHandler {

    private WeixinKeywordresponseService weixinKeywordresponseService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {
        //关键字回复
        String content = wxMessage.getContent();
        List<WeixinKeywordresponse> keywordresponses = weixinKeywordresponseService.listWeixinKeywordresponse();
        if (StrUtil.isBlank(content)) {
            return null;
        }
        content = content.trim();

        // 查找可以匹配的关键字
        WeixinKeywordresponse response = null;
        for (WeixinKeywordresponse keywordresponse : keywordresponses) {
            String keyword = keywordresponse.getKeyword();
            Integer pptype = keywordresponse.getPptype();
            // 模糊匹配
            if (pptype == 1 && StrUtil.containsIgnoreCase(content, keyword)) {
                response = keywordresponse;
                break;
            }
            // 完全匹配
            else if (pptype == 2 && Objects.equals(content, keyword)) {
                response = keywordresponse;
                break;
            }
        }
        if (response == null) {
            return null;
        }
        String msgType = response.getMsgType();
        if (KefuMsgType.VIDEO.equals(msgType)) {
            return WxMpXmlOutMessage.VIDEO().mediaId(response.getMediaId()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        } else if (KefuMsgType.VOICE.equals(msgType)) {
            return WxMpXmlOutMessage.VOICE().mediaId(response.getMediaId()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        } else if (WxConsts.KefuMsgType.IMAGE.equals(msgType)) {
            return WxMpXmlOutMessage.IMAGE().mediaId(response.getMediaId()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        } else if (KefuMsgType.NEWS.equals(msgType)) {
            WxMpMaterialNews wxMpMaterialNews = Json.parseObject(response.getContent(), WxMpMaterialNews.class);
            List<WxMpNewsArticle> articles = wxMpMaterialNews.getArticles();
            List<WxMpXmlOutNewsMessage.Item> newsItems = articles.stream().map(article -> {
                WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
                item.setDescription(article.getDigest());
                item.setPicUrl(article.getThumbUrl());
                item.setTitle(article.getTitle());
                item.setUrl(article.getUrl());
                return item;
            }).collect(Collectors.toList());
            return WxMpXmlOutMessage.NEWS().articles(newsItems).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        }
        return WxMpXmlOutMessage.TEXT().content(response.getContent()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
    }
}
