import type { EditorObject, ToolType } from "../../store/editorStore";
import { useShapeStore } from "../../store/shapeStore";

type DrawingParams = {
  tool: ToolType;
  startPos: { x: number; y: number };
  pointerPos: { x: number; y: number };
  tempObject: EditorObject | null;
};

export const createDrawingObject = ({
  tool,
  startPos,
  pointerPos,
  tempObject,
}: DrawingParams): EditorObject | null => {
  const width = pointerPos.x - startPos.x;
  const height = pointerPos.y - startPos.y;

  if (tool === "rectangle") {
    const { activeShape, useAsMask } = useShapeStore.getState();

    // برای دایره، مرکز و شعاع را محاسبه می‌کنیم
    if (activeShape === "circle") {
      const centerX = (startPos.x + pointerPos.x) / 2;
      const centerY = (startPos.y + pointerPos.y) / 2;
      const radius = Math.max(Math.abs(width), Math.abs(height)) / 2;

      return {
        id: tempObject?.id || `rect-${Date.now()}`,
        type: "rectangle",
        x: centerX,
        y: centerY,
        width: radius * 2,
        height: radius * 2,
        fill: "#3b82f6",
        stroke: "#1e40af",
        strokeWidth: 0,
        shapeType: activeShape,
        isMask: useAsMask,
      };
    }

    // برای مثلث، مرکز را محاسبه می‌کنیم و سپس گوشه بالا-چپ را
    if (activeShape === "triangle") {
      const centerX = (startPos.x + pointerPos.x) / 2;
      const centerY = (startPos.y + pointerPos.y) / 2;
      const size = Math.max(Math.abs(width), Math.abs(height));

      // گوشه بالا-چپ مستطیل محدودکننده
      const x = centerX - size / 2;
      const y = centerY - size / 2;

      return {
        id: tempObject?.id || `rect-${Date.now()}`,
        type: "rectangle",
        x: x,
        y: y,
        width: size,
        height: size,
        fill: "#3b82f6",
        stroke: "#1e40af",
        strokeWidth: 0,
        shapeType: activeShape,
        isMask: useAsMask,
      };
    }

    // برای شکل انتزاعی، مرکز را محاسبه می‌کنیم و سپس گوشه بالا-چپ را
    if (activeShape === "abstract") {
      const centerX = (startPos.x + pointerPos.x) / 2;
      const centerY = (startPos.y + pointerPos.y) / 2;
      const size = Math.max(Math.abs(width), Math.abs(height));

      // گوشه بالا-چپ مستطیل محدودکننده
      const x = centerX - size / 2;
      const y = centerY - size / 2;

      return {
        id: tempObject?.id || `rect-${Date.now()}`,
        type: "rectangle",
        x: x,
        y: y,
        width: size,
        height: size,
        fill: "#3b82f6",
        stroke: "#1e40af",
        strokeWidth: 0,
        shapeType: activeShape,
        isMask: useAsMask,
      };
    }

    // برای سایر اشکال (مستطیل)
    return {
      id: tempObject?.id || `rect-${Date.now()}`,
      type: "rectangle",
      x: Math.min(startPos.x, pointerPos.x),
      y: Math.min(startPos.y, pointerPos.y),
      width: Math.abs(width),
      height: Math.abs(height),
      fill: "#3b82f6",
      stroke: "#1e40af",
      strokeWidth: 0,
      shapeType: activeShape,
      isMask: useAsMask,
    };
  }

  if (tool === "line") {
    return {
      id: tempObject?.id || `line-${Date.now()}`,
      type: "line",
      x: startPos.x,
      y: startPos.y,
      width: pointerPos.x,
      height: pointerPos.y,
      stroke: "#000000",
      strokeWidth: 2,
    };
  }

  if (tool === "arrow") {
    return {
      id: tempObject?.id || `arrow-${Date.now()}`,
      type: "arrow",
      x: startPos.x,
      y: startPos.y,
      width: pointerPos.x,
      height: pointerPos.y,
      stroke: "#000000",
      strokeWidth: 2,
    };
  }

  return null;
};
