import { Cell, PieceId } from "interface";

export class PlacePieceAction {
  pieceId: PieceId;
  rotation: number;
  cell: {
    row: number;
    col: number;
  };

  constructor(pieceId: PieceId, rotation: number, row: number, col: number) {
    this.pieceId = pieceId;
    this.rotation = rotation;
    this.cell = {
      row: row,
      col: col,
    };
  }

  do(board: Cell[][]) {
    const cell = board[this.cell.row][this.cell.col];
    cell.placePiece(this.pieceId, this.rotation as 0 | 90 | 180 | 270);
  }

  undo(board: Cell[][]) {
    const cell = board[this.cell.row][this.cell.col];
    if (!cell.placedPiece || cell.placedPiece.id !== this.pieceId) {
      throw Error("Un-doing action error");
    }
    cell.removePiece();
  }
}
