package auth

import (
	"crypto/rand"
	"eta/eta_mini_ht_api/common/exception"
	stringUtils "eta/eta_mini_ht_api/common/utils/string"
	"math/big"
	"regexp"
)

const (
	letterBytes = "0123456789"
)

func GenerateCode(length int) (string, error) {
	if length <= 0 {
		return "", exception.New(exception.IllegalCodeLength)
	}
	b := make([]byte, length)
	for i := range b {
		n, err := rand.Int(rand.Reader, big.NewInt(int64(len(letterBytes))))
		if err != nil {
			return "", err
		}
		b[i] = letterBytes[n.Int64()]
	}
	return string(b), nil
}

func IsValidMobile(mobile string) bool {
	if stringUtils.IsBlank(mobile) {
		return false
	}
	regex := `^(13[0-9]|14[5-9]|15[0-3,5-9]|16[6]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$`
	matched, _ := regexp.MatchString(regex, mobile)
	return matched
}