import { useProjectStore } from "@/store/modules/project";
import { calcComboInfo } from "@/utils/calc";

const ABSORB_DISTANCE = 10;
export function useAdsorb() {
  const projectStore = useProjectStore();

  const getMoveAdsorb = ( move: number, xy: number, wh: number, targetVal: number) => {
    // 始端吸附
    if (Math.abs((xy + move) - targetVal) < ABSORB_DISTANCE) {
      move = targetVal - xy;
    }
    // 中间吸附
    if (Math.abs((xy + wh / 2 + move) - targetVal) < ABSORB_DISTANCE) {
      move = targetVal - xy - wh / 2;
    }
    // 终端吸附
    if (Math.abs((xy + wh + move) - targetVal) < ABSORB_DISTANCE) {
      move = targetVal - xy - wh;
    }
    
    return move;
  }

  const getScaleAdsorb = ( xy: number, wh: number, targetVal: number) => {
    // 始端吸附
    if (Math.abs(xy - targetVal) < ABSORB_DISTANCE) {
      xy = targetVal;
    }
    // 中间吸附
    if (Math.abs((xy + wh / 2 ) - targetVal) < ABSORB_DISTANCE) {
      xy = targetVal - wh / 2;
    }
    // 终端吸附
    if (Math.abs((xy + wh ) - targetVal) < ABSORB_DISTANCE) {
      xy = targetVal - wh;
    }

    if (Math.abs(xy + wh - targetVal) < ABSORB_DISTANCE) {
      wh = targetVal - xy;
    }
    
    return {xy, wh};
  }

  /**
   * 获取移动吸附信息
   * @param moveX 移动x坐标
   * @param moveY 移动y坐标
   * @param moveW 移动宽度
   * @param moveH 移动高度
   */
  const getMoveAdsorbInfo = ({ moveX, moveY }: { moveX: number, moveY: number }) => {
    // 1、获取当前操作元素
    const selcetedElements = projectStore.currentSelectedElements;
    // 2、获取所有组件及参考线
    const referLines = projectStore.showReffer ? projectStore.referLines : [];
    const elements = projectStore.elements.filter(item => selcetedElements.findIndex(ele => ele.key === item.key) === -1);
    // 3、将当前的操作元素当作一个整体 计算出x y w h
    const { minX: x, minY: y, maxX, maxY } = calcComboInfo(selcetedElements);
    const w = maxX - x;
    const h = maxY - y;

    // 组件间吸附
    elements.forEach(item => {
      const { x: itemX = 0, y: itemY = 0, width = 0, height = 0 } = item.container.props;
      // 左
      moveX = getMoveAdsorb(moveX, x, w, itemX);
      // 中
      moveX = getMoveAdsorb(moveX, x, w, itemX + (width / 2));
      // 右
      moveX = getMoveAdsorb(moveX, x, w, itemX + width);

      // 上
      moveY = getMoveAdsorb(moveY, y, h, itemY);
      // 中
      moveY = getMoveAdsorb(moveY, y, h, itemY + (height / 2));
      // 下
      moveY = getMoveAdsorb(moveY, y, h, itemY + height);
    })

    referLines.forEach(line => {
      if (line.type === "vertical") {
        moveY = getMoveAdsorb(moveY, y, h, line.value);
      } else {
        moveX = getMoveAdsorb(moveX, x, w, line.value);
      }
    })

    // 屏幕边线吸附
    moveY = getMoveAdsorb(moveY, y, h, 0);
    moveY = getMoveAdsorb(moveY, y, h, projectStore.projectInfo.height);
    moveX = getMoveAdsorb(moveX, x, w, 0);
    moveX = getMoveAdsorb(moveX, x, w, projectStore.projectInfo.width);

    return {
      newMoveX: moveX,
      newMoveY: moveY,
    }
  }

  /**
   * 获取缩放吸附信息
   * @param x X坐标
   * @param y Y坐标
   * @param w 宽
   * @param h 高
   */
  const getScaleAdsorbInfo = ({x, y, width, height}: {x: number, y: number, width: number, height: number}) => {
    // TODO 完善缩放吸附功能
    // 1、获取当前操作元素
    // const selcetedElements = projectStore.currentSelectedElements;
    // 2、获取所有组件及参考线
    const referLines = projectStore.showReffer ? projectStore.referLines : [];
    // const elements = projectStore.elements.filter(item => selcetedElements.findIndex(ele => ele.key === item.key) === -1);

    referLines.forEach(line => {
      if (line.type === "vertical") {
        const res = getScaleAdsorb(y, height, line.value);
        y = res.xy;
        height = res.wh;
      } else {
        const res = getScaleAdsorb(x, width, line.value);
        x = res.xy;
        width = res.wh;
      }
    })

    return {
      newX: x,
      newY: y,
      newWidth: width,
      newHeight: height
    }
  }

  return {
    getMoveAdsorbInfo,
    getScaleAdsorbInfo
  }
}