import { Pos } from "@/utils/math.ts";
import { themeMouseColors } from "@/constant/help-style.ts";
import { getMouseColors } from "@/utils/colors.ts";
import { onlyId } from "@/utils/shared.ts";
import { BaseItem, generateSnapInfos, getBaseItem } from "../util.ts";
import { AddMessage } from "@/core/hook/use-draw.ts";

export { default as Component } from "./rectangle.vue";
export { default as TempComponent } from "./temp-rectangle.vue";

export const shapeName = "矩形";
export const defaultStyle = {
  dash: [30, 0],
  strokeWidth: 1,
  stroke: themeMouseColors.theme,
};

export const getMouseStyle = (data: RectangleData) => {
  const fillStatus = data.fill && getMouseColors(data.fill);
  const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  const strokeWidth = data.strokeWidth || defaultStyle.strokeWidth;

  return {
    default: { fill: fillStatus && fillStatus.pub, stroke: strokeStatus.pub, strokeWidth },
    hover: { fill: fillStatus && fillStatus.hover, stroke: strokeStatus.hover },
    press: { fill: fillStatus && fillStatus.press, stroke: strokeStatus.press },
  };
};

export const addMode = "area";

export type RectangleData = Partial<typeof defaultStyle> &
  BaseItem & {
    id: string;
    attitude: number[];
    points: Pos[];
    createTime: number;
    zIndex: number;
    dash?: number[];
    stroke?: string;
    fill?: string;
    strokeWidth?: number;
  };

export const interactiveToData = (
  info: AddMessage<'rectangle'>,
  preset: Partial<RectangleData> = {}
): RectangleData | undefined => {
  if (info.cur) {
    const item = {
      ...getBaseItem(),
      ...preset,
      id: onlyId(),
      createTime: Date.now(),
      zIndex: 0,
    } as unknown as RectangleData;
    return interactiveFixData(item, info);
  }
};

export const getSnapPoints = (data: RectangleData) => {
  return data.points
}

export const getSnapInfos = (data: RectangleData) => generateSnapInfos(getSnapPoints(data), true, false)

export const interactiveFixData = (
  data: RectangleData,
  info: AddMessage<'rectangle'>
) => {
  if (info.cur) {
    const area = info.cur!;
    const width = area[1].x - area[0].x;
    const height = area[1].y - area[0].y;

    data.points = [
      info.cur[0],
      { x: info.cur[0].x + width, y: info.cur[0].y },
      { x: info.cur[0].x + width, y: info.cur[0].y + height },
      { x: info.cur[0].x, y: info.cur[0].y + height },
    ];
    data.attitude = [1, 0, 0, 1, 0, 0];
  }
  return data;
};
