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 getRealNameFilter(): InputFilter {
    return InputFilter { source, _, _, _, _, _ ->
      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, _, _, _, _, _ ->
      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, _, _, _, _, _ ->
      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, _, _, _, _, _ ->
      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, _, _, _, _, _ ->
      val pattern = Pattern.compile("[0-9A-Z]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }

  /**
   * 生活号名称过滤
   */
  fun getLifeAccountNameFilter(): InputFilter {
    return InputFilter { source, _, _, _, _, _ ->
      val pattern = Pattern.compile("[\\u4E00-\\u9FFF]+")
      val matcher = pattern.matcher(source.toString())
      if (!matcher.matches()) return@InputFilter ""
      return@InputFilter null
    }
  }
}
