/**
 * 标记工具函数（已废弃，保留用于Konva兼容）
 * 新的DOM渲染不再需要这些工具函数
 * @deprecated 使用 useMarkingDom 替代
 */
import type { BaseShape, TextAnnotation } from './useMarkingCommand'
import Konva from 'konva'

export function useMarkingTools() {
  const generateId = (): string => {
    return `mark_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
  }

  // 创建矩形
  const createRect = (
    x: number,
    y: number,
    width: number,
    height: number,
    options?: Partial<BaseShape>,
  ): Konva.Rect => {
    return new Konva.Rect({
      id: generateId(),
      x,
      y,
      width,
      height,
      fill: 'transparent',
      stroke: options?.stroke || '#ff0000',
      strokeWidth: options?.strokeWidth || 2,
      draggable: true,
      ...options,
    })
  }

  // 创建文本
  const createText = (
    x: number,
    y: number,
    text: string,
    options?: Partial<TextAnnotation>,
  ): Konva.Text => {
    return new Konva.Text({
      id: generateId(),
      x,
      y,
      text,
      fontSize: options?.fontSize || 14,
      fill: options?.color || '#000000',
      draggable: true,
      ...options,
    })
  }

  // 创建线条
  const createLine = (points: number[], options?: Partial<BaseShape>): Konva.Line => {
    return new Konva.Line({
      id: generateId(),
      points,
      stroke: options?.stroke || '#ff0000',
      strokeWidth: options?.strokeWidth || 2,
      lineCap: 'round',
      lineJoin: 'round',
      ...options,
    })
  }

  // 创建特殊标记（正确/错误/半对）
  const createSpecialMark = (
    type: 'correct' | 'wrong' | 'half',
    x: number,
    y: number,
  ): Konva.Group => {
    const group = new Konva.Group({
      id: generateId(),
      x,
      y,
      draggable: true,
    })

    if (type === 'correct') {
      // ✓ 符号 - 使用路径绘制更精确的勾
      const checkmark = new Konva.Line({
        points: [-8, -2, -3, 4, 8, -8],
        stroke: '#ff4d4f',
        strokeWidth: 4,
        lineCap: 'round',
        lineJoin: 'round',
      })
      group.add(checkmark)
    }
    else if (type === 'wrong') {
      // ✗ 符号 - 绘制X
      const line1 = new Konva.Line({
        points: [-8, -8, 8, 8],
        stroke: '#ff4d4f',
        strokeWidth: 4,
        lineCap: 'round',
      })
      const line2 = new Konva.Line({
        points: [8, -8, -8, 8],
        stroke: '#ff4d4f',
        strokeWidth: 4,
        lineCap: 'round',
      })
      group.add(line1)
      group.add(line2)
    }
    else {
      // 半对标记 - 勾+横线组合
      const checkmark = new Konva.Line({
        points: [-8, -2, -3, 4, 8, -8],
        stroke: '#ff4d4f',
        strokeWidth: 4,
        lineCap: 'round',
        lineJoin: 'round',
      })
      const line = new Konva.Line({
        points: [-3, -8, 10, 2],
        stroke: '#ff4d4f',
        strokeWidth: 4,
        lineCap: 'round',
      })
      group.add(checkmark)
      group.add(line)
    }

    return group
  }

  return {
    generateId,
    createRect,
    createText,
    createLine,
    createSpecialMark,
  }
}
