import _ from 'lodash';
import { Dict } from '@zenview/micro-basic-library';

export const faceTypeName = [
  { code: '100000', label: '性别', name: 'sex' },
  { code: '100800', label: '年龄段', name: 'generation' },
  { code: '119000', label: '是否戴帽子或头盔', name: 'hat' },
  { code: '112000', label: '头部特征', name: 'head' },
  { code: '112800', label: '是否戴眼镜', name: 'eyeGlass' },
  { code: '100250', label: '是否戴口罩', name: 'faceMask' },
  { code: '112400', label: '上身颜色', name: 'upperColor' },
  { code: '112100', label: '上身纹理', name: 'upperTexture' },
  { code: '119010', label: '是否戴墨镜', name: 'sunglasses' },
  { code: '121150', label: '肤色', name: 'skinColorTags' },
  { code: '121170', label: '面部情绪', name: 'faceMoodTags' },
  { code: '120970', label: '上衣类型', name: 'upperClothesTags' },
  { code: '119100', label: '所在区域', name: 'cameraTags' },
];

export const bodyTypeName = [
  { code: '100000', label: '性别', name: 'sex' },
  { code: '100800', label: '年龄段', name: 'generation' },
  { code: '119000', label: '是否戴帽子或头盔', name: 'hat' },
  { code: '112000', label: '头部特征', name: 'head' },
  { code: '112800', label: '是否戴眼镜', name: 'eyeGlass' },
  { code: '100250', label: '是否戴口罩', name: 'faceMask' },
  { code: '112400', label: '上身颜色', name: 'upperColor' },
  { code: '112100', label: '上身纹理', name: 'upperTexture' },
  { code: '119010', label: '是否戴墨镜', name: 'sunglasses' },
  { code: '112300', label: '随身物品', name: 'personalEffectsTags' },
  { code: '120970', label: '上衣类型', name: 'upperClothesTags' },
  { code: '112200', label: '下身类别', name: 'lowerTexture' },
  { code: '102600', label: '下身服饰', name: 'lowerBodyClothing' },
  { code: '112500', label: '下身颜色', name: 'lowerColor' },
  { code: '120980', label: '鞋子类型', name: 'shoesTypeTags' },
  { code: '131730', label: '鞋子颜色', name: 'shoesColorTags' },
  { code: '120960', label: '发型', name: 'hairstyleTags' },
  { code: '119100', label: '所在区域', name: 'cameraTags' },
];

export const vehicleTypeName = [
  { code: '117100', label: '机动车品牌', name: 'vehicleBrands' },
  { code: '117500', label: '机动车类型', name: 'vehicleType' },
  { code: '118800', label: '车牌类型', name: 'plateTypeTags' },
  { code: '117750', label: '车牌颜色', name: 'plateColor' },
  { code: '119380', label: '汽车车身', name: 'vehicleBodyTags' },
  { code: '119320', label: '车身颜色', name: 'vehicleColor' },
  { code: '119200', label: '汽车配饰', name: 'accessoryTags' },
  { code: '119370', label: '乘车人状态', name: 'abnormalTags' },
  { code: '121050', label: '车牌遮挡', name: 'plateOcclusionTags' },
  { code: '121070', label: '面部遮挡', name: 'faceOcclusionTags' },
  { code: '119390', label: '副驾是否有人', name: 'copilotCode' },
  { code: '116350', label: '车辆抓拍方向', name: 'directionCode' },
  { code: '119100', label: '所在区域', name: 'cameraTags' },
];

export const nonVehicleTypeName = [
  { code: '100000', label: '性别', name: 'sex' },
  { code: '100800', label: '年龄段', name: 'generation' },
  { code: '112000', label: '头部特征', name: 'head' },
  { code: '119280', label: '非机动车车型', name: 'nonMotorVehicleModel' },
  { code: '119320', label: '车身颜色', name: 'vehicleColor' },
  { code: '119300', label: '上身纹理', name: 'upperColorType' },
  { code: '112400', label: '上身颜色', name: 'upperColor' },
  { code: '119240', label: '非机动车车辆角度', name: 'directionTags' },
  { code: '119350', label: '违法载人', name: 'illegalMannedTags' },
  { code: '120960', label: '发型', name: 'hairstyleTags' },
  { code: '120970', label: '上衣类型', name: 'upperClothesTags' },
  { code: '119260', label: '非机动车包款式', name: 'nonMotorVehicleType' },
  { code: '119100', label: '所在区域', name: 'cameraTags' },
];

export function formatRectInfo(structuredInfo, id, typeCodes, defaultRect = []) {
  const {persons,vehicles,boundaries} = structuredInfo||{}
  let targetRect = '';
  try {
    targetRect = defaultRect[0].rect.join(',') || '';
  } catch (error) {}
  const rects = [];
  if (persons) {
    persons.forEach((item) => {
      let personTags = (item.personTags && item.personTags.filter((tag) => tag.length === 6)) || [];
      let faceDicts = [],
        bodyDicts = [];
      getTargetTag(faceTypeName, faceDicts);
      getTargetTag(bodyTypeName, bodyDicts);

      let faceTags = getDictForTagInfo(faceDicts, personTags || [], typeCodes);
      let bodyTags = getDictForTagInfo(bodyDicts, personTags || [], typeCodes);
      if (item.faceRect) {
        rects.push({
          imgId: item.imgId,
          smId: item.id,
          imgUrl: item.faceUrl,
          type: 'face',
          rect: item.faceRect.split(',').map((v) => +v) || [],
          needExpand: true,
          tags: _.orderBy(faceTags, ['typeCode'], ['asc']),
          // isDefault: id === item.id,
          isDefault: targetRect == item.faceRect,
          feature: item.faceFeature,
          lowQuality: item?.faceLowQuality,
          dupId: item?.dupId,
        });
      }
      if (item.bodyRect) {
        rects.push({
          imgId: item.imgId,
          smId: item.id,
          imgUrl: item.bodyUrl,
          type: 'body',
          rect: item.bodyRect.split(',').map((v) => +v) || [],
          needExpand: true,
          tags: _.orderBy(bodyTags, ['typeCode'], ['asc']),
          // isDefault: id === item.id,
          isDefault: targetRect == item.bodyRect,
          feature: item.bodyFeature,
          lowQuality: item?.bodyLowQuality,
          dupId: item?.dupId,
        });
      }
    });
  }
  if (vehicles) {
    vehicles.forEach((item) => {
      const type = item.vehicleType === 0 ? 'vehicle' : 'nonVehicle';

      let tags = (item.vehicleTags && item.vehicleTags.filter((tag) => tag.length === 6)) || [];
      let vehicleDicts = [],
        nonVehicleDicts = [];
      getTargetTag(vehicleTypeName, vehicleDicts);
      getTargetTag(nonVehicleTypeName, nonVehicleDicts);
      let vehicleTags = getDictForTagInfo(vehicleDicts, tags || [], typeCodes);
      let nonVehicleTags = getDictForTagInfo(nonVehicleDicts, tags || [], typeCodes);

      rects.push({
        imgId: item.imgId,
        smId: id,
        type,
        imgUrl: item.vehicleUrl,
        feature: item.feature,
        tags: item.vehicleType === 0 ? _.orderBy(vehicleTags, ['typeCode'], ['asc']) : _.orderBy(nonVehicleTags, ['typeCode'], ['asc']),
        rect: item.vehicleRect ? item.vehicleRect.split(',').map((v) => +v) : [],
        // isDefault: id === item.id,
        isDefault: targetRect == item.vehicleRect,

        lowQuality: item.vehicleLowQuality,
        dupId: item.dupId,
      });
    });
  }
  if(boundaries&&boundaries.length){
    boundaries.forEach(item => {
      item.map(polygonAreaItem => {
        if(polygonAreaItem.polygonArea){
          try {
            polygonAreaItem.polygonArea=polygonAreaItem.polygonArea.map(v=>JSON.parse(v))
            
          } catch (error) {
            console.error(error);
          }
        }
        return polygonAreaItem
      })
      rects.push({
        type:'roi',
        boundaries:item
      });
    })
  }

  return rects;
}

/**
 * 过滤要显示的tag
 */
export function getTargetTag(filterTag, result = []) {

  let allDict = {}
  try {
    allDict = Dict.cacheMap.data;
  } catch (error) {
    
  }
  filterTag.forEach((v) => {
    let tag = allDict[v.code];
    tag && result.push(tag);
  });
}

/**
 * 获取显示tag的icon信息
 */
export function getDictForTagInfo(dicts = [], tags, typeCodes = []) {
  let tagInfos = [];
  for (let i = 0, l = dicts.length; i < l; i++) {
    const item = dicts[i];
    for (let i = 0, l = tags.length; i < l; i++) {
      const tag = tags[i];
      const tagInfo = item.find((v) => +v.code === +tag);
      if (tagInfo) {
        let icon = tagInfo.icon;
        if (!icon) {
          const findIcon = typeCodes.find((type) => type.icon && type.code === tagInfo.typeCode);
          icon = findIcon ? findIcon.icon : '';
        }
        tagInfos.push({ ...tagInfo, icon });
        break;
      }
    }
  }

  return tagInfos;
}
