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

/**
 * 钢笔笔刷
 * 模拟钢笔效果，具有变化的透明度
 */
export class CustomPenBrush extends BaseBrush {
  /**
   * 构造函数
   * @param {Object} canvas fabric画布实例
   * @param {Object} options 配置选项
   */
  constructor(canvas, options = {}) {
    super(canvas, {
      id: "pen",
      name: options.t('Canvas.Pen'),
      description: "模拟钢笔效果，具有变化的透明度",
      category: options.t('Canvas.BasicBrushes'),
      icon: "pen",
      ...options,
    });

    // 钢笔笔刷特有属性
    this._baseWidth = options._baseWidth || 15;
    this._lineWidth = options._lineWidth || 2;
    this.inkOpacityMin = options.inkOpacityMin || 0.2;
    this.inkOpacityMax = options.inkOpacityMax || 0.6;
  }

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

    // 创建fabric原生钢笔笔刷
    this.brush = new fabric.PenBrush(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;
      // 更新笔刷相关属性
      this._baseWidth = options.width / 2;
    }

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

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

    // 钢笔笔刷特有属性
    if (options._baseWidth !== undefined) {
      brush._baseWidth = options._baseWidth;
      this._baseWidth = options._baseWidth;
    }

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

    // 确保线条连接设置正确
    brush.canvas.contextTop.lineJoin = "round";
    brush.canvas.contextTop.lineCap = "round";
  }

  /**
   * 设置最小墨水透明度
   * @param {Number} opacity 透明度值(0-1)
   */
  setInkOpacityMin(opacity) {
    this.inkOpacityMin = Math.max(0.1, Math.min(0.5, opacity));
    return this.inkOpacityMin;
  }

  /**
   * 设置最大墨水透明度
   * @param {Number} opacity 透明度值(0-1)
   */
  setInkOpacityMax(opacity) {
    this.inkOpacityMax = Math.max(0.3, Math.min(1, opacity));
    return this.inkOpacityMax;
  }

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

    // 定义钢笔笔刷特有属性
    const penProperties = [
      {
        id: "lineWidth",
        name: this.t('Canvas.PenWidth'),	
        type: "slider",
        defaultValue: this._lineWidth,
        min: 1,
        max: 10,
        step: 0.5,
        description: this.t('Canvas.PenWidthDescription'),
        category: this.t('Canvas.PenSettings'),
        order: 100,
      },
      {
        id: "inkOpacityMin",
        name: this.t('Canvas.PenMinimumInkTransparency'),
        type: "slider",
        defaultValue: this.inkOpacityMin,
        min: 0.1,
        max: 0.5,
        step: 0.05,
        description: this.t('Canvas.PenMinimumInkTransparencyDescription'),
        category: this.t('Canvas.PenSettings'),
        order: 110,
      },
      {
        id: "inkOpacityMax",
        name: this.t('Canvas.PenMaximumInkTransparency'),
        type: "slider",
        defaultValue: this.inkOpacityMax,
        min: 0.3,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.PenMaximumInkTransparencyDescription'),
        category: this.t('Canvas.PenSettings'),
        order: 120,
      },
    ];

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

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

    // 处理钢笔笔刷特有属性
    if (propId === "lineWidth") {
      this._lineWidth = value;
      if (this.brush) {
        this.brush._lineWidth = value;
      }
      return true;
    } else if (propId === "inkOpacityMin") {
      this.setInkOpacityMin(value);
      return true;
    } else if (propId === "inkOpacityMax") {
      this.setInkOpacityMax(value);
      return true;
    }

    return false;
  }

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