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

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @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)
}
// 校验保留两位小数金额
export const isMoney = (rule, value, callback) => {
  var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  if (reg.test(value)) {
    if (value >= 100000000)
      callback(new Error('金额必须小于100000000'));
     else
      callback()
  } else {
    callback(new Error('请输入金额'));
  }
}

export function checkMaxVal(rule, value, callback) {
  if (value < 0 || value > 365) {
    callback(new Error('请输入[0,365]之间的数字'));
  } else {
    callback();
  }
}
export function checkTargets(rule, value, callback) {
  value = value.trim();
  if (value.startsWith(",") || value.endsWith(",")) {
    callback(new Error("不能以逗号开头或结尾"));
  }
  let ip1 = /^(?:(?:^|,|-)(?:[1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:[0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3})+$/;
  let ip2 = /^(?:(?:^|,)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\/[0-9]{0,4})+$/;
  if (!ip1.test(value) && !ip2.test(value)) {
    callback(new Error('输入正确的IP地址,多个地址用逗号分隔,如:192.168.3.1,192.168.3.1/24,192.168.3.1-196.168.3.24'));
  } else {
    let items = value.split(",");
    for (let index in items) {
      if (items[index].trim() != '') {
        if (items[index].indexOf("-") > 0) {
          let start = items[index].split("-")[0];
          let end =  items[index].split("-")[1];
          if (ip2int(start) >= ip2int(end)) {
            callback(new Error('IP地址段的终止地址必须大于起始地址'));
          }
        }
      }
    }
    callback();
  }
}
export function checkPorts(rule, value, callback) {
  value = value.trim();
  if (value == '') {
    callback();
  }
  if (value.startsWith(",") || value.endsWith(",")) {
    callback(new Error("不能以逗号开头或结尾"));
  }
  let portRule = /^(?:(?:^|,|-)([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]))+$/;
  if (!portRule.test(value)) {
    callback(new Error('请输入[0-65535]之间的数字,多个端口或端口范围使用逗号分隔,如:12,13-14'));
  } else {
    let items = value.split(",");
    for (let index in items) {
      if (items[index].trim() != '') {
        if (items[index].indexOf("-") > 0) {
          let start = items[index].split("-")[0];
          let end = items[index].split("-")[1];
          if (start >= end) {
            callback(new Error('端口范围的终止端口必须大于起始端口'));
          }
        }
      }
    }
    callback();
  }
}
//IP转成整型
function ip2int(ip){
  let num = 0;
  ip = ip.split(".");
  num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]);
  num = num >>> 0;
  return num;
}
