import { v4 as uuidv4 } from 'uuid';

// 角度转弧度
export const degToRad = (deg: number): number => {
  return deg * (Math.PI / 180);
};

// 判断两个矩形是否重叠
export const checkTwoRectIsOverlap = (
  minx1: number,
  maxx1: number,
  miny1: number,
  maxy1: number,
  minx2: number,
  maxx2: number,
  miny2: number,
  maxy2: number
): boolean => {
  return maxx1 > minx2 && maxx2 > minx1 && maxy1 > miny2 && maxy2 > miny1;
};

// 创建节点唯一的id
export const createUid = (): string => {
  return uuidv4();
};

// 计算两个点的直线距离
export const getTwoPointDistance = (
  x1: number,
  y1: number,
  x2: number,
  y2: number
): number => {
  return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
};

interface Rect {
  x: number;
  y: number;
  width: number;
  height: number;
}

type Direction =
  | 'left-top'
  | 'right-top'
  | 'right-bottom'
  | 'left-bottom'
  | 'left'
  | 'right'
  | 'top'
  | 'bottom'
  | 'overlap';

// 判断两个矩形的相对位置
// 第一个矩形在第二个矩形的什么方向
export const getRectRelativePosition = (rect1: Rect, rect2: Rect): Direction => {
  // 获取第一个矩形的中心点坐标
  const rect1CenterX = rect1.x + rect1.width / 2;
  const rect1CenterY = rect1.y + rect1.height / 2;

  // 获取第二个矩形的中心点坐标
  const rect2CenterX = rect2.x + rect2.width / 2;
  const rect2CenterY = rect2.y + rect2.height / 2;

  // 判断第一个矩形在第二个矩形的哪个方向
  if (rect1CenterX < rect2CenterX && rect1CenterY < rect2CenterY) {
    return 'left-top';
  } else if (rect1CenterX > rect2CenterX && rect1CenterY < rect2CenterY) {
    return 'right-top';
  } else if (rect1CenterX > rect2CenterX && rect1CenterY > rect2CenterY) {
    return 'right-bottom';
  } else if (rect1CenterX < rect2CenterX && rect1CenterY > rect2CenterY) {
    return 'left-bottom';
  } else if (rect1CenterX < rect2CenterX && rect1CenterY === rect2CenterY) {
    return 'left';
  } else if (rect1CenterX > rect2CenterX && rect1CenterY === rect2CenterY) {
    return 'right';
  } else if (rect1CenterX === rect2CenterX && rect1CenterY < rect2CenterY) {
    return 'top';
  } else if (rect1CenterX === rect2CenterX && rect1CenterY > rect2CenterY) {
    return 'bottom';
  } else {
    return 'overlap';
  }
};

// 缩放宽度
export const zoomWidth = (ratio: number, height: number): number => {
  // w / height = ratio
  return ratio * height
}

// 缩放高度 
export const zoomHeight = (ratio: number, width: number): number => {
  // width / h = ratio
  return width / ratio
}