package com.secspace.sms.util

/**
 * 手机号码处理工具类
 * Created by
 */
object PhoneUtils {

    /**
     * 电话号掩码规则
     */
    fun phoneNumberFormat(number: String, phoneCallBack: (String) -> Unit ={}/*, errorCallback: (String) -> Unit*/) {
        if (number.isBlank() || number.length <= 6) return
        var phoneResult = ""
        val numberResult = number.replace(" ", "")//原始号码
        when (numberResult.length) {
            7 -> {
                val phone = numberResult.substring(3, numberResult.length)
                phoneResult = "${"***$phone"}"
                phoneCallBack.invoke(phoneResult)
            }

            8 -> {
                val phone = numberResult.substring(4, numberResult.length)
                phoneResult = "${"****$phone"}"
                phoneCallBack.invoke(phoneResult)
            }

            11 -> {
                if (numberResult.substring(0, 1) == "1") {
                    phoneResult = numberResult.replace("(\\d{3})\\d{4}(\\d{4})".toRegex(), "$1****$2")
                    phoneCallBack.invoke(phoneResult)
                } else {
                    val phone1 = numberResult.substring(0, 3)
                    val phone2 = numberResult.substring(7, numberResult.length)
                    phoneResult = "${"$phone1****$phone2"}"
                    phoneCallBack.invoke(phoneResult)
                }
            }

            12 -> {
                val phone1 = numberResult.substring(0, 4)
                val phone2 = numberResult.substring(9, numberResult.length)
                phoneResult = "${"$phone1****$phone2"}"
                phoneCallBack.invoke(phoneResult)
            }

            14 -> {
                if (numberResult.contains("+86")) {
                    val phoneNum = numberResult.substring(3, numberResult.length)
                    phoneResult = phoneNum.replace("(\\d{3})\\d{4}(\\d{4})".toRegex(), "$1****$2")
                    phoneCallBack.invoke(phoneResult)
                }
            }

            else -> {
                if (number.isNotBlank()) {
                    phoneCallBack.invoke(number)
                }
            }
        }
//        Log.e("shuju", "---phoneNumberFormat---原号码:${numberResult} 掩码后:$phoneResult //手机号长度:${numberResult.length}")
    }

}

