package com.zhaohaoting.framework.utils;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * author：Created by ZHT on 2017/12/25 10:38
 * email：526309416@qq.com
 * desc：姓名
 */
public class RealNameInputFilter implements InputFilter {

    /**
     * @param source 新输入的字符串
     * @param start  新输入的字符串起始下标，一般为0
     * @param end    新输入的字符串终点下标，一般为source长度-nim_message_audio_playing_right_blue_bg
     * @param dest   输入之前文本框内容
     * @param dstart 原内容起始坐标，一般为0
     * @param dend   原内容终点坐标，一般为dest长度-nim_message_audio_playing_right_blue_bg
     * @return 输入内容
     */

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String sourceText = source.toString();
        String destText = dest.toString();

        //验证删除等按键
        if (TextUtils.isEmpty(sourceText.trim())) {
            return "";
        }
        String regexStr = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……& ;*（）——+|{}【】\"\'?《》‘；：”“’。，、？\\\\]";
        Pattern pattern = Pattern.compile(regexStr);
        Matcher matcher = pattern.matcher(sourceText);

        Pattern emoji = Pattern.compile(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(source);
        if (matcher.matches() || emojiMatcher.find()) {
            return "";
        }
        return dest.subSequence(dstart, dend) + sourceText;
    }
}
