package boundary;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import controller.BuilderBoardMouseCtrl;
import model.Board;
import model.BuilderModel;
import model.Cell;

/**
 * Actual view for the builder board view.
 *
 * @author Eli Skeggs
 */
public class BuilderBoardView extends BoardView {
  BuilderApplication app;
  BuilderModel model;

  public BuilderBoardView(BuilderApplication app, BuilderModel model) {
    super(true, Board.width, Board.height);

    this.app = app;
    this.model = model;

    BuilderBoardMouseCtrl ctrl = new BuilderBoardMouseCtrl(app, model);
    addMouseListener(ctrl);
    addMouseMotionListener(ctrl);

    setCacheSource(app.loader);

    Color[] fallbackColors = new Color[] {Color.GREEN, Color.MAGENTA,
      Color.YELLOW, Color.RED, Color.BLUE, Color.GRAY};
    for (int i = 0; i < fallbackColors.length; i++) {
      addCache((i + 1) + ".png", fallbackColors[i]);
    }

    addCache("playable.png", Color.WHITE);
    addCache("bucket.png", Color.GRAY);
    addCache("inert.png", Color.BLACK);
    addCache("x2.png", null);
    addCache("x3.png", null);
  }

  @Override
  protected void paintCell(Graphics g, int x, int y,
      int x1, int y1, int x2, int y2) {
    Board board = model.level.currentBoard;
    boolean isPreview = board != null;
    if (board == null) {
      board = model.level.initialBoard;
    }

    Cell cell = board.grid[x][y];
    String resource;
    switch (cell.type) {
    case PLAYABLE:
      resource = cell.tile == null ? "playable.png" : (cell.tile.number + 1) +
        ".png";
      break;

    case BUCKET:
      resource = "bucket.png";
      break;

    // TODO: should probably warn in case of default
    case INERT:
    default:
      resource = "inert.png";
    }

    g.drawImage(getCache(resource), x1, y1, null);
  }
}

@@ -1293,47 +1293,8 @@
 rd;%0A
-    boolean isPreview = board != null;%0A
     
@@ -1399,29 +1399,8 @@
 y%5D;%0A
-    String resource;%0A
     
@@ -1445,27 +1445,37 @@
 :%0A      
-resource = 
+g.drawImage(getCache(
 cell.til
@@ -1502,16 +1502,24 @@
 e.png%22 :
+%0A       
  (cell.t
@@ -1539,85 +1539,269 @@
 1) +
-%0A        %22.png%22;%0A      break;%0A%0A    case BUCKET:%0A      resource = %22bucket.png%22
+ %22.png%22), x1, y1, null);%0A%0A      if (cell.tile != null && cell.tile.multiplier %3E 1) %7B%0A        g.drawImage(getCache(%22x%22 + cell.tile.multiplier + %22.png%22), x1, y1, null);%0A      %7D%0A      break;%0A%0A    case BUCKET:%0A      g.drawImage(getCache(%22bucket.png%22), x1, y1, null)
 ;%0A  
@@ -1900,43 +1900,8 @@
 :%0A  
-    resource = %22inert.png%22;%0A    %7D%0A%0A
     
@@ -1925,16 +1925,19 @@
 che(
-resource
+%22inert.png%22
 ), x
@@ -1946,18 +1946,24 @@
  y1, null);%0A
+    %7D%0A
   %7D%0A%7D%0A

Make BuilderBoardView consistent