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

/**
 * 长毛发笔刷
 * 创建类似于长毛、毛皮、草或头发的效果
 */
export class LongfurBrush extends BaseBrush {
  /**
   * 构造函数
   * @param {Object} canvas fabric画布实例
   * @param {Object} options 配置选项
   */
  constructor(canvas, options = {}) {
    super(canvas, {
      id: "longfur",
      name: options.t('Canvas.Longfur'),
      description: "创建流动的长毛发效果，适合绘制动物毛皮、草或头发",
      category: options.t('Canvas.SpecialEffectsBrush'),
      icon: "longfur",
      ...options,
    });

    // 长毛发笔刷特有属性
    this.furLength = options.furLength || 20;
    this.furDensity = options.furDensity || 0.7;
    this.furFlowFactor = options.furFlowFactor || 0.5;
    this.furCurvature = options.furCurvature || 0.3;
    this.randomizeDirection =
      options.randomizeDirection !== undefined ? options.randomizeDirection : true;
  }

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

    // 创建fabric原生长毛发笔刷
    this.brush = new fabric.LongfurBrush(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.furLength !== undefined) {
      this.furLength = options.furLength;

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

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

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

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

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

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

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

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

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

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

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

    return this.furLength;
  }

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

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

    return this.furDensity;
  }

  /**
   * 设置毛发流动系数
   * @param {Number} factor 流动系数(0-1)
   */
  setFurFlowFactor(factor) {
    this.furFlowFactor = Math.max(0, Math.min(1, factor));

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

    return this.furFlowFactor;
  }

  /**
   * 设置毛发弯曲度
   * @param {Number} curvature 弯曲度(0-1)
   */
  setFurCurvature(curvature) {
    this.furCurvature = Math.max(0, Math.min(1, curvature));

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

    return this.furCurvature;
  }

  /**
   * 设置是否随机化方向
   * @param {Boolean} randomize 是否随机化
   */
  setRandomizeDirection(randomize) {
    this.randomizeDirection = randomize;

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

    return this.randomizeDirection;
  }

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

    // 定义长毛发笔刷特有属性
    const longfurProperties = [
      {
        id: "furLength",
        name: this.t('Canvas.FurLength'),
        type: "slider",
        defaultValue: this.furLength,
        min: 5,
        max: 100,
        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: "furFlowFactor",
        name: this.t('Canvas.FlowCoefficient'),
        type: "slider",
        defaultValue: this.furFlowFactor,
        min: 0,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.FlowCoefficientDescription'),
        category: this.t('Canvas.FurSettings'),
        order: 120,
      },
      {
        id: "furCurvature",
        name: this.t('Canvas.furCurvature'),
        type: "slider",
        defaultValue: this.furCurvature,
        min: 0,
        max: 1,
        step: 0.05,
        description: this.t('Canvas.furCurvatureDescription'),
        category: this.t('Canvas.FurSettings'),
        order: 130,
      },
      {
        id: "randomizeDirection",
        name: this.t('Canvas.randomizeDirection'),
        type: "checkbox",
        defaultValue: this.randomizeDirection,
        description: this.t('Canvas.randomizeDirectionDescription'),
        category: this.t('Canvas.FurSettings'),
        order: 140,
      },
    ];

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

  /**
   * 更新笔刷属性
   * @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 === "furFlowFactor") {
      this.setFurFlowFactor(value);
      return true;
    } else if (propId === "furCurvature") {
      this.setFurCurvature(value);
      return true;
    } else if (propId === "randomizeDirection") {
      this.setRandomizeDirection(value);
      return true;
    }

    return false;
  }

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