import { validateLowerCase, validateNumberAndLowerCase } from '@/utils/validate'

const USERNAME_DIC: Array<string> = [
  'admin',
  'zhangsan',
  'lisi',
  'wangwu',
  'abc',
  'asdf',
  'qwer',
  'zxcv',
  '123',
  'aaa',
  'bbb',
  'ccc',
  'ddd',
  'eee',
  'fff',
  'ggg',
  'hhh',
  'iii',
  'jjj',
  'kkk',
  'lll',
  'mmm',
  'nnn',
  'ooo',
  'ppp',
  'qqq',
  'rrr',
  'sss',
  'ttt',
  'uuu',
  'vvv',
  'www',
  'xxx',
  'yyy',
  'zzz'
]
/**
 * 校验用户名
 * @param rule
 * @param value
 * @param callback
 */
function usernameValidator(value: string | undefined, callback: (errMsg?: string) => void) {
  if (value === '' || !value) {
    callback('用户名不能为空')
  } else if (value.length < 3 || value.length > 20) {
    callback('用户名长度为3-20个字符')
  } else if (!validateLowerCase(value.substring(0, 1))) {
    callback('用户名只能由小写字母开头')
  } else if (!validateNumberAndLowerCase(value)) {
    callback('用户名只能由小写字母和数字组成')
  } else {
    for (let i = 0; i < USERNAME_DIC.length; i += 1) {
      const str = USERNAME_DIC[i]
      if (value.indexOf(str) >= 0) {
        callback(`请勿包含简易字符[${str}]`)
        return
      }
    }
    callback()
  }
}

/**
 * 校验密码
 * @param rule
 * @param value
 * @param callback
 */
export function passwordValidator(value: string | undefined, callback: (errMsg?: string) => void) {
  if (value === '' || !value) {
    callback()
  } else if (value.length < 6 || value.length > 20) {
    callback('密码长度在6-20个字符之间')
  }
  // else if (!/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-./\\]).{8,}$/.test(value)) {
  //   callback('密码须至少包含一个大小写字母、数字和特殊符合')
  // }
  else {
    callback()
  }
}
/**
 * 校验支付密码
 * @param rule
 * @param value
 * @param callback
 */
export function passwordPayValidator(
  value: string | undefined,
  callback: (errMsg?: string) => void
) {
  if (value === '' || !value) {
    callback()
  } else if (value.length < 6 || value.length > 20) {
    callback('密码长度在6-20个字符之间')
  } else {
    callback()
  }
}

export const usernameRule = [
  { required: true, message: '用户名不能为空' },
  { validator: usernameValidator }
]

export const passwordRule = [
  { required: true, message: '密码不能为空' },
  {
    validator: passwordValidator
  }
]
export const passwordPayRule = [
  { required: true, message: '支付密码不能为空' },
  {
    validator: passwordPayValidator
  }
]
export const passwordWithBlankRule = [{ validator: passwordValidator }]
