package com.ruoyi.common.weixin.message;

import com.ruoyi.common.utils.StringUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息工具类
 *
 * @author liufeng
 * @date 2013-05-19
 */
public class MessageUtil {

    /**
     * 返回消息类型：文本
     */
    public static final String RESP_MESSAGE_TYPE_TEXT = "text";

    /**
     * 返回消息类型：音乐
     */
    public static final String RESP_MESSAGE_TYPE_MUSIC = "music";

    /**
     * 返回消息类型：图文
     */
    public static final String RESP_MESSAGE_TYPE_NEWS = "news";

    /**
     * 请求消息类型：文本
     */
    public static final String REQ_MESSAGE_TYPE_TEXT = "text";

    /**
     * 请求消息类型：图片
     */
    public static final String REQ_MESSAGE_TYPE_IMAGE = "image";

    /**
     * 请求消息类型：链接
     */
    public static final String REQ_MESSAGE_TYPE_LINK = "link";

    /**
     * 请求消息类型：地理位置
     */
    public static final String REQ_MESSAGE_TYPE_LOCATION = "location";

    /**
     * 请求消息类型：音频
     */
    public static final String REQ_MESSAGE_TYPE_VOICE = "voice";

    /**
     * 请求消息类型：推送
     */
    public static final String REQ_MESSAGE_TYPE_EVENT = "event";

    /**
     * 事件类型：subscribe(订阅)
     */
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";

    /**
     * 事件类型：unsubscribe(取消订阅)
     */
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";

    /**
     * 事件类型：CLICK(自定义菜单点击事件)
     */
    public static final String EVENT_TYPE_CLICK = "CLICK";

    public static Map<String,String> xmlToMap(String strxml) throws JDOMException, IOException {

        if(StringUtils.isBlank(strxml)) {
            return null;
        }

        Map<String, String> m = new HashMap<String, String>();

        InputStream in = new ByteArrayInputStream(strxml.getBytes(StandardCharsets.UTF_8));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List<Element> list = root.getChildren();
        for (Element e : list) {
            String k = e.getName();
            String v = "";
            List<Element> children = e.getChildren();
            if (children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = getChildrenText(children);
            }

            m.put(k, v);
        }

        //关闭流
        in.close();

        return m;
    }

    public static String getChildrenText(List<Element> children) {
        StringBuilder sb = new StringBuilder();
        if(!children.isEmpty()) {
            for (Element e : children) {
                String name = e.getName();
                String value = e.getTextNormalize();
                List<Element> list = e.getChildren();
                sb.append("<").append(name).append(">");
                if (!list.isEmpty()) {
                    sb.append(getChildrenText(list));
                }
                sb.append(value);
                sb.append("</").append(name).append(">");
            }
        }

        return sb.toString();
    }

    /**
     * 回复文本消息
     * @param ToUserName  接收者
     * @param FromUserName 发送者
     * @param title 内容
     * @return 结果
     */
    public static String replyContent(String FromUserName, String ToUserName, String title){
        return "<xml>" +
                "  <ToUserName><![CDATA["+ FromUserName +"]]></ToUserName>" +
                "  <FromUserName><![CDATA["+ ToUserName +"]]></FromUserName>" +
                "  <CreateTime>"+ System.currentTimeMillis() +"</CreateTime>" +
                "  <MsgType><![CDATA["+ MessageUtil.REQ_MESSAGE_TYPE_TEXT +"]]></MsgType>" +
                "  <Content><![CDATA["+ title +"]]></Content>" +
                "</xml>";
    }

    /**
     * 统一回复图文格式
     * @param FromUserName
     * @param ToUserName
     * @return
     */
    public static  String replayArticle(String FromUserName, String ToUserName,String Title,String intro,String QRCode,String urlSite) {
        return "<xml>\n" +
                "  <ToUserName><![CDATA["+ FromUserName +"]]></ToUserName>" +
                "  <FromUserName><![CDATA["+ ToUserName +"]]></FromUserName>" +
                "  <CreateTime>"+ System.currentTimeMillis() +"</CreateTime>" +
                "  <MsgType><![CDATA["+ MessageUtil.RESP_MESSAGE_TYPE_NEWS +"]]></MsgType>" +
                "  <ArticleCount>1</ArticleCount>" +
                "  <Articles>" +
                "    <item>" +
                "      <Title><![CDATA["+ Title +"]]></Title>" +
                "      <Description><![CDATA["+ intro +"]]></Description>" +
                "      <PicUrl><![CDATA["+ QRCode +"]]></PicUrl>" +
                "      <Url><![CDATA["+ urlSite +"]]></Url>" +
                "    </item>" +
                "  </Articles>" +
                "</xml>";
    }

    /**
     * 图片消息组装
     * @param ToUserName 用户
     * @param FromUserName
     * @return
     */
    public static String replayImage(String ToUserName, String FromUserName,String mediaId){
        return "<xml>\n" +
                "  <ToUserName><![CDATA["+ FromUserName +"]]></ToUserName>" +
                "  <FromUserName><![CDATA["+ ToUserName +"]]></FromUserName>" +
                "  <CreateTime>"+ System.currentTimeMillis() +"</CreateTime>" +
                "  <MsgType><![CDATA["+ MessageUtil.REQ_MESSAGE_TYPE_IMAGE +"]]></MsgType>" +
                "  <Image>" +
                "    <MediaId><![CDATA["+ mediaId +"]]></MediaId>" +
                "  </Image>" +
                "</xml>";
    }


}
