package com.simplemobiletools.commons.utils

import java.lang.reflect.Field


object PhoneFromUtilCommon {

    /**
     * 获取调用者的包名
     *
     * @return
     */


    // 反射的方式获取mReferrer
    fun reflectGetReferrerA(): String {
        try {
            val activityClass = Class.forName("android.app.Activity");

            val refererField = activityClass.getDeclaredField("mReferrer");
            refererField.isAccessible = true
            val referrer: String = refererField.get(this).toString()
            return referrer
        } catch (e: Exception) {
            return "No referrer"
        }
    }

    fun reflectGetReferrer(): String {
        try {
            val activityClass = Class.forName("android.app.Activity")
            val refererField: Field = activityClass.getDeclaredField("mReferrer")
            refererField.isAccessible = true
            return refererField.get(this).toString()
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
            return "No referrer"
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
            return "No referrer"
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
            return "No referrer"
        }
    }

    /**
     * 是否是纯数字 正则
     */
    fun isNumeric(str: String): Boolean {
        return str.matches("\\d+".toRegex())
    }

    /**
     * 判断字符串是否为纯中文
     *
     * @param str 输入的字符串
     * @return 是否为纯中文
     */
    fun isPureChinese(str: String?): Boolean {
        if (str.isNullOrEmpty()) {
            return false
        }
        // 定义中文字符的 Unicode 范围
        val regex = "[\\u4E00-\\u9FA5|\\u3400-\\u4DBF|\\u20000-\\u2A6DF]+"

        return str.matches(regex.toRegex())
    }

    fun containsChinese(str: String): Boolean {
        for (i in 0 until str.length) {
            val c = str[i]
            if (c >= '\u4e00' && c <= '\u9fff' || c >= '\u3400' && c <= '\u4DBF') {
                return true
            }
        }
        return false
    }
}
