package com.yidian.shenghuoquan.newscontent.utils

import android.text.InputFilter
import java.util.regex.Pattern


/**
 * author: yinjiacheng
 * date: 6/15/21 1:59 AM
 * description: 输入过滤
 */
object InputFilterUtil {

  /**
   * 只允许汉字输入
   */
  fun getChineseFilter(): InputFilter {
    return InputFilter { source, start, end, dest, dstart, dend ->
      val pattern = Pattern.compile("[\\u4E00-\\u9FFF]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }

  /**
   * 真实姓名过滤
   * 汉字 + ·
   */
  fun getRealNameFilter(): InputFilter {
    return InputFilter { source, start, end, dest, dstart, dend ->
      val pattern = Pattern.compile("[\\u4E00-\\u9FFF|·]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }

  /**
   * 身份证号过滤
   * 数字 + X
   */
  fun getIDCardNumberFilter(): InputFilter {
    return InputFilter { source, start, end, dest, dstart, dend ->
      val pattern = Pattern.compile("[0-9|X]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }

  /**
   * 手机号过滤
   * 数字
   */
  fun getPhoneNumberFilter(): InputFilter {
    return InputFilter { source, start, end, dest, dstart, dend ->
      val pattern = Pattern.compile("[0-9]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }

  /**
   * 公司注册名过滤
   * 汉字
   */
  fun getRegistrationNameFilter(): InputFilter {
    return InputFilter { source, start, end, dest, dstart, dend ->
      val pattern = Pattern.compile("[\\u4E00-\\u9FFF]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }

  /**
   * 统一社会信用代码过滤
   * 数字 + 大写字母
   */
  fun getRegistrationCodeFilter(): InputFilter {
    return InputFilter { source, start, end, dest, dstart, dend ->
      val pattern = Pattern.compile("[0-9A-Z]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }
}
