import NumberEditLayer from './number-edit-layer';
import NumberEditTool from './number-edit-tool';
import { FillTask, FILL_TYPE } from '../common/filltask';

export default class EraseTool extends NumberEditTool {
  fasterase: boolean;

  constructor(numberEditLayer) {
    super(numberEditLayer, 'number-edit-erase', 'url("/static/cursor/cursor-erase.png") 0 22, default');
    this.setProp('fasterase', true);
  }

  isfasteraseEnabled() { return this.getProp('fasterase'); }

  override onmousedown(evt: MouseEvent) {
    console.log('BucketTool@onmousedown()');
    if (!this.numberEditLayer)
      return;
    this.fasterase = false;
  }

  override onmouseup(evt: MouseEvent) { console.log('BucketTool@onmouseup()'); }

  override onDragStart(evt: MouseEvent) {
    if (!this.numberEditLayer)
      return;
    console.log('BucketTool@onDragStart()');
    if (!this.isfasteraseEnabled())
      return;

    this.fasterase = true;

    let task = new FillTask(FILL_TYPE.SOLID);
    let pp = this.numberEditLayer.translate(evt._contentPoint);
    task.x = pp.x;
    task.y = pp.y;
    this.numberEditLayer.fasteraseStart(task);
  }

  override onDragMove(evt: MouseEvent) {
    console.log('ondragmove......');
    if (!this.numberEditLayer)
      return;
    console.log('BucketTool@onDragMove()');
    if (!this.isfasteraseEnabled())
      return;
    let pp = this.numberEditLayer.translate(evt._contentPoint);
    this.numberEditLayer.fasteraseAddPoint(pp.x, pp.y);
  }

  override onDragEnd(evt: MouseEvent) {
    if (!this.numberEditLayer)
      return;
    if (!this.isfasteraseEnabled())
      return;
    console.log('BucketTool@onDragEnd()');
    this.numberEditLayer.fasteraseEnd();
  }

  override onclick(evt : MouseEvent) {
    if (!this.editor) return;
    if (!this.numberEditLayer) return;

    let task = new FillTask(FILL_TYPE.ERASE);
    let pp = this.numberEditLayer.translate(evt._contentPoint);
    task.x = pp.x;
    task.y = pp.y;
    this.numberEditLayer.erase(task);
  }

  override onSelect(): void {
    if (!this.numberEditLayer) return;
    this.numberEditLayer.showUnorderMask = false;
    this.numberEditLayer.invalidate();
  }

}
