package minesweeper.view;

import java.awt.*;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 * @author Daniel "3ICE" Berezvai
 */
public class MinesweeperTile extends JButton {

public final int x, y;
/**
 * 0 null, 1 Blue; 2 Green; 3 Red; 4 Purple;
 *
 * 5 Maroon; 6 Turquoise; 7 Black; 8 Gray;
 */
private static final Color[] foreGroundColors = new Color[]{
  null, Color.blue, Color.green, Color.red, new Color(255, 0, 255),
  new Color(128, 0, 0), new Color(64, 224, 208), Color.black, Color.gray
};

public MinesweeperTile(int x, int y, MouseListener a) {
  super();
  this.x = x;
  this.y = y;
  addMouseListener(a);
  setUI(new BasicButtonUI());
  setPreferredSize(new Dimension(30, 30));
  setMargin(new Insets(1, 1, 1, 1));//3ICE: This avoids the "..." problem.
  setFont(new Font("Courier new", Font.BOLD, 20));
  setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

public void setNum(int n) {
  if (n == 0) {
    //setText("");//3ICE: Not necessary for the rare case of unflagging a safe tile.
    setBackground(Color.white);
  } else if (n > 0) {
    setText("" + n);
    setForeground(foreGroundColors[n]);
    setBackground(Color.lightGray);
  } else {
    setText("*");
    setBackground(Color.red);
  }
}

public void flag() {
  setText("♪");//3ICE: I know this isn't a real "flag".
}
}
