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

/**
 * 水墨笔刷
 * 模拟中国传统水墨画效果
 */
export class InkBrush extends BaseBrush {
  /**
   * 构造函数
   * @param {Object} canvas fabric画布实例
   * @param {Object} options 配置选项
   */
  constructor(canvas, options = {}) {
    super(canvas, {
      id: "ink",
      name: options.t('Canvas.Ink'),	
      description: "模拟中国传统水墨画效果，墨色深浅不一",
      category: options.t('Canvas.SpecialEffectsBrush'),
      icon: "ink",
      ...options,
    });

    // 水墨笔刷特有属性
    this._baseWidth = options._baseWidth || 15;
    this._inkAmount = options._inkAmount || 7;
    this._range = options._range || 10;
    this.splashEnabled = options.splashEnabled !== undefined ? options.splashEnabled : true;
    this.splashSize = options.splashSize || 5;
    this.splashDistance = options.splashDistance || 30;
	this.t = options.t
  }

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

    // 创建fabric原生水墨笔刷
    this.brush = new fabric.InkBrush(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._inkAmount !== undefined) {
      brush._inkAmount = options._inkAmount;
      this._inkAmount = options._inkAmount;
    }

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

    // 更新溅墨相关配置（这需要修改原始InkBrush的drawSplash方法）
    if (this.splashEnabled !== undefined) {
      // 由于原始InkBrush没有直接暴露这个配置，我们可能需要覆盖方法
      // 这里仅保存配置，实际逻辑需要在brush创建后处理
    }
  }

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

    if (this.brush) {
      this.brush._inkAmount = this._inkAmount;
    }

    return this._inkAmount;
  }

  /**
   * 设置笔触范围
   * @param {Number} range 范围值
   */
  setRange(range) {
    this._range = Math.max(5, Math.min(50, range));

    if (this.brush) {
      this.brush._range = this._range;
    }

    return this._range;
  }

  /**
   * 启用/禁用溅墨效果
   * @param {Boolean} enabled 是否启用
   */
  setSplashEnabled(enabled) {
    this.splashEnabled = enabled;

    // 实际应用需要更多的逻辑来支持这个功能
    // 由于需要修改fabric.InkBrush的内部行为

    return this.splashEnabled;
  }

  /**
   * 设置溅墨大小
   * @param {Number} size 大小值
   */
  setSplashSize(size) {
    this.splashSize = Math.max(1, Math.min(20, size));
    return this.splashSize;
  }

  /**
   * 设置溅墨距离
   * @param {Number} distance 距离值
   */
  setSplashDistance(distance) {
    this.splashDistance = Math.max(10, Math.min(100, distance));
    return this.splashDistance;
  }

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

    // 定义水墨笔刷特有属性
    const inkProperties = [
      {
        id: "inkAmount",
        name: this.t('Canvas.InkAmount'),	
        type: "slider",
        defaultValue: this._inkAmount,
        min: 1,
        max: 20,
        step: 1,
        description: this.t('Canvas.InkAmountDescription'),
        category: this.t('Canvas.InkSettings'),
        order: 100,
      },
      {
        id: "range",
        name: this.t('Canvas.InkRange'),		
        type: "slider",
        defaultValue: this._range,
        min: 5,
        max: 50,
        step: 1,
        description: this.t('Canvas.InkRangeDescription'),
        category: this.t('Canvas.InkSettings'),		
        order: 110,
      },
      {
        id: "splashEnabled",
        name: this.t('Canvas.InkSplashEnabled'),
        type: "checkbox",
        defaultValue: this.splashEnabled,
        description: this.t('Canvas.InkSplashEnabledDescription'),
        category: this.t('Canvas.InkSettings'),		
        order: 120,
      },
      {
        id: "splashSize",
        name: this.t('Canvas.InkSize'),	
        type: "slider",
        defaultValue: this.splashSize,
        min: 1,
        max: 20,
        step: 1,
        description: this.t('Canvas.InkSizeDescription'),
        category: this.t('Canvas.InkSettings'),								
        order: 130,
        visibleWhen: { splashEnabled: true },
      },
      {
        id: "splashDistance",
        name: this.t('Canvas.InkRandomness'),	
        type: "slider",
        defaultValue: this.splashDistance,
        min: 10,
        max: 100,
        step: 5,
        description: this.t('Canvas.InkRandomnessDescription'),
        category: this.t('Canvas.InkSettings'),								
        order: 140,
        visibleWhen: { splashEnabled: true },
      },
    ];

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

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

    // 处理水墨笔刷特有属性
    if (propId === "inkAmount") {
      this.setInkAmount(value);
      return true;
    } else if (propId === "range") {
      this.setRange(value);
      return true;
    } else if (propId === "splashEnabled") {
      this.setSplashEnabled(value);
      return true;
    } else if (propId === "splashSize") {
      this.setSplashSize(value);
      return true;
    } else if (propId === "splashDistance") {
      this.setSplashDistance(value);
      return true;
    }

    return false;
  }

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