package com.gingersoft.gsa.cloud.common.utils.inputFilter;

import android.content.Context;
import android.text.InputFilter;
import android.util.Log;

import com.gingersoft.gsa.cloud.common.utils.other.TextUtil;
import com.gingersoft.gsa.cloud.common.utils.toast.ToastUtils;

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

/**
 * @author 宇航.
 * User: admin
 * Date: 2020/11/25
 * Time: 17:30
 * Use:
 */
public class InputFilterUtils {
    /**
     * 只能輸入中英文和數字
     */
    public static final Pattern chAndEnAndNum = Pattern.compile("[a-zA-Z0-9\u4E00-\u9FA5]+");
    /**
     * 只能輸入英文和數字
     */
    public static final Pattern enAndNum = Pattern.compile("[a-zA-Z0-9]+");
    /**
     * 只能輸入中英文和數字還有一些標點符號
     * Punctuation
     */
    public static final Pattern chAndEnAndNumAndPt = Pattern.compile("[-a-zA-Z0-9\u4E00-\u9FA5/,.;，。；！!?？]+");
    /**
     * 只能輸入中英文和數字還有/
     * Punctuation
     */
    public static final Pattern chAndEnAndNumAndSlash = Pattern.compile("[-a-zA-Z0-9\u4E00-\u9FA5/]+");

    /**
     * emoji过滤器
     */
    public static final Pattern emoji = Pattern.compile(
            "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
            Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

    public static InputFilter getChAndEnAndNumAndPtInputFilter(Context mContext) {
        return InputFilterUtils.getTypeFilter(mContext, "請輸入中英文和數字", chAndEnAndNumAndPt);
    }

    public static InputFilter getTypeFilter(Context mContext, Pattern p) {
        return InputFilterUtils.getTypeFilter(mContext, "不支持輸入此內容", p);
    }

    public static InputFilter getChAndEnAndNumInputFilter(Context mContext) {
        return InputFilterUtils.getTypeFilter(mContext, "請輸入中英文和數字", chAndEnAndNum);
    }

    public static InputFilter getEnAndNumInputFilter(Context mContext) {
        return InputFilterUtils.getTypeFilter(mContext, "請輸入英文和數字", enAndNum);
    }

    public static InputFilter getLengthFilter(Context mContext, int maxLength) {
        return InputFilterUtils.getMaxLengthFilter(mContext, "最多輸入" + maxLength + "個字", maxLength);
    }

    /**
     * source 新输入的字符串
     * start  新输入的字符串起始下标，一般为0
     * end    新输入的字符串终点下标，一般为source长度-1
     * dest   输入之前文本框内容
     * dstart 原内容起始坐标，一般为0
     * dend   原内容终点坐标，一般为dest长度-1
     *
     * @param p
     * @return
     */
    private static InputFilter getTypeFilter(Context context, String tipString, Pattern p) {
        return (source, start, end, dest, dstart, dend) -> {
            Matcher m = p.matcher(source.toString());
            if (!m.matches()) {
                if (TextUtil.isNotEmptyOrNullOrUndefined(source.toString()) &&
                        TextUtil.isNotEmptyOrNullOrUndefined(tipString)) {
                    ToastUtils.show(context, tipString);
                }
                return "";
            }
            return null;
        };
    }

    /**
     * source 新输入的字符串
     * start  新输入的字符串起始下标，一般为0
     * end    新输入的字符串终点下标，一般为source长度-1
     * dest   输入之前文本框内容
     * dstart 原内容起始坐标，一般为0
     * dend   原内容终点坐标，一般为dest长度-1
     *
     * @param p
     * @return
     */
    public static InputFilter getTypeFilter(Pattern p) {
        return (source, start, end, dest, dstart, dend) -> {
            Matcher m = p.matcher(source.toString());
            if (!m.matches()) {
                return "";
            }
            return null;
        };
    }

    /**
     * 輸入最大長度限制
     *
     * @param context
     * @param tipString 超過最大長度限制的提示
     * @param maxLength 最大長度
     * @return
     */
    private static InputFilter getMaxLengthFilter(Context context, String tipString, int maxLength) {
        return (source, start, end, dest, dstart, dend) -> {
            int keep = maxLength - (dest.length() - (dend - dstart));
            if (keep <= 0) {
                if (TextUtil.isNotEmptyOrNullOrUndefined(source.toString()) && TextUtil.isNotEmptyOrNullOrUndefined(tipString)) {
                    ToastUtils.show(context, tipString);
                }
                return "";
            } else if (keep >= end - start) {
                return null; // keep original
            } else {
                keep += start;
                if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        if (TextUtil.isNotEmptyOrNullOrUndefined(tipString)) {
                            ToastUtils.show(context, tipString);
                        }
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        };
    }

    /**
     * 輸入最大長度限制
     *
     * @param maxLength 最大長度
     * @return
     */
    private static InputFilter getMaxLengthFilter(int maxLength) {
        return (source, start, end, dest, dstart, dend) -> {
            int keep = maxLength - (dest.length() - (dend - dstart));
            if (keep <= 0) {
                return "";
            } else if (keep >= end - start) {
                return null; // keep original
            } else {
                keep += start;
                if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        };
    }
}

