package com.yidian.bcommon.utils

import android.text.Editable
import android.text.InputFilter
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.EditText
import java.io.Serializable
import java.util.regex.Pattern

/**
 * 检查工具类
 */
abstract class ValidationUtils : Serializable {

  /**
   * 检查输入是否超出规定长度
   *
   * @param length
   * @param value
   * @return
   */
  fun checkLength(value: String?, length: Int): Boolean {
    return (if (value == null || "" == value.trim { it <= ' ' }) 0 else value.length) <= length
  }

  /**
   * 检查是否为整数
   *
   * @param value
   * @return
   */
  fun checkInteger(value: String?): Boolean {
    val p = Pattern.compile("^\\d*$")
    val m = p.matcher(value)
    return m.matches()
  }

  companion object {
    private const val serialVersionUID = -7712299555022613697L

    /**
     * 匹配正则表达式
     *
     * @param reg     正则表达式
     * @param content 匹配内容
     * @return
     */
    fun pattern(reg: String?, content: String?): Boolean {
      val pattern = Pattern.compile(reg)
      val matcher = pattern.matcher(content)
      return matcher.matches()
    }

    /**
     * 是否为null/字符串""
     *
     * @param str
     * @return
     */
    fun isNull(str: String?): Boolean {
      return str == null || str == "" || str == "0"
    }

    /**
     * 价格输入控制
     * 小数点后两位
     *
     * @param editText
     */
    fun setPricePoint(editText: EditText) {
      editText.addTextChangedListener(object : TextWatcher {
        override fun onTextChanged(
          s: CharSequence, start: Int, before: Int,
          count: Int
        ) {
          var s = s
          if (s.toString().contains(".")) {
            if (s.length - 1 - s.toString().indexOf(".") > 2) {
              s = s.toString().subSequence(
                0,
                s.toString().indexOf(".") + 3
              )
              editText.setText(s)
              editText.setSelection(editText.text.length)
            }
          }
          if (s.toString().trim { it <= ' ' }.substring(0) == ".") {
            s = "0$s"
            editText.setText(s)
            editText.setSelection(2)
          }
          if (s.toString().startsWith("0")
            && s.toString().trim { it <= ' ' }.length > 1
          ) {
            if (s.toString().substring(1, 2) != ".") {
              editText.setText(s.subSequence(0, 1))
              editText.setSelection(1)
              return
            }
          }
        }

        override fun beforeTextChanged(
          s: CharSequence, start: Int, count: Int,
          after: Int
        ) {
        }

        override fun afterTextChanged(s: Editable) {}
      })
    }

    /**
     * 手机号码匹配	 *
     * rg\iuyt	qwertyuio[po0`124`569hqw23
     *
     * @param mobiles
     * @return
     */
    fun isMobile(mobiles: String?): Boolean {
      val p = Pattern.compile("^1[3,4,5,7,8]\\d{9}$")
      val m = p.matcher(mobiles)
      return m.matches()
    }

    /**
     * 银行卡号匹配	 *
     * rg\iuyt	qwertyuio[po0`124`569hqw23
     *
     * @param mobiles
     * @return
     */
    fun isBankCard(mobiles: String?): Boolean {
      val p = Pattern.compile("^\\d{16}|\\d{17}|\\d{18}|\\d{19}$")
      val m = p.matcher(mobiles)
      return m.matches()
    }

    /**
     * 验证邮箱地址是否正确
     *
     * @param email
     * @return
     */
    fun isEmail(email: String?): Boolean {
      var flag = false
      flag = try {
        val check = "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"
        val regex = Pattern.compile(check)
        val matcher = regex.matcher(email)
        matcher.matches()
      } catch (e: Exception) {
        false
      }
      return flag
    }

    fun length(str: String): Int {
      var str = str
      str = str.replace("[^\\x00-\\xff]".toRegex(), "**")
      return str.length
    }

    fun equlse(v: String?, v1: String?): Boolean {
      if (v == null || v1 == null) {
        return false
      }
      return v == v1
    }

    /**
     * 只要有一个 引用相等 都返回True
     *
     * @param value
     * @param v
     * @return
     * @Title: orEqulse
     * @return: boolean
     */
    fun orEqulse(value: String?, vararg v: String?): Boolean {
      if (value == null) {
        return false
      }
      if (v.size > 0) {
        for (i in v) {
          if (equlse(value, i)) {
            return true
          }
        }
      }
      return false
    }

    /**
     * 字符串函数  模仿数据库 REGIHT()
     *
     * @param value
     * @param count
     * @return
     */
    fun right(value: String, count: Int): String {
      return value.substring(value.length - count)
    }

    /**
     * 字符串函数  模仿数据库 LEFT()
     *
     * @param value
     * @param count
     * @return
     */
    fun left(value: String, count: Int): String {
      return if (value.length >= count) {
        value.substring(0, count) + "..."
      } else {
        value
      }
    }

    /**
     * 检查身份证号码
     */
    fun isIDCardNum(num: String?): Boolean {
      if (TextUtils.isEmpty(num)) {
        return false
      }
      val regex = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])"
      val pattern = Pattern.compile(regex)
      return pattern.matcher(num).matches()
    }

  }
}
