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

/**
 * 书法笔刷
 * 模拟中国传统书法效果，具有笔锋和墨色变化
 */
export class WritingBrush extends BaseBrush {
  /**
   * 构造函数
   * @param {Object} canvas fabric画布实例
   * @param {Object} options 配置选项
   */
  constructor(canvas, options = {}) {
    super(canvas, {
      id: "writing",
      name: options.t('Canvas.Writing'),
      description: "模拟中国传统书法毛笔效果，具有笔锋和墨色变化",
      category: options.t('Canvas.SpecialEffectsBrush'),
      icon: "writing",
      ...options,
    });

    // 书法笔刷特有属性
    this.brushPressure = options.brushPressure || 0.7;
    this.inkAmount = options.inkAmount || 20;
    this.brushTaperFactor = options.brushTaperFactor || 0.6;
    this.enableInkDripping =
      options.enableInkDripping !== undefined ? options.enableInkDripping : true;
  }

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

    // 创建fabric原生书法笔刷
    this.brush = new fabric.WritingBrush(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.brushPressure !== undefined) {
      this.brushPressure = options.brushPressure;

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

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

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

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

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

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

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

  /**
   * 设置笔压感应
   * @param {Number} pressure 笔压值(0-1)
   */
  setBrushPressure(pressure) {
    this.brushPressure = Math.max(0.1, Math.min(1, pressure));

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

    return this.brushPressure;
  }

  /**
   * 设置墨量
   * @param {Number} amount 墨量值
   */
  setInkAmount(amount) {
    this.inkAmount = Math.max(1, Math.min(50, amount));

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

    return this.inkAmount;
  }

  /**
   * 设置笔锋系数
   * @param {Number} factor 笔锋系数(0-1)
   */
  setBrushTaperFactor(factor) {
    this.brushTaperFactor = Math.max(0, Math.min(1, factor));

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

    return this.brushTaperFactor;
  }

  /**
   * 启用/禁用墨滴效果
   * @param {Boolean} enabled 是否启用
   */
  setInkDripping(enabled) {
    this.enableInkDripping = enabled;

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

    return this.enableInkDripping;
  }

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

    // 定义书法笔刷特有属性
    const writingProperties = [
      {
        id: "brushPressure",
        name: this.t('Canvas.WritingBrushPressure'),
        type: "slider",
        defaultValue: this.brushPressure,
        min: 0.1,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.WritingBrushPressureDescription'),
        category: this.t('Canvas.WritingSettings'),
        order: 100,
      },
      {
        id: "inkAmount",
        name: this.t('Canvas.WritingAmount'),
        type: "slider",
        defaultValue: this.inkAmount,
        min: 1,
        max: 50,
        step: 1,
        description: this.t('Canvas.WritingAmountDescription'),
        category: this.t('Canvas.WritingSettings'),
        order: 110,
      },
      {
        id: "brushTaperFactor",
        name: this.t('Canvas.WritingPenTipCoefficient'),
        type: "slider",
        defaultValue: this.brushTaperFactor,
        min: 0,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.WritingPenTipCoefficientDescription'),
        category: this.t('Canvas.WritingSettings'),
        order: 120,
      },
      {
        id: "enableInkDripping",
        name: this.t('Canvas.WritingDropEffect'),
        type: "checkbox",
        defaultValue: this.enableInkDripping,
        description: this.t('Canvas.WritingDropEffectDescription'),
        category: this.t('Canvas.WritingSettings'),
        order: 130,
      },
    ];

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

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

    // 处理书法笔刷特有属性
    if (propId === "brushPressure") {
      this.setBrushPressure(value);
      return true;
    } else if (propId === "inkAmount") {
      this.setInkAmount(value);
      return true;
    } else if (propId === "brushTaperFactor") {
      this.setBrushTaperFactor(value);
      return true;
    } else if (propId === "enableInkDripping") {
      this.setInkDripping(value);
      return true;
    }

    return false;
  }

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