import { SocketEmitter, formatTimeStamp, imageMethods } from '@zenview/micro-utils';
import ServiceHttp from './service';
export function formartRects(data, type, dicts, typeCodes = []) {
  let rects = [];
  let rect = {
    default: true,
    needExpand: true,
    isDefault: true,
    smId: data.id,
    feature: data.feature || data.faceFeature || data.bodyFeature,
  };
  const tags = getDictForTagInfo(dicts, data.personTags || [], typeCodes);
  // 以下为图库、布控中心、订阅中心字段兼容
  if (data.faceRect) {
    rects.push({
      type: 'face',
      rect: data.faceRect.split(','),
      value: data.faceRect,
      url: data.faceUrl,
      tags,
      ...rect,
    });
  }
  if (data.bodyRect) {
    rects.push({
      type: 'body',
      rect: data.bodyRect.split(','),
      value: data.bodyRect,
      url: data.bodyUrl,
      tags,
      ...rect,
    });
  }
  if (data.vehicleRect) {
    if (data.vehicleType === '0') {
      rects.push({
        type: 'vehicle',
        rect: data.vehicleRect.split(','),
        value: data.vehicleRect,
        url: data.vehicleUrl,
        ...rect,
      });
    }
    if (data.vehicleType === '1') {
      rects.push({
        type: 'nonVehicle',
        rect: data.vehicleRect.split(','),
        value: data.vehicleRect,
        url: data.vehicleUrl,
        ...rect,
      });
    }
  }
  // 以下为防控任务 -- 无帽进入后厨字段兼容
  try {
    let extraInfo = JSON.parse(data.extraInfo);
    if (extraInfo.face_rect) {
      rects.push({
        type: 'face',
        value: extraInfo.face_rect.replaceAll('.0','').split(','),
        rect: extraInfo.face_rect.replaceAll('.0','').split(','),
        url: data.sceneUrl,
        ...rect,
        feature: extraInfo.face_feature,
      });
    }
    if (extraInfo.body_rect) {
      rects.push({
        type: 'body',
        value: extraInfo.body_rect.replaceAll('.0','').split(','),
        rect: extraInfo.body_rect.replaceAll('.0','').split(','),
        url: data.sceneUrl,
        ...rect,
        feature: extraInfo.body_feature,
      });
    }
  } catch (error) {}
  // 以下为防控任务 -- 除人员入侵、无帽进入后厨字段兼容
  if (type === 'thing' && data.rects && data.rects.length) {
    data.rects.forEach((rect) => {
      rects.push({ type: 'thing', value: rect, url: data.imageUrl, default: true });
    });
  }

  return rects;
}

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;
}

/**
 * 走下载审批流程
 * @param {*} param
 */
export function downloadImg({ data, imagePath }) {
  data.uploadImgUrl = imagePath;
  const modelInfoOfApply = {
    applyPicsItem: [data],
    dataArray: {
      downloadContent: [
        { label: '下载类型', value: '图片' },
        { label: '设备名称', value: data.deviceName || data.cameraName },
        { label: '抓拍时间', value: formatTimeStamp(data.captureTime) },
      ],
      imageDetails: [
        {
          url: data.uploadImgUrl,
        },
      ],
    },
    isApply: true,
    title: '下载申请',
  };
  SocketEmitter.emit('applyUploadModal', modelInfoOfApply);
}

//收藏类型
export function getPictureType(data, type) {
  let pictureType = '1';
  switch (type) {
    case 'face':
      pictureType = '2';
      break;
    case 'personAlarm':
      pictureType = '2';
      break;
    case 'body':
      pictureType = '3';
      break;
    case 'eventAlarm':
      pictureType = '3';
      break;
    case 'vehicle':
      pictureType = '4';
      break;
    case 'nonVehicle':
      pictureType = '4';
      break;
    default:
      break;
  }
  // 无帽进入特殊处理
  if (data.taskType === '101509') {
    pictureType = '3';
  }
  // 防控中心非机动车收藏特殊处理
  if (type === 'thing' && data.type === '101510') {
    pictureType = '4';
  }
  // 布控中心车辆布控告警收藏特殊处理
  if (data.taskType === '101501' || data.taskType === '101502') {
    pictureType = '4';
  }
  return pictureType;
}

// 添加图片收藏
export async function addPictureFavorite(options) {
  if (options.scenceImageUrl.indexOf('data:image') >= 0) {
    // 图片地址为base64则先上传至羚羊再添加到mediaList
    const oldFile = imageMethods.base64ToBlob(options.scenceImageUrl);
    const formData = new FormData();
    formData.append('file', oldFile);
    await ServiceHttp.uploadImg(formData).then((res) => {
      if (res.code !== 0) {
        return false;
      }
      options.scenceImageUrl = options.smallImageUrl = res.data.url;
    });
  }
  return ServiceHttp.addPictureFavorite(options);
}
