/* eslint-disable */

/**
 * [randomString 生成固定长度的随机数]
 * @param  {Number} [len=32] [字符串长度]
 * @return {[String]}
 */
const randomString = (len = 32) => {
  // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
  let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  let maxPos = $chars.length;
  let random = '';
  for (let i = 0; i < len; i++) {
    random += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return random;
}

/**
 * [getPairKey 获取后端以pair形式返回数据的key]
 * @param  {[Object]} pair [键值对,形式为 {key: 'xx', value: 'xx'}]
 * @return {[String]}      [返回key]
 */
const getPairKey = (pair) => {
  return pair.key;
}

/**
 * [getPairValue 获取后端以pair形式返回数据的value]
 * @param  {[Object]} pair [键值对,形式为 {key: 'xx', value: 'xx'}]
 * @return {[String]}      [返回value]
 */
const getPairValue = (pair) => {
  return pair.value;
}

/**
 * [stateToText]
 * @param  {[String]} state [状态]
 * @return {[String]}
 */
const stateToText = (state) => {
  let text = { '01': '启用', '02': '停用'};
  return text[state];
}

/**
 * [stringToBoolean 字符串转布尔类型]
 * @param  {[String]} str [枚举值['01','02']]
 * @return {[Boolean]}
 */
const stringToBoolean = (str) => {
  let bool = undefined;
  switch (str) {
    case '01':
      bool = true;
      break;
    case '02':
      bool = false;
      break;
    default:

  }
  return bool
}

/**
 * [booleanToString 布尔类型转字符串]
 * @param  {[type]} bool
 * @return {[String]}      [返回'01'或'02']
 */
const booleanToString = (bool) => {
  let str = undefined;
  switch (bool) {
    case true:
      str = '01';
      break;
    case false:
      str = '02';
      break;
    default:

  }
  return str;
}

/**
 * [booleanToText 布尔型转文字描述]
 * @param  {[Boolean||String]} param ['01', '02', true, false]
 * @return {[String]}
 */
const booleanToText = (param) => {
  if (param === '01' || param === true) {
    return '是';
  } else if (param === '02' || param === false) {
    return '否';
  } else {
    return null;
  }
}

/**
 * 初始化时间选择控件的选项值
 * @returns {Array}
 */
const initTimePickerData = () => {
  let data = [];
  for (let i = 0; i < 24; i++) {
    let obj = {
      value: preZero(i),
      label: preZero(i),
      children: []
    };
    for (let j = 0; j < 60; j++) {
      obj.children.push({
        value: preZero(j),
        label: preZero(j)
      })
    }
    data.push(obj);
  }
  return data;
};

const preZero = (num) => {
  if (num >= 0 && num <= 9) {
    return '0' + num;
  } else {
    return '' + num;
  }
};

const utils = {
  randomString,
  getPairKey,
  getPairValue,
  stateToText,
  booleanToString,
  stringToBoolean,
  booleanToText,
  initTimePickerData,
  preZero
};

export default utils;
