import moment from 'moment'
// import { Dict } from '@zenview/micro-basic-library'
import { BaseStore } from '@zenview/micro-basic-library'
import { relationDeviceType } from './dict'

export const mapLabelType = [
  { value: ["119903"], label: "基本属性", color: "#B9B5FF",name:'basicAttributes' },
  { value: ["119904"], label: "身份", color: "#FF837D",name:'identity' },
  {
    value: ["119905", "119922", "119923", "119924", "119925", "119926", "119927", "120131"],
    label: "外观规律",
    color: "#50E9B2",
    name:'appearanceRule'
  },
  { value: ["119906", "119920"], label: "时空规律", color: "#54BEFF",name:'spatioTemporalLaw' },
  { value: ["119907"], label: "行为规律", color: " #FFBB22",name:'behaviorRule' }
]

export const vehicleType = [
  { value: ["119929"], label: "身份", color: "#FF837D",name:'identity' },
  { value: ["119928"], label: "时空规律", color: "#54BEFF",name:'spatioTemporalLaw' },
]

export const mapGeneration = [
  {"id":"1007053","code":"100801","name":"小孩","typeCode":"100800","icon":"icon-S_AID_AID_AgeBaby","value":"100801","label":"小孩","typeName":"generation"},
  {"id":"1007052","code":"100802","name":"青年","typeCode":"100800","icon":"icon-S_AID_AID_AgeYoung","value":"100802","label":"青年","typeName":"generation"},
  {"id":"1007111","code":"100803","name":"中年","typeCode":"100800","icon":"icon-S_AID_AID_AgeMen","value":"100803","label":"中年","typeName":"generation"},
  {"id":"1007051","code":"100804","name":"老人","typeCode":"100800","icon":"icon-S_AID_AID_AgeOld","value":"100804","label":"老人","typeName":"generation"}
]

export let _LM_Event_ = {
  names: {
    updatePerson: 'updatePerson',
  }
}

export let LM_DB_NAMES = {
  personDetail: 'personDetail',
  searchParams: 'searchParams'
}

export function formartTagCodes(list=[], isNoTree) {
  list.forEach(item => {
    item.value = item.code = item.dicCode;
    item.typeCode = item.superTag;
    item.label = item.tagName;
    const group = mapLabelType.find(v => v.value.indexOf(item.tagGroup) > -1);
    if (group) {
      item.tagColor = group.color;
    }
  });
  if (isNoTree) {
    return list;
  }
  return computTreeList(list, "code", "typeCode");
}

export function computTreeList(list, id = 'id', pid = 'parentId', isNoDeep) {
  let treeData;
  if (!isNoDeep) {
    treeData = window._.cloneDeep(list);
  } else {
    treeData = list;
  }
  let treeMap = {};

  treeData.forEach((v) => {
    treeMap[v[id]] = v;
  });

  let arr = [];
  for (let i = 0, l = treeData.length; i < l; i++) {
    const item = treeData[i];
    let hasParent = false;
    if (item[pid] && treeMap[item[pid]]) {
      hasParent = true;
      const item2 = treeMap[item[pid]];
      !Array.isArray(item2.children) && (item2.children = []);
      item2.children.push(item);
    }
    !hasParent && arr.push(i);
  }
  treeMap = null;
  return treeData.filter((_, index) => arr.includes(index));
}

export function computedGrid(grid) {
  const contentGrid = { xxl: 0, xl: 0, lg: 0, md: 0, sm: 0, xs: 0 };
  if (Array.isArray(grid)) {
    grid.forEach(item => {
      for (let k in item) {
        if (contentGrid[k]) {
          contentGrid[k] = contentGrid[k] - item[k];
        } else {
          contentGrid[k] = 24 - item[k];
        }
      }
    });
  } else {
    for (let k in grid) {
      contentGrid[k] = 24 - grid[k];
    }
  }
  for (let k in contentGrid) {
    if (contentGrid[k] <= 0) {
      contentGrid[k] = 24;
    }
  }
  return contentGrid;
}


/**
 * 生成UUID
 */
export function uuid() {
  let s = [];
  let hexDigits = '0123456789abcdef';
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = '4';
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = '-';

  let uuidStr = s.join('');
  return uuidStr;
}

export const simpleGrid = {
  xxl: 4,
  xl: 6,
  lg: 6,
  md: 7,
  sm: 24,
  xs: 24
};

/**
 * 处理location.search的方法,将字符串转换成json
 * @param {string} search
 */
export function queryFormat(search = "") {
  let params = {};
  if (search === "") {
    return params;
  }
  if (typeof search === "string") {
    search = search.indexOf("?") < 0 ? search : search.substr(search.indexOf("?") + 1);
    let a = search.split("&");
    let b = a.map(v => v.split("="));
    b.map(v => (params[v[0]] = v[1]));
  }
  return params;
}

export function computedCuurentGrid() {
  const width = document.body.clientWidth;
  if (width >= 1600) {
    return "xxl";
  }
  if (width >= 1200 && width < 1600) {
    return "xl";
  }
  if (width >= 992 && width < 1200) {
    return "lg";
  }
  if (width >= 768 && width < 992) {
    return "md";
  }
  if (width >= 576 && width < 768) {
    return "sm";
  }
  if (width < 576) {
    return "xs";
  }
}

// 切割数组 1维变2维，分组聚合展示
export function arraySliceForXG(arr, x = 3, isGroupTitle) {
  let newArr = [];
  for (let i = 0, l = arr.length; i < l; i++) {
    if (newArr.length === 0) {
      newArr.push([]);
    }
    if (isGroupTitle(arr[i])) {
      if (newArr[newArr.length - 1].length === 0) {
        newArr[newArr.length - 1].push(arr[i]);
      } else {
        newArr.push([arr[i]]);
      }
      newArr.push([]);
    } else {
      if (newArr[newArr.length - 1].length > x - 1) {
        newArr.push([]);
      }
      newArr[newArr.length - 1].push(arr[i]);
    }
  }
  return newArr;
}

// 切割数组 1维变2维
export function arraySliceForX(arr, x = 3) {
  let newArr = [];
  for (let i = 0, l = arr.length; i < l; i++) {
    if (newArr.length === 0) {
      newArr.push([]);
    }
    if (newArr[newArr.length - 1].length > x - 1) {
      newArr.push([]);
    }
    newArr[newArr.length - 1].push(arr[i]);
  }
  return newArr;
}

/**
 * @desc 时间戳格式化
 */
export const formatTimeStamp = (value, format = 'YYYY.MM.DD HH:mm:ss') => {
  return value ? moment(parseInt(value, 10)).format(format) : null
}

export function getPlaceType() {
  if(window.dictMap){
    const placeType = window.dictMap['119100']
    const placeTypeEntertainment = window.dictMap['119113']
    const placeTypeSchool = window.dictMap['119104']
    const placeTypeStation = window.dictMap['119109']
    return [].concat(placeType, placeTypeEntertainment, placeTypeSchool, placeTypeStation)
  }
}

/**
 * 获取设备类型对应的图片
 */
export function getDeviceInfo(cid,list) {
  let deviceList =list|| BaseStore.device.deviceList
  let deviceInfo = deviceList.find(v => v.cid === cid) || {}
  let deviceType = deviceInfo.deviceType
  let item = relationDeviceType.find(v => v.value.split(',').includes(deviceType) ) || {}
  return item
}