import { cloneDeep, flatten } from 'lodash';

export function customContextMenu(e, menu) {
  e.preventDefault();
  return false;
}

export function cloneImageNode(image) {
  if (!image) {
    return Promise.resolve();
  }
  let newImage = image.cloneNode();
  return new Promise((resolve, reject) => {
    newImage.addEventListener(
      'load',
      function () {
        resolve(this);
      },
      false
    );
    newImage.addEventListener(
      'error',
      function (e) {
        reject(e);
      },
      false
    );
  });
}

export function computedRectScale({ imageSource, domImg, rects }) {
  if (!imageSource || !domImg) {
    return [];
  }
  const width = domImg.width;
  const height = domImg.height;
  let scaleOptions = {
    scaleX: width / imageSource.width,
    scaleY: height / imageSource.height,
  };
  return rects.map((item) => {
    let rect = item.needExpand ? getCaptureRect(cloneDeep(item.rect), item.type) : cloneDeep(item.rect);

    rect[0] = Math.round(rect[0] * scaleOptions.scaleX);
    rect[0] < 0 && (rect[0] = 0);

    rect[2] = Math.round(rect[2] * scaleOptions.scaleX);
    rect[2] < 0 && (rect[2] = 0);

    rect[1] = Math.round(rect[1] * scaleOptions.scaleY);
    rect[1] < 0 && (rect[1] = 0);

    rect[3] = Math.round(rect[3] * scaleOptions.scaleY);
    rect[3] < 0 && (rect[3] = 0);

    rect[2] > width - rect[0] && (rect[2] = width - rect[0]);
    rect[3] > height - rect[1] && (rect[3] = height - rect[1]);

    return {
      ...item,
      rect,
    };
  });
}
export function computedDrawAreaScale({ imageSource, domImg, drawArea = [] }) {
  if (!imageSource || !domImg) {
    return [];
  }
  const width = domImg.width;
  const height = domImg.height;
  let scaleOptions = {
    scaleX: width / imageSource.width,
    scaleY: height / imageSource.height,
  };

  return cloneDeep(drawArea).map(item => {
    item[0] = Math.round(item[0] * scaleOptions.scaleX);
    item[0] < 0 && (item[0] = 0);
    item[1] = Math.round(item[1] * scaleOptions.scaleY);
    item[1] < 0 && (item[1] = 0);

    return { rect: item, color: '#127aff' }
  })
}
export function computedRoiRectScale({ imageSource, domImg, roiRects = [] }) {
  if (!imageSource || !domImg) {
    return [];
  }
  let allRoiRects = flatten(roiRects.map(v => v.boundaries)) || []
  allRoiRects = allRoiRects.map(item => {
    let polygonArea = item.polygonArea || []
    item.rect = polygonArea.map(v => {
      return [Math.round(v.x * imageSource.width), Math.round(v.y * imageSource.height)]
    })
    return item
  })
  const width = domImg.width;
  const height = domImg.height;


  let scaleOptions = {
    scaleX: width / imageSource.width,
    scaleY: height / imageSource.height,
  };
  let arr = cloneDeep(allRoiRects).map(roiRect => {
    roiRect.rect = roiRect.rect.map(item => {
      item[0] = Math.round(item[0] * scaleOptions.scaleX);
      item[0] < 0 && (item[0] = 0);
      item[1] = Math.round(item[1] * scaleOptions.scaleY);
      item[1] < 0 && (item[1] = 0);
      return item
    })
    return roiRect
  })
  return arr

}


export function getCaptureRect(rect, type) {
  let rectArea = rect;
  if (rectArea.length !== 4) {
    console.error('区域有误 实例：string = "x,y,w,h"! ', rect);
    return [0, 0, 0, 0];
  }
  let x, y, w, h;
  switch (type) {
    case 'body':
      x = rectArea[0] - rectArea[2] * 0.2;
      y = rectArea[1] - rectArea[3] * 0.2;
      w = rectArea[2] * 1.4;
      h = rectArea[3] * 1.4;
      break;
    case 'nonVehicle':
    case 'vehicle':
    case 'thing':
      x = rectArea[0];
      y = rectArea[1];
      w = rectArea[2];
      h = rectArea[3];
      break;
    default:
      // face
      x = rectArea[0] - rectArea[2] * 0.8;
      y = rectArea[1] - rectArea[3] * 1.5;
      w = rectArea[2] * 2.6;
      h = rectArea[3] * 3.2;
      break;
  }
  return [x, y, w, h];
}

