import EventEmitter from './eventemitter';
import { UndoRedo } from './interface';
import Tool from './tool';


export default class Layer extends EventEmitter implements UndoRedo {
  width: number;
  height: number;

  private _lastToolKey: string;

  constructor(width, height) {
    super();
    this.width = width;
    this.height = height;
  }

  invalidate() {
    this.trigger('invalidate');
  }

  //记录最后一切换的工具
  get lastToolKey(): string { return this._lastToolKey ? this._lastToolKey : this.defaultToolKey; }
  set lastToolKey(key) { this._lastToolKey = key; }
  get defaultToolKey(): string { return null; } //Children should overide this getter

  private tools: Tool[] = []; //layer支持的所有工具列表

  /**
   * Return list of Tool.
   * @returns 
   */

  getToolByKey(toolKey: string): Tool {
    return this.toolHash[toolKey];
  }

  addTool(tool : Tool) {this.tools.push(tool)}

  getTools(): Tool[] { return this.tools.slice(); }

  get toolHash(): any {
    return this.tools.reduce((last, tool) => {
      last[tool.key] = tool;
      return last;
    }, {});
  }


  getWidth() {
    return this.width;
  }

  getHeight() {
    return this.height;
  }

  //Overide this function.
  draw(ctx: CanvasRenderingContext2D) {
    ctx.fillStyle = 'blue';
    ctx.fillRect(0, 0, this.width, this.height);
  }

  undo(): void { }
  redo(): void { }
  undoSize(): number { return 0; }
  redoSize(): number { return 0; }


  private _smoothing = false;
  set smoothing(smooth) { this._smoothing = smooth; this.invalidate() }
  get smoothing() { return this._smoothing; }

  private _showSvg = false;
  public get showSvg() { return this._showSvg; }
  public set showSvg(value) { this._showSvg = value; this.invalidate() }


}
