package com.ads.common.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 编码与解码操作工具类
 *
 * @author AllenPang
 * @date 2016-08-17
 */
@Slf4j
public class CodecUtil {

    /**
     * 将 URL 编码
     */
    public static String encodeURL(String str) {
        if (StrUtil.isBlank(str)) {
            return "";
        }
        String target = "";
        try {
            target = URLEncoder.encode(str, "UTF-8");
        } catch (Exception e) {
            log.error("编码出错！", e);
        }
        return target;
    }

    /**
     * 将 URL 解码
     */
    public static String decodeURL(String str) {
        if (StrUtil.isBlank(str)) {
            return "";
        }
        String target = "";
        try {
            target = URLDecoder.decode(str, "UTF-8");
        } catch (Exception e) {
            log.error("解码出错！", e);
        }
        return target;
    }

    /**
     * 将字符串 MD5 加密
     */
    public static String encryptMD5(String str) {
        return DigestUtils.md5Hex(str);
    }

    /**
     * @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式（表情占4个字节，需要utf8mb4字符集）
     * @param str
     *            待转换字符串
     * @return 转换后字符串
     * @throws UnsupportedEncodingException
     *             exception
     */
    public static String emojiConvert1(String str)
            {
        if (str==null) {
            return "";
        }
        String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";

        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            try {
                matcher.appendReplacement(sb,
                        "[[" + URLEncoder.encode(matcher.group(1), "UTF-8")
                                + "]]");
            } catch (UnsupportedEncodingException e) {
               return "";
            }
        }
        matcher.appendTail(sb);
        // LOG.debug("emojiConvert " + str + " to " + sb.toString()
        // + ", len：" + sb.length());
        return sb.toString();
    }

    /**
     * @Description 还原utf8数据库中保存的含转换后emoji表情的字符串
     * @param str
     *            转换后的字符串
     * @return 转换前的字符串
     * @throws UnsupportedEncodingException
     *             exception
     */
    public static String emojiRecovery2(String str) {
        if (str == null) {
            str = "";
        }
        String patternString = "\\[\\[(.*?)\\]\\]";

        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);

        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            try {
                matcher.appendReplacement(sb,
                        URLDecoder.decode(matcher.group(1), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // LOG.error("emojiRecovery error", e);
                return "";
            }
        }
        matcher.appendTail(sb);
        // LOG.debug("emojiRecovery " + str + " to " + sb.toString());
        return sb.toString();
    }

    /**
     * @Description 过滤emoji处理过的表情
     * @param str
     *            转换后的字符串
     * @return 转换前的字符串
     * @throws UnsupportedEncodingException
     *             exception
     */
    public static String emojiFilter(String str)
            throws UnsupportedEncodingException {
        if (str == null) {
            str = "";
        }
        String patternString = "\\[\\[(.*?)\\]\\]";

        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);

        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb,
                    "");
        }
        matcher.appendTail(sb);
        // LOG.debug("emojiRecovery " + str + " to " + sb.toString());
        return sb.toString();
    }
    /**
     * 判断是否保护emoji
     * @param text
     * @return
     */
    public static boolean hasEmoji(String text){
        String speChat = "[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]";
        Pattern pattern = Pattern.compile(speChat);
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }


}
