package queenproblem;

import java.awt.*;
import javax.swing.*;

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

  private Point p;
  private boolean queen;

  public ChessBoardButton(int i, int j) {
    super();
    //super.setText(i + " " + j);
    p = new Point(i, j);
    queen = false;
    setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.BLACK);
    setForeground((i + j) % 2 == 0 ? Color.BLACK : Color.WHITE);
    setFont(new Font("Courier new", Font.BOLD, 16));
  }

  public int getPointX() {
    return p.x;
  }

  public int getPointY() {
    return p.y;
  }

  public boolean isQueen() {
    return queen;
  }

  public void setQueen(boolean queen) {
    this.queen = queen;
    setText(null);
  }

  void setQueenInverse() {
    setQueen(!isQueen());
  }

//Hiba: Overridable method call in constructor.
//  public void setBackground(Color bg) {
//    //Minek bonyolítjuk túl? Elég lenne a konstruktorban ezt hívni:
//    //setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.BLACK);
//    if (p != null) {
//      super.setBackground((p.x + p.y) % 2 == 0 ? Color.WHITE : Color.BLACK);
//      super.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//    } else {
//      System.out.println("Calling overridden methods in the contructor when the object is not fully initialized is stupid and wasteful.");
//      super.setBackground(null);
//    }
//  }
  public void setText(String text) {
    //Ezt is minek bonyolítjuk túl? Elég lenne setQueen-ben meghívni ezt:
    //setText(queen ? "k" : "");
    super.setText(queen ? "K" : "");
  }

  public void setEnabled(boolean b) {
    //Ezzel a felüldefiniálással egyet értek.
    super.setEnabled(b);
    if (!b) {
      super.setBackground((p.x + p.y) % 2 == 0 ? Color.LIGHT_GRAY : Color.DARK_GRAY);
    } else {
      super.setBackground((p.x + p.y) % 2 == 0 ? Color.WHITE : Color.BLACK);
    }
  }
}
