package com.doverfuelingsolutions.issp.utils

import java.util.regex.Pattern

class ValidateUtil {

    companion object {

        private val regexIP: Regex by lazyOf(Regex("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\$"))
        private val regexPositiveInt: Regex by lazyOf(Regex("^[1-9]\\d*$"))
        private val regexUrl: Regex by lazyOf(Regex("^(https?|ftp|file)://([\\da-z.-]+)\\.([a-z.]{2,6})([/\\w .-]*)*/?\$"))
        private val regexIPHost: Regex by lazyOf(Regex("^(https?|ftp|file)://(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\$"))
        private val wexinRegex: String = "^(10|11|12|13|14|15)\\d{16}$"
        private val aliRegex = "^(25|26|27|28|29|30)\\d{14,22}$"

        fun isIP(ip: String): Boolean = regexIP.matches(ip)
        fun isPositiveInt(num: String): Boolean = regexPositiveInt.matches(num)
        fun isUrl(url: String): Boolean = regexUrl.matches(url)
        fun isIPHost(url: String): Boolean = regexIPHost.matches(url)
        fun isPort(port: String): Boolean = isPositiveInt(port) && port.length < 6 && port.toInt() < 65535
        // 判断付款码是否为支付宝的
        fun isAliPaymentCode(code: String) = code.startsWith("25") ||code.startsWith("26")
                ||code.startsWith("27") ||code.startsWith("28") ||code.startsWith("29") ||code.startsWith("30")

        // 判断是否是微信付款码
        fun isWXPaymentQRCode(code: String):Boolean {
            val pattern = Pattern.compile(wexinRegex) //编译正则表达式

            val matcher = pattern.matcher(code)
            return matcher.matches()
        }

        //判断是否是支付包付款码
        fun isAliPaymentQRCode(code: String):Boolean {
            val pattern = Pattern.compile(aliRegex) //编译正则表达式

            val matcher = pattern.matcher(code)
            return matcher.matches()
        }
    }
}