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

/**
 * 蜡笔笔刷
 * 模拟蜡笔效果，具有颗粒感和纹理
 */
export class CrayonBrush extends BaseBrush {
  /**
   * 构造函数
   * @param {Object} canvas fabric画布实例
   * @param {Object} options 配置选项
   */
  constructor(canvas, options = {}) {
    super(canvas, {
      id: "crayon",
      name: options.t("Canvas.Crayon"),
      description: "模拟蜡笔效果，具有颗粒感和纹理",
      category: options.t("Canvas.SpecialEffectsBrush"),
      icon: "crayon",
      ...options,
    });

    // 蜡笔笔刷特有属性
    this._baseWidth = options._baseWidth || 15;
    this._size = options._size || 0;
    this._sep = options._sep || options._sep === 0 ? options._sep : 3;
    this._inkAmount = options._inkAmount || 10;
    this.randomness = options.randomness || 0.5; // 随机性
    this.texture = options.texture || "default"; // 纹理类型
	this.t = options.t
  }

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

    // 创建fabric原生蜡笔笔刷
    this.brush = new fabric.CrayonBrush(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;
      this._size = options.width / 2 + this._baseWidth;
    }

    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;
      this._size = this.width / 2 + this._baseWidth;
    }

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

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

  /**
   * 设置颗粒分离度
   * @param {Number} sep 分离度值
   */
  setSeparation(sep) {
    this._sep = Math.max(0.5, Math.min(10, sep));

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

    return this._sep;
  }

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

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

    return this._inkAmount;
  }

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

  /**
   * 设置纹理类型
   * @param {String} type 纹理类型
   */
  setTexture(type) {
    this.texture = type;
    // 实际应用可能需要更多的实现逻辑
    return this.texture;
  }

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

    // 定义蜡笔笔刷特有属性
    const crayonProperties = [
      {
        id: "separation",
        name: this.t('Canvas.ParticleSeparationDegree'),
        type: "slider",
        defaultValue: this._sep,
        min: 0.5,
        max: 10,
        step: 0.5,
        description: this.t('Canvas.ParticleSeparationDegreeDescription'),
        category: this.t('Canvas.PencilSettings'),
        order: 100,
      },
      {
        id: "inkAmount",
        name: this.t('Canvas.TheAmountOfInk'),
        type: "slider",
        defaultValue: this._inkAmount,
        min: 1,
        max: 50,
        step: 1,
        description: this.t('Canvas.TheAmountOfInkDescription'),
        category: this.t('Canvas.PencilSettings'),
        order: 110,
      },
      {
        id: "randomness",
        name: this.t('Canvas.randomness'),
        type: "slider",
        defaultValue: this.randomness,
        min: 0,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.randomnessDescription'),
        category: this.t('Canvas.PencilSettings'),
        order: 120,
      },
      {
        id: "texture",
        name: this.t('Canvas.TextureType'),
        type: "select",
        defaultValue: this.texture,
        options: [
          { value: "default", label: this.t('Canvas.Default') },
          { value: "rough", label: this.t('Canvas.Rough') },
          { value: "smooth", label: this.t('Canvas.Smooth') },
        ],
        description: this.t('Canvas.TextureTypeDescription'),
        category: this.t('Canvas.PencilSettings'),
        order: 130,
      },
    ];

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

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

    // 处理蜡笔笔刷特有属性
    if (propId === "separation") {
      this.setSeparation(value);
      return true;
    } else if (propId === "inkAmount") {
      this.setInkAmount(value);
      return true;
    } else if (propId === "randomness") {
      this.setRandomness(value);
      return true;
    } else if (propId === "texture") {
      this.setTexture(value);
      return true;
    }

    return false;
  }

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