import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;

public class Keno extends Applet implements MouseListener
{
  Graphics graphics;

  AudioClip beep;

  String imagePath;

  int squareSize;

  KenoGame kenoGame;

  int gameState = 0;

  int previousGameState;

  static final int UNMARKED = 0;
  static final int PLAYERMARKED = 1;
  static final int HOUSEMARKED = 2;
  static final int WIN = 3;

  Image [] [] []  image = new Image [4] [10] [2];
  
  Image goButton;
  Image newButton;
  Image setButton;
  Image resetButton;

  GraphicText graphicText;

  public void init() {
    graphics = getGraphics();

    addMouseListener(this);

    squareSize = 26;

    /* get board images */

    initializeImages(UNMARKED, "unmarked");
    initializeImages(PLAYERMARKED, "playerMarked");
    initializeImages(HOUSEMARKED, "houseMarked");
    initializeImages(WIN, "win");

    /* get button images */

    goButton = getImageFromJAR("go.gif");
    newButton = getImageFromJAR("new.gif");
    setButton = getImageFromJAR("set.gif");
    resetButton = getImageFromJAR("reset.gif");

    /* set up graphic text */

    graphicText = new GraphicText(this, "images/characters/");
    graphicText.setOptions(10, 6, 25, 0);

    beep = getAudioClip(getCodeBase(), "audio/beep.au");

    kenoGame = new KenoGame(this);
    displayBoard();
  }

  void initializeImages(int imageState, String imagePath) {
    image [imageState] [0] [0] = getImageFromJAR(imagePath + "00.gif");
    image [imageState] [0] [1] = getImageFromJAR(imagePath + "0.gif");
    image [imageState] [1] [0] = getImageFromJAR(imagePath + "10.gif");
    image [imageState] [1] [1] = getImageFromJAR(imagePath + "01.gif");
    image [imageState] [2] [0] = getImageFromJAR(imagePath + "20.gif");
    image [imageState] [2] [1] = getImageFromJAR(imagePath + "02.gif");
    image [imageState] [3] [0] = getImageFromJAR(imagePath + "30.gif");
    image [imageState] [3] [1] = getImageFromJAR(imagePath + "03.gif");
    image [imageState] [4] [0] = getImageFromJAR(imagePath + "40.gif");
    image [imageState] [4] [1] = getImageFromJAR(imagePath + "04.gif");
    image [imageState] [5] [0] = getImageFromJAR(imagePath + "50.gif");
    image [imageState] [5] [1] = getImageFromJAR(imagePath + "05.gif");
    image [imageState] [6] [0] = getImageFromJAR(imagePath + "60.gif");
    image [imageState] [6] [1] = getImageFromJAR(imagePath + "06.gif");
    image [imageState] [7] [0] = getImageFromJAR(imagePath + "70.gif");
    image [imageState] [7] [1] = getImageFromJAR(imagePath + "07.gif");
    image [imageState] [8] [0] = getImageFromJAR(imagePath + "80.gif");
    image [imageState] [8] [1] = getImageFromJAR(imagePath + "08.gif");
    image [imageState] [9] [0] = getImageFromJAR(imagePath + "90.gif");
    image [imageState] [9] [1] = getImageFromJAR(imagePath + "09.gif");
  }

  Image getImageFromJAR(String fileName) {
    URL imageURL = getClass().getResource(fileName);
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image img = null;
    try {
      img = tk.createImage(
        (java.awt.image.ImageProducer)imageURL.getContent());
    }
    catch (java.io.IOException e) {
      System.out.println(e);
    }
    return img;
  }

  void beepUser() {
    beep.play();
  }

  void drawSquare(int n, int squareType) {
    int i, j;
    int iImage, jImage;

    i = n / 10;
    j = n % 10;
    iImage = (n + 1) / 10;
    jImage = (n + 1) % 10;

    graphics.drawImage(
      image [squareType] [iImage] [0], j*squareSize, i*squareSize, this);
    graphics.drawImage(
      image [squareType] [jImage] [1],
      j*squareSize + squareSize / 2, i*squareSize, this);
  }

  void drawButton(int n, Image buttonImage) {
    int i, j;
    i = n / 10;
    j = n % 10;
    graphics.drawImage(buttonImage, j*squareSize, i*squareSize, this);
  }

  void displayBoard() {
    int gameWinnings = kenoGame.gameWinnings;
    int totalWinnings = kenoGame.totalWinnings;
    kenoGame.displayBoard();
    drawButton(80, goButton);
    drawButton(81, newButton);
    drawButton(82, setButton);
    drawButton(83, resetButton);
    graphicText.setOptions(10, 6, 25, 0);
    graphicText.printResults("Results after game: " + kenoGame.gameNumber);
    graphicText.setOptions(10, 6, 26, 0);
    graphicText.printResults(
      "Bet amount = " + displayWagerAmount(kenoGame.wagerAmount));
    graphicText.setOptions(10, 6, 27, 0);
    graphicText.printResults(
      "Game winnings = " + displayAmount(gameWinnings) + "      ");
    graphicText.setOptions(10, 6, 28, 0);
    graphicText.printResults(
      "Total winnings = " + displayAmount(totalWinnings) + "      ");
  }

  String displayAmount(int amount) {
    return
      "$" +
      displayDollars(amount) + "." +
      displayCents(amount);
  }

  String displayWagerAmount(int amount) {
    return
      "$" +
      displayWagerDollars(amount) + "." +
      displayCents(amount);
  }

  String displayDollars(int amount) {
    return Integer.toString(amount / 100);
  }

  String displayWagerDollars(int amount) {
    int dollars = amount / 100;
    String fill = "";
    if (dollars <= 9)
      fill = "0";
    return fill + Integer.toString(dollars);
  }

  String displayCents(int amount) {
    int cents = amount % 100;
    if (cents < 0) cents *= -1;
    String fill = "";
    if (cents <= 9)
      fill = "0";
    return fill + Integer.toString(cents);
  }

  int getSquareCoordinate(int x, int y) {
    int i, j;

    j = x / squareSize;
    i = y / squareSize;

    if (i > 8 || j > 9)
      return 100; // out of range coordinate

    return i * 10 + j;
  }

  boolean wagerSelected(int x, int y) {
    int lineNumber = graphicText.getLineNumber(y);
    int charNumber = graphicText.getCharNumber(x);
    return 
      lineNumber == 26 && (
        charNumber == 14 ||
        charNumber == 15 ||
        charNumber == 17 ||
        charNumber == 18);
  }

  void setWager(int x, int y) {
    int lineNumber = graphicText.getLineNumber(y);
    int charNumber = graphicText.getCharNumber(x);
    int digit;
    if (charNumber == 14)
      digit = 3;
    else
    if (charNumber == 15)
      digit = 2;
    else
    if (charNumber == 17)
      digit = 1;
    else
      digit = 0;
    kenoGame.changeWagerAmountByDigit(digit);
  }

  boolean badCoordinate(int n) {
    return n > 83;
  }

  boolean previousWasGo() {
    if (previousGameState == 80) {
      beepUser();
      gameState = previousGameState;
      return true;
    }
    return false;
  }

  public void mouseReleased(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    previousGameState = gameState;
    gameState = getSquareCoordinate(x, y);
    if (wagerSelected(x, y)) {
      if (!previousWasGo()) {
        setWager(x, y);
        displayBoard();
      }
    }
    else
    if (badCoordinate(gameState))
      beepUser();
    else
    if (gameState == 80) {
      if (!previousWasGo()) {
        kenoGame.houseSelection();
        displayBoard();
      }
    }
    else
    if (gameState == 81)
      kenoGame.newGame();
    else
    if (gameState == 82) {
      if (!previousWasGo())
        kenoGame.rememberPosition();
    }
    else
    if (gameState == 83)
      kenoGame.forgetPosition();
    else {
      if (!previousWasGo())
        kenoGame.playerSelection(gameState);
    }
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void paint(Graphics g) {
    displayBoard();
  }

  public void destroy() {
    removeMouseListener(this);
  }

  public String getAppletInfo() {
    return "Keno game by Rick Miskowski";
  }
}
