
import Tool from '../core/tool';
import MarkEditLayer, { InnerMark } from './mark-edit-layer';


export default class MarkTool extends Tool {
  mark: InnerMark;
  moving: boolean = false;
  resizeing: boolean = false;
  creating: boolean = false;

  lastMouseDownX: number;
  lastMouseDownY: number;

  markEditLayer: MarkEditLayer;

  constructor(markEditLayer) {
    super('mark-edit-mark', 'crosshair');
    this.markEditLayer = markEditLayer;
  }


  override ondblclick(evt: MouseEvent): void {
    console.log('MarkTool@ondblclick()');
    let x = evt._contentPoint.x;
    let y = evt._contentPoint.y;
    let mark = this.markEditLayer.checkInMarks(x, y);
    if (mark) {
      this.markEditLayer.selectMark(mark);
    } else {
      this.markEditLayer.createMark(x, y, 40);
    }    
  }

  override onclick(evt: MouseEvent): void {
    console.log('MarkTool@onclick()');
    let x = evt._contentPoint.x;
    let y = evt._contentPoint.y;
    let mark = this.markEditLayer.checkInMarks(x, y);
    if (mark) {
      // 鼠标点击落在某个圈圈内，置为选中状态
      this.markEditLayer.selectMark(mark);
    } else {
      // 鼠标点击未落在任何圈圈内，判断是否落当前选中的外接正方形四个边角内，如果没有，全部置为非选中
      mark = this.markEditLayer.getSelectedMark();
      if (mark) {
        if (this.markEditLayer.checkQuadrant(mark, x, y) == 0) {
          this.markEditLayer.unselectMarks();
        }
      }
    }
  }

  override onmousedown(evt: MouseEvent) {
    console.log('MarkTool@onmousedown()');
    this.lastMouseDownX = evt._contentPoint.x;
    this.lastMouseDownY = evt._contentPoint.y;
  }

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

  override onmousemove(evt: MouseEvent): void {
    // console.log('MarkTool@onmousemove()');
    let x = evt._contentPoint.x;
    let y = evt._contentPoint.y;
    let mark = this.markEditLayer.checkInMarks(x, y);
    if (mark) {
      this.cursor = 'move';
    } else {
      this.cursor = 'crosshair';
    }
    mark = this.markEditLayer.getSelectedMark();
    if (mark) {
      let quadrant = this.markEditLayer.checkQuadrant(mark, x, y);
      if (quadrant == 1) this.cursor = 'ne-resize';
      if (quadrant == 2) this.cursor = 'nw-resize';
      if (quadrant == 3) this.cursor = 'sw-resize';
      if (quadrant == 4) this.cursor = 'se-resize';
    }

    this.editor.updateCursor();
  }

  
  override onDragStart(evt: MouseEvent) {
    console.log('MarkTool@onDragStart()');
    let x = evt._contentPoint.x;
    let y = evt._contentPoint.y;
    // dragstart的时候，坐标已经偏移，用最早mounsedown的坐标
    let mark = this.markEditLayer.checkInMarks(this.lastMouseDownX, this.lastMouseDownY);
    if (mark) { // 起始落点在某个圈圈内，这是要移动
      this.markEditLayer.selectMark(mark);
      this.mark = mark;
      this.moving = true;
      console.log("start moving...");
    } else {
      mark = this.markEditLayer.getSelectedMark();
      if (mark) {
        // dragstart的时候，坐标已经偏移，用最早mounsedown的坐标
        let quadrant = this.markEditLayer.checkQuadrant(mark, this.lastMouseDownX, this.lastMouseDownY);
        if (quadrant > 0) { // 起始落点是在选中圈圈的外接正方形的四个角落区域，那么标识要拖动调整大小
          this.mark = mark;
          this.resizeing = true;
          console.log("start resizeing...");
        }
      }
    }

    // 以上情况都不是，那么表示拖动生成一个新的圈圈
    if (!this.mark) {
      this.creating = true;
      this.mark = { x: this.lastMouseDownX, y: this.lastMouseDownY, radius: 0, note: '', status: 0};
      console.log("start creating...");
    }
    
  }

  override onDragMove(evt: MouseEvent) {
    console.log('ondragmove......');
    let x = evt._contentPoint.x;
    let y = evt._contentPoint.y;
    if (this.moving && this.mark) { // move
      this.markEditLayer.moveMark(this.mark, x, y);
    } else if (this.resizeing && this.mark) { // resize
      let dx = this.mark.x - x
      let dy = this.mark.y - y;
      let radius = Math.floor(Math.sqrt((dx * dx) + (dy * dy)));
      console.log("mark resize to " + radius);
      this.markEditLayer.resizeMark(this.mark, radius);
    } else if (this.creating && this.mark) {
      let dx = this.mark.x - x
      let dy = this.mark.y - y;
      let radius = Math.floor(Math.sqrt((dx * dx) + (dy * dy)));
      if (radius) {
        if (this.mark.radius == 0) {
          this.mark = this.markEditLayer.createMark(this.mark.x, this.mark.y, radius);
        } else {
          this.markEditLayer.resizeMark(this.mark, radius);
        }
      }
      console.log("creating");
    }


  }

  override onDragEnd(evt: MouseEvent) {
    this.moving = false;
    this.resizeing = false;
    this.creating = false;
    this.mark = null;
  }


}
