/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}



/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(value) {
  return value.length >= 6
}

/**
 * 验证是否整数
 * @param rule
 * @param value
 * @param callback
 * @returns {*}
 */
export function isInteger(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else {
    const re = /^(0|\+?[1-9][0-9]*)$/
    // const re = /^[0-9]*[0-9][0-9]*$/
    const rsCheck = re.test(value)
    if (!rsCheck) {
      callback(new Error('请输入正整数'))
    } else {
      callback()
    }
  }
}

// 验证是否是[0-1]的小数
export function isDecimal(rule, value, callback) {
  if (!value) {
    return callback(new Error('输入不可以为空'))
  }
  setTimeout(() => {
    if (!Number(value)) {
      callback(new Error('请输入数字'))
    } else {
      if (value < 0 || value > 1) {
        callback(new Error('请输入[0,1]之间的数字'))
      } else {
        callback()
      }
    }
  }, 1000)
}

// 验证端口是否在[0,65535]之间
export function isPort(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else {
    const re = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/
    const rsCheck = re.test(value)
    if (!rsCheck) {
      callback(new Error('请输入在[0-65535]之间的端口值'))
    } else {
      callback()
    }
  }
}

/**
 * 是否合法IP地址
 * @param rule
 * @param value
 * @param callback
 */
export function validIP(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else {
    const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
    if (!reg.test(value)) {
      callback(new Error('请输入正确的IP地址'))
    } else {
      callback()
    }
  }
}

/**
 * 是否手机号码
 * @param rule
 * @param value
 * @param callback
 */
export function validPhone(rule, value, callback) {
  const reg = /^[1][3-9][0-9]{9}$/
  if (value === '' || value === undefined || value === null) {
    callback()
  } else {
    if ((!reg.test(value)) && value !== '') {
      callback(new Error('请输入正确的电话号码'))
    } else {
      callback()
    }
  }
}

/**
 * 是否身份证号码
 * @param rule
 * @param value
 * @param callback
 */
export function validateIdNo(rule, value, callback) {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  if (value === '' || value === undefined || value === null) {
    callback()
  } else {
    if ((!reg.test(value)) && value !== '') {
      callback(new Error('请输入正确的身份证号码'))
    } else {
      callback()
    }
  }
}

/**
 * 验证内容是否包含英文数字以及下划线
 * @param rule
 * @param value
 * @param callback
 */
export function isPassword(rule, value, callback) {
  const reg = /^[_a-zA-Z0-9]+$/
  if (value === '' || value === undefined || value === null) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error('仅由英文字母，数字以及下划线组成'))
    } else {
      callback()
    }
  }
}

/**
 * 自动检验数值的范围
 * @param rule
 * @param value
 * @param callback
 */
export function validMax20000(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (!Number(value)) {
    callback(new Error('请输入[1, 20000]之间的数字'))
  } else if (value < 1 || value > 20000) {
    callback(new Error('请输入[1, 20000]之间的数字'))
  } else {
    callback()
  }
}

export function validText10(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length > 10) {
    callback(new Error('内容太长，已超出' + (value.length - 10) + '字'))
  } else {
    callback()
  }
}

export function validText20(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length > 20) {
    callback(new Error('内容太长，已超出' + (value.length - 20) + '字'))
  } else {
    callback()
  }
}

export function validText50(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length > 50) {
    callback(new Error('内容太长，已超出' + (value.length - 50) + '字'))
  } else {
    callback()
  }
}

export function validText127(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length > 127) {
    callback(new Error('内容太长，已超出' + (value.length - 127) + '字'))
  } else {
    callback()
  }
}

export function validText255(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length > 255) {
    callback(new Error('内容太长，已超出' + (value.length - 255) + '字'))
  } else {
    callback()
  }
}

export function validText512(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length > 255) {
    callback(new Error('内容太长，已超出' + (value.length - 255) + '字'))
  } else {
    callback()
  }
}

export function validText1023(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length > 1023) {
    callback(new Error('内容太长，已超出' + (value.length - 1023) + '字'))
  } else {
    callback()
  }
}

export function validateLoginname(rule, value, callback) {
  if (!validUsername(value) && !validEmail(value)) {
    callback(new Error('Please enter the correct email'))
  } else {
    callback()
  }
}

export function validateUsername(rule, value, callback) {
  if (!validUsername(value)) {
    callback(new Error('Please enter the correct user name'))
  } else {
    callback()
  }
}

export function validateEmail(rule, value, callback) {
  if (!validEmail(value)) {
    callback(new Error('Please enter the correct email'))
  } else {
    callback()
  }
}

export function validatePassword(rule, value, callback){
  if (value.length < 6) {
    callback(new Error('The password can not be less than 6 digits'))
  } else {
    callback()
  }
}

export function validateSourceUrl(rule, value, callback){
  if (value) {
    if (validURL(value)) {
      callback()
    } else {
      callback(new Error('外链url填写不正确'))
    }
  } else {
    callback()
  }
}

/**
 * 验证是否为年
 * @param rule
 * @param value
 * @param callback
 * @returns {*}
 */
export function isYear(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()

    callback(new Error('Error year'))
  } else if (!Number(value)) {
    callback(new Error('Error year'))
  } else if (value < 1000 || value > 9999) {
    callback(new Error('Error year'))
  } else {
    const re = /^[0-9]*[1-9][0-9]*$/
    const rsCheck = re.test(value)
    if (!rsCheck) {
      callback(new Error('Error year'))
    } else {
      callback()
    }
  }
}

export function validNumberLength(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback()
  } else if (value.length != 10) {
    callback(new Error('Error length'))
  } else {
    callback()
  }
}

export function validRequired(rule, value, callback) {
  if (value === '' || value === undefined || value === null) {
    callback(new Error('Required value'))
  } else {
    callback()
  }
}
