import { BaseBrush } from "../BaseBrush";

/**
 * 喷漆笔刷
 * 创建喷漆效果，点状分散的绘制风格
 */
export class SpraypaintBrush extends BaseBrush {
  /**
   * 构造函数
   * @param {Object} canvas fabric画布实例
   * @param {Object} options 配置选项
   */
  constructor(canvas, options = {}) {
    super(canvas, {
      id: "spraypaint",
      name: "喷漆笔刷",
      description: "创建喷漆效果，点状分散的绘制风格",
      category: "绘画笔刷",
      icon: "spraypaint",
      ...options,
    });

    // 喷漆笔刷特有属性
    this.density = options.density || 20;
    this.sprayRadius = options.sprayRadius || 10;
    this.randomOpacity = options.randomOpacity !== undefined ? options.randomOpacity : true;
    this.dotSize = options.dotSize || 1;
    this.dotShape = options.dotShape || "circle";
  }

  /**
   * 创建笔刷实例
   * @returns {Object} fabric笔刷实例
   */
  create() {
    if (!this.canvas) {
      throw new Error("画布实例不存在");
    }

    // 创建fabric原生喷漆笔刷
    this.brush = new fabric.SprayBrush(this.canvas);

    // 配置笔刷
    this.configure(this.brush, this.options);

    return this.brush;
  }

  /**
   * 配置笔刷
   * @param {Object} brush fabric笔刷实例
   * @param {Object} options 配置选项
   */
  configure(brush, options = {}) {
    if (!brush) return;

    // 设置基本属性
    if (options.width !== undefined) {
      brush.width = options.width;
    }

    if (options.color !== undefined) {
      brush.color = options.color;
    }

    if (options.opacity !== undefined) {
      brush.opacity = options.opacity;
    }

    // 喷漆笔刷特有属性
    if (options.density !== undefined) {
      this.density = options.density;

      // 如果原生笔刷支持此属性，则设置
      if (brush.density !== undefined) {
        brush.density = this.density;
      }
    }

    if (options.sprayRadius !== undefined) {
      this.sprayRadius = options.sprayRadius;

      // 如果原生笔刷支持此属性，则设置
      if (brush.sprayWidth !== undefined) {
        brush.sprayWidth = this.sprayRadius;
      } else if (brush.width !== undefined) {
        brush.width = this.sprayRadius;
      }
    }

    if (options.randomOpacity !== undefined) {
      this.randomOpacity = options.randomOpacity;

      // 如果原生笔刷支持此属性，则设置
      if (brush.randomOpacity !== undefined) {
        brush.randomOpacity = this.randomOpacity;
      }
    }

    if (options.dotSize !== undefined) {
      this.dotSize = options.dotSize;

      // 如果原生笔刷支持此属性，则设置
      if (brush.dotWidth !== undefined) {
        brush.dotWidth = this.dotSize;
      }
    }

    if (options.dotShape !== undefined) {
      this.dotShape = options.dotShape;

      // 如果原生笔刷支持此属性，则设置
      if (brush.dotShape !== undefined) {
        brush.dotShape = this.dotShape;
      }
    }
  }

  /**
   * 设置喷漆密度
   * @param {Number} value 密度值
   */
  setDensity(value) {
    this.density = Math.max(1, Math.min(100, value));

    if (this.brush && this.brush.density !== undefined) {
      this.brush.density = this.density;
    }

    return this.density;
  }

  /**
   * 设置喷漆半径
   * @param {Number} value 半径值
   */
  setSprayRadius(value) {
    this.sprayRadius = Math.max(1, value);

    if (this.brush) {
      if (this.brush.sprayWidth !== undefined) {
        this.brush.sprayWidth = this.sprayRadius;
      } else if (this.brush.width !== undefined) {
        this.brush.width = this.sprayRadius;
      }
    }

    return this.sprayRadius;
  }

  /**
   * 设置是否随机透明度
   * @param {Boolean} value 是否随机透明度
   */
  setRandomOpacity(value) {
    this.randomOpacity = value;

    if (this.brush && this.brush.randomOpacity !== undefined) {
      this.brush.randomOpacity = this.randomOpacity;
    }

    return this.randomOpacity;
  }

  /**
   * 设置点大小
   * @param {Number} value 点大小
   */
  setDotSize(value) {
    this.dotSize = Math.max(0.1, value);

    if (this.brush && this.brush.dotWidth !== undefined) {
      this.brush.dotWidth = this.dotSize;
    }

    return this.dotSize;
  }

  /**
   * 设置点形状
   * @param {String} value 点形状，如 'circle', 'square', 'diamond'
   */
  setDotShape(value) {
    const validShapes = ["circle", "square", "diamond", "random"];

    if (validShapes.includes(value)) {
      this.dotShape = value;

      if (this.brush && this.brush.dotShape !== undefined) {
        this.brush.dotShape = this.dotShape;
      }
    }

    return this.dotShape;
  }

  /**
   * 获取笔刷可配置属性
   * @returns {Array} 可配置属性描述数组
   * @override
   */
  getConfigurableProperties() {
    // 获取基础属性
    const baseProperties = super.getConfigurableProperties();

    // 定义喷漆笔刷特有属性
    const spraypaintProperties = [
      {
        id: "density",
        name: "喷漆密度",
        type: "slider",
        defaultValue: this.density,
        min: 1,
        max: 100,
        step: 1,
        description: "控制喷漆点的密度",
        category: "喷漆设置",
        order: 100,
      },
      {
        id: "sprayRadius",
        name: "喷漆半径",
        type: "slider",
        defaultValue: this.sprayRadius,
        min: 1,
        max: 50,
        step: 1,
        description: "控制喷漆的覆盖半径",
        category: "喷漆设置",
        order: 110,
      },
      {
        id: "randomOpacity",
        name: "随机透明度",
        type: "checkbox",
        defaultValue: this.randomOpacity,
        description: "使喷漆点有随机透明度",
        category: "喷漆设置",
        order: 120,
      },
      {
        id: "dotSize",
        name: "点大小",
        type: "slider",
        defaultValue: this.dotSize,
        min: 0.1,
        max: 10,
        step: 0.1,
        description: "控制喷漆点的大小",
        category: "喷漆设置",
        order: 130,
      },
      {
        id: "dotShape",
        name: "点形状",
        type: "select",
        defaultValue: this.dotShape,
        options: [
          { value: "circle", label: "圆形" },
          { value: "square", label: "方形" },
          { value: "diamond", label: "菱形" },
          { value: "random", label: "随机" },
        ],
        description: "设置喷漆点的形状",
        category: "喷漆设置",
        order: 140,
      },
    ];

    // 合并并返回所有属性
    return [...baseProperties, ...spraypaintProperties];
  }

  /**
   * 更新笔刷属性
   * @param {String} propId 属性ID
   * @param {any} value 属性值
   * @returns {Boolean} 是否更新成功
   * @override
   */
  updateProperty(propId, value) {
    // 先检查基类能否处理此属性
    if (super.updateProperty(propId, value)) {
      return true;
    }

    // 处理喷漆笔刷特有属性
    if (propId === "density") {
      this.setDensity(value);
      return true;
    } else if (propId === "sprayRadius") {
      this.setSprayRadius(value);
      return true;
    } else if (propId === "randomOpacity") {
      this.setRandomOpacity(value);
      return true;
    } else if (propId === "dotSize") {
      this.setDotSize(value);
      return true;
    } else if (propId === "dotShape") {
      this.setDotShape(value);
      return true;
    }

    return false;
  }

  /**
   * 获取预览图
   * @returns {String} 预览图URL
   */
  getPreview() {
    return "data:image/svg+xml;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