
function legitimacyCheck () {
  //判断ip合法性
  function ipCheck (ipString) {
    var 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])$/
    return reg.test(ipString)
  }

  //判断长度合法性
  function textLengthCheck (textString) {
    if (textString.length > 20) {
      return false
    }
    else {
      return true
    }
  }
  //判断长度合法性
  function textLengthCheckForConfFileName (textString) {
    if (textString.length > 35) {
      return false
    }
    else {
      return true
    }
  }

  //判断数字合法性
  function numberCheck (numberString) {
    var isNumber = true
    var numReg = /^-?[0-9]\d*$/
    var numRe = new RegExp(numReg)

    if (!numRe.test(numberString)) {
      isNumber = false
    }
    //端口号应该在-2147483648-2147483647之间
    if (parseInt(numberString) > 2147483647 || parseInt(numberString) < -2147483648) {
      isNumber = false
    }
    //长度不能大于20
    if (numberString.length > 20) {
      isNumber = false
    }
    return isNumber
  }

  //判断文本是否为空
  function textNullCheck (text) {
    if(text === null){
      return false
    }
    else if (text.length === 0 ) {
      return false
    }
    else if($.trim(text).length===0 ){
      return false
    }
    return true
  }

  //字符串非空检验
  function stringNullCheck (checkedString) {
    let isNull = false
    if (checkedString === '') {
      isNull = true
    }
    return isNull
  }

  //时间合法性检验
  function timeCheck (timeString) {
    var isTime = false
    var timeRegex = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-" +
      "(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})" +
      "(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+" +
      "([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]).([0-9]{3})$"
    var Regex = new RegExp(timeRegex)
    if (Regex.test(timeString)) {
      isTime = true
    }
    return isTime
  }

  //监测周期合法性
  function periodCheck(periodString) {
    if (periodString === null || periodString === '') {
      return false
    } else {
      return true
    }
  }

  //起始时间不能大于终止时间
  function timeLegitmacyCheck (beginData, endData) {
    if (beginData > endData) {
      return false
    } else {
      return true
    }
  }

  //查询时段不能超过24小时
  function timeSlotCheck (beginData, endData) {
    if ((endData.getTime() - beginData.getTime()) > 24 * 60 * 60 * 1000) {
      return false
    } else {
      return true
    }
  }

  // 正整数判断
  function positiveIntegerCheck (NumberString) {
    // var val = parseInt(NumberString)
    var regu = /(^[1-9]\d*$)/
    if (!regu.test(NumberString)) {
      return false
    } else {
      return true
    }
  }

  //非负判断
  function NumberCheck (NumberString) {
    var val = parseInt(NumberString)
    var regu = /^\d+$/;
    if (!regu.test(val)) {
      return false
    } else {
      return true
    }
  }

  //检验数组是否按大小排序
  function numberLowToHigt(numberList) {
    for (var i = 0; i < numberList.length - 1; i++) {
      if (numberList[i] > numberList[i+1]) {
        return false
      }
    }
    return true
  }

  //检验数组是否按大到小排序
  function numberHighToLow(numberList) {
    for (var i = 0; i < numberList.length - 1; i++) {
      if (numberList[i] < numberList[i+1]) {
        return false
      }
    }
    return true
  }

  //电话合法性检验
  function phoneNumberCheck (phoneNumber) {
    let isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/; //手机号码
    let isMob = /^0?1[3|4|5|8][0-9]\d{8}$/;  // 座机格式
    if (isMob.test(phoneNumber) || isPhone.test(phoneNumber) || phoneNumber===''){
      return true
    } else {
      return false
    }
  }

  //密码非空检测
  function passwordCheck (passworld) {
    let isTrue = false
    if (passworld !== '') {
      isTrue = true
    }
    return isTrue
  }

  //端口检验，必须是数字
  function portNameCheck (text) {

    var reg = /^[0-9]*$/
    var re = new RegExp(reg)
    if (!re.test(text)) {
      return true
    } else {
      return false
    }
  }

  //判断两次密码是否相等
  function passwordIsEqual (originalPassword, newPassword) {
    let isEqual = false
    if (originalPassword === newPassword) {
      isEqual = true
    }
    return isEqual
  }

  //上下限连续性检验
  function continuityCheck(NumberList) {
    let newNumberList = []

    //找出数组中的非空值
    for (var i = 0; i < NumberList.length; i++) {
      if(NumberList[i] !== '') {
        newNumberList.push(parseFloat(NumberList[i]))
      }

      //判断非空的数是否按大小排序
      if(newNumberList.length >= 2) {
        if(!numberLowToHigt(newNumberList)) {
          if(!numberHighToLow()) {
            return false
          }
        }
      }

      //根据非空值的数量判断各种合法情况
      if(newNumberList.length === 0) {
        return false
      } else if(newNumberList.length === 1) {
        return true
      } else if (newNumberList.length === 2) {
        //原数组中0 1或2 3或4 5号位不为空肯定合法
        if((NumberList[0] !== '' && NumberList[1]) !== '' || (NumberList[2] !== '' && NumberList[3] !== '') || (NumberList[4] !== '' && NumberList[5] !== ''))
        {
          return true
        }
        //原数组中0 3或3 5或0 5号位不为空肯定合法
        else if ((NumberList[0] !== '' && NumberList[3]) !== '' || (NumberList[3] !== '' && NumberList[5] !== '') || (NumberList[0] !== '' && NumberList[5] !== '')) {
          return true
        }
        //原数组中1 2或3 4号位相等肯定合法
        else if(NumberList[1] === NumberList[2] || NumberList[3] === NumberList[4]) {
          return true
        } else {
          return false
        }
      } else if (newNumberList.length === 3) {
        //原数组中1 2或3 4号位相等肯定合法
        if (NumberList[1] === NumberList[2]) {
          if(NumberList[4] !== '') {
            return false
          }
        } else if (NumberList[3] === NumberList[4]){
          if(NumberList[0] !== ''){
            return false
          }
        } else if (NumberList[1] === NumberList[4]) {
          if(NumberList[2] !== '' || NumberList[3] !== '') {
            return false
          }
        } else {
          return true
        }
      } else if (newNumberList.length === 4) {
        if (NumberList[0] === '') {
          if (NumberList[1] === NumberList[2] && NumberList[3] === NumberList[5]) {
            return true
          } else {
            return false
          }
        }
        else {
          if (NumberList[1] === NumberList[4]) {
            return true
          } else {
            return false
          }
        }
      } else if (newNumberList.length === 5 || newNumberList.length === 6) {
        if (NumberList[1] === NumberList[2] && NumberList[3] === NumberList[4]) {
          return true
        } else {
          return false
        }
      }
    }
  }

  //判断两个list是否相等
  function checkListEqual (list1, list2) {
    for (var i = 0; i < list1.length; i++) {
      let number = list2.indexOf(list1[i])
      if (number === -1) {
        return false
      }
    }
    return true
  }

  //判断结构体相等
  function compareObject (object1, object2) {
    //object1, object2均为未定义
    if (object1 === object2) {
      return true
    }

    //判断是否为结构体
    if (!(object1 instanceof Object) || !(object2 instanceof Object)) {
      return false
    }

    //判断内部属性是否相同
    if (object1.constructor !== object2.constructor) {
      return false
    }

    for (var arrtibute in object1) {
      //object1的属性是否等于object2的属性
      if (object1.hasOwnProperty(arrtibute)) {
        // Allows comparing x[ p ] and y[ p ] when set to undefined
        if (!object2.hasOwnProperty(arrtibute)) {
          return false
        }

        // 判断值严格相等，未定义算相等
        if (object1[arrtibute] === object2[arrtibute]) {
          continue
        }

        // Numbers, Strings, Functions, Booleans must be strictly equal
        if (typeof (object1[arrtibute]) !== "object") {
          return false
        }

        if (!Object.equals(object1[arrtibute], object2[arrtibute])) {
          return false
        }
      }
    }
    for (arrtibute in object2) {
      // allows x[ p ] to be set to undefined
      if (object2.hasOwnProperty(arrtibute) && !object1.hasOwnProperty(arrtibute)) {
        return false
      }
    }
    return true
  }

  // 端口列表校验
  function portCheck (portString) {
    //判断“，”合法性，防止多个‘，’连接
    var tempPortList0 = new Array()
    tempPortList0 = portString.split('');
    for (var i = 0; i < tempPortList0.length; i++) {
      if (tempPortList0[i] === ',' && tempPortList0[i + 1] === ',') {
        return false
      }
    }
    if(tempPortList0[tempPortList0.length-1] === ","){
      return false
    }
    let tempPortList1 = portString.split(',');
    for(let j=0; j<tempPortList1.length; j++){
      let numberIs = this.numberCheck(tempPortList1[j])
      if(numberIs === false){
        return false;
      }
    }
    return true
  }

  //TODO 时间长度合法性

  return Object.freeze({
    ipCheck,
    textLengthCheck,
    textLengthCheckForConfFileName,
    numberCheck,
    textNullCheck,
    timeCheck,
    periodCheck,
    stringNullCheck,
    timeLegitmacyCheck,
    timeSlotCheck,
    positiveIntegerCheck,
    NumberCheck,
    continuityCheck,
    phoneNumberCheck,
    passwordCheck,
    portNameCheck,
    passwordIsEqual,
    checkListEqual,
    compareObject,
    portCheck
  })
}

export default legitimacyCheck
