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

/**
 * 毛发笔刷
 * 创建类似于毛发或草的效果
 */
export class FurBrush extends BaseBrush {
  /**
   * 构造函数
   * @param {Object} canvas fabric画布实例
   * @param {Object} options 配置选项
   */
  constructor(canvas, options = {}) {
    super(canvas, {
      id: "fur",
      name: options.t('Canvas.Longfur'),
      description: "创建类似于毛发或草的纹理效果",
      category: options.t('Canvas.SpecialEffectsBrush'),
      icon: "fur",
      ...options,
    });

    // 毛发笔刷特有属性
    this.furLength = options.furLength || 10;
    this.furDensity = options.furDensity || 0.7;
    this.furRandomness = options.furRandomness || 0.5;
  }

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

    // 创建fabric原生毛发笔刷
    this.brush = new fabric.FurBrush(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;
    }

    // 这里可以添加对毛发笔刷特有属性的配置
    // 由于fabric.FurBrush的原始实现可能没有直接暴露这些属性，
    // 我们可能需要在onMouseMove等事件中动态调整行为

    // 存储特有属性，供后续使用
    if (options.furLength !== undefined) {
      this.furLength = options.furLength;
    }

    if (options.furDensity !== undefined) {
      this.furDensity = options.furDensity;
    }

    if (options.furRandomness !== undefined) {
      this.furRandomness = options.furRandomness;
    }
  }

  /**
   * 设置毛发长度
   * @param {Number} length 长度值
   */
  setFurLength(length) {
    this.furLength = Math.max(1, Math.min(50, length));
    return this.furLength;
  }

  /**
   * 设置毛发密度
   * @param {Number} density 密度值(0-1)
   */
  setFurDensity(density) {
    this.furDensity = Math.max(0.1, Math.min(1, density));
    return this.furDensity;
  }

  /**
   * 设置毛发随机性
   * @param {Number} randomness 随机性值(0-1)
   */
  setFurRandomness(randomness) {
    this.furRandomness = Math.max(0, Math.min(1, randomness));
    return this.furRandomness;
  }

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

    // 定义毛发笔刷特有属性
    const furProperties = [
      {
        id: "furLength",
        name: this.t('Canvas.FurLength'),
        type: "slider",
        defaultValue: this.furLength,
        min: 1,
        max: 50,
        step: 1,
        description: this.t('Canvas.FurLengthDescription'),		
        category: this.t('Canvas.FurSettings'),
        order: 100,
      },
      {
        id: "furDensity",
        name: this.t('Canvas.FurDensity'),	
        type: "slider",
        defaultValue: this.furDensity,
        min: 0.1,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.FurDensityDescription'),	
        category: this.t('Canvas.FurSettings'),
        order: 110,
      },
      {
        id: "furRandomness",
        name: this.t('Canvas.FurRandomness'),	
        type: "slider",
        defaultValue: this.furRandomness,
        min: 0,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.FurRandomnessDescription'),	
        category: this.t('Canvas.FurSettings'),
        order: 120,
      },
    ];

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

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

    // 处理毛发笔刷特有属性
    if (propId === "furLength") {
      this.setFurLength(value);
      return true;
    } else if (propId === "furDensity") {
      this.setFurDensity(value);
      return true;
    } else if (propId === "furRandomness") {
      this.setFurRandomness(value);
      return true;
    }

    return false;
  }

  /**
   * 获取预览图
   * @returns {String} 预览图URL
   */
  getPreview() {
    return "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIj48cGF0aCBkPSJNMTAgODBMNTAgMjBNMjAgODBMNjAgMjBNMzAgODBMNzAgMjBNNDAgODBMODAgMjBNNTAgODBMOTAgMjAiIHN0cm9rZT0iIzAwMCIgc3Ryb2tlLXdpZHRoPSIxIiBmaWxsPSJub25lIi8+PC9zdmc+";
  }
}
