package utility

import (
	"regexp"
)

var (
	emailRegexp, _ = regexp.Compile("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$)")
)

// ValidateEmail 验证邮箱
func ValidateEmail(email string) bool {
	// userName := "[a-zA-Z0-9][a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]{0,62}[a-zA-Z0-9]"
	// domainName := "[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?"
	// topLevelDomainName := "(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*"
	// pattern := "^" + userName + "@" + domainName + topLevelDomainName + "$"
	// reg := regexp.MustCompile(pattern)
	// match := reg.MatchString(email)
	match := emailRegexp.MatchString(email)
	return match
}

// ValidatePhoneNumber 手机号码验证,+91-9819882936
//
//  1. Mobile Number
//     - Starts with 6,7,8,8
//     - 10 digit
//     - Prexix can be +91, 0
//
//  2. LandLine Number
//     - {area-code}-{local number}
//     - 10 digit number
//     - area codes range from 2-digits to 4-digits
//     - ex. 02321-238200
func ValidatePhoneNumber(num string) bool {
	prefixMobileNum := `(?:(?:\+|0{0,2})91([\s-])?|[0]?)?`
	mobileNum := `[6-9]\d{9}`
	landLineNum := `((0)?(([1-9]\d{1}-\d{8})|([1-9]\d{2}-\d{7})|([1-9]\d{3}-\d{6})))`
	pattern := "^(" + "(" + prefixMobileNum + mobileNum + ")" + "|" + landLineNum + ")$"
	reg := regexp.MustCompile(pattern)
	match := reg.MatchString(num)

	return match
}

// ValidatePhoneForeign 外国手机
func ValidatePhoneForeign(phone string) bool {
	reg, err := regexp.MatchString("^[0-9]{7}$", phone)
	if err != nil {
		return false
	}
	return reg
}

// IsMobileChina 中国手机验证
func IsMobileChina(phone string) bool {
	// reg, err := regexp.MatchString("^[0-9]{11}$", phone)
	reg, err := regexp.MatchString("^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9])\\d{8}$", phone)
	if err != nil {
		return false
	}
	return reg
}

// ReturnEmail 替换邮箱中间几位为*号
func ReturnEmail(email string) string {
	if len(email) == 0 {
		return ""
	}
	re, _ := regexp.Compile("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)")
	return re.ReplaceAllString(email, "$1****$3$4")
}

// ReturnPhoneNO 替换手机号中间四位为*
func ReturnPhoneNO(phone string) string {
	if len(phone) == 0 {
		return ""
	}
	re, _ := regexp.Compile("(\\d{3})(\\d{4})(\\d{4})")
	return re.ReplaceAllString(phone, "$1****$3")
}

// CheckPasswordOk
// 密码长度minLength-maxLength位，可使用字母、数字、符号组成，区分大小写，至少包含两种
// minLength: 指定密码的最小长度
// maxLength：指定密码的最大长度
// pwd：明文密码
func CheckPasswordOk(minLength, maxLength int, pwd string) bool {
	if len(pwd) < minLength {
		return false // fmt.Errorf("BAD PASSWORD: The password is shorter than %d characters", minLength)
	}
	if len(pwd) > maxLength {
		return false // fmt.Errorf("BAD PASSWORD: The password is logner than %d characters", maxLength)
	}
	// patternList := []string{`[0-9]+`, `[a-z]+`, `[A-Z]+`, `[~!@#$%^&*?_-]+`}
	isNum, _ := regexp.MatchString(`[0-9]+`, pwd)
	isLower, _ := regexp.MatchString(`[a-z]+`, pwd)
	isUpper, _ := regexp.MatchString(`[A-Z]+`, pwd)
	//isSpe, _ := regexp.MatchString(`[~!@#$%^&*?_-]+`, pwd)

	return isNum && isLower && isUpper
}

// IsAccountTwo 用户账号验证
func IsAccountTwo(phone string) bool {
	// reg, err := regexp.MatchString("^[0-9]{11}$", phone)
	reg, err := regexp.MatchString("^[A-Za-z\\d]{6,12}$", phone)
	if err != nil {
		return false
	}
	return reg
}
