// CMPU-101-51, Spring 2007
// Asmt 9 Solutions:  GraphicalYahtzee.java

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

// -----------------------------
//  The GraphicalYahtzee class
// -----------------------------

class GraphicalYahtzee extends JFrame implements ActionListener{

  // --------------------------
  //  class-oriented data 
  // --------------------------

  static final int 
    FRAME_WIDTH = 600, FRAME_HEIGHT = 600,
    SCORE_LEFT_MARGIN = 50, SCORE_TOP_MARGIN = 50,
    SCORE_WIDTH = 220, SCORE_HEIGHT = 320,
    DICE_LEFT_MARGIN = 50, DICE_TOP_MARGIN = 400, 
    DICE_WIDTH = 38, DICE_HEIGHT = 38,
    DICE_BORDER = 2, DICE_PADDING = 5;
  
  // --------------------------
  //  object-oriented data 
  // --------------------------
  
  // the content pane associated with the JFrame
  Container cPane;
  
  // the Yahtzee game
  Yahtzee yahtz;
  
  // an array of panels constituting the DICE
  DiePanel[] dice = new DiePanel[5];
  
  // the panel for displaying the scores
  ScorePanel scorePanel;
  
  // an array of JButtons for the categories
  JButton[] catButtons = new JButton[6];
  
  
  // -----------------------
  //   constructor
  // -----------------------
  
  GraphicalYahtzee() {

    this.yahtz = new Yahtzee();
    
    // set the size of the onscreen window
    this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    this.setTitle("GRAPHICAL YAHTZEE!");
    // get a reference to the "content pane" from the JFrame
    //   store it in the "cPane" field
    this.cPane = this.getContentPane();
    // tell the "content pane" that we will provide absolute
    //   coordinates for all panels to be added to it
    this.cPane.setLayout(null);

    this.scorePanel = new ScorePanel(yahtz);
    // specify the location and size of the scorePanel
    this.scorePanel.setBounds(SCORE_LEFT_MARGIN,
                              SCORE_TOP_MARGIN,
                              SCORE_WIDTH,
                              SCORE_HEIGHT);
    // ask the "contentPane" to add the new scorePanel
    //    to its contents.
    this.cPane.add(this.scorePanel);
       
    // set up the DICE panels
    for (int i=0; i<5; i++) {
      // create a new DiePanel 
      this.dice[i] = new DiePanel(yahtz, i);
      // specify the location and size of the new panel
      int topLeft = DICE_LEFT_MARGIN
        + i*(DICE_WIDTH + DICE_BORDER + DICE_PADDING);
      this.dice[i].setBounds(topLeft, DICE_TOP_MARGIN,
                             DICE_WIDTH, DICE_HEIGHT);
      // ask the "contentPane" to add the new panel to
      //   its contents.
      this.cPane.add(this.dice[i]);
    }
    
    // set up the Category Buttons
    for (int i=0; i<6; i++) {
      this.catButtons[i] = new JButton("a");
      this.catButtons[i].setBounds(280, 50+50*i,
                                   100,45);
      this.cPane.add(this.catButtons[i]);
      this.catButtons[i].addActionListener(this);
    }
      
    // Make the JFrame visible
    this.setVisible(true);
    
    // call the playGame function!
    this.playGame();
  }

  // getValidCategory
  
  int getValidCategory() {
    // set up connection to keyboard
    Scanner sc = new Scanner(System.in);
    // kat will eventually hold the category entered by the player
    int kat = 0;
    // need is a boolean variable that governs the while loop
    boolean need = true;
    while (need) {
      // prompt the user to type something
      System.out.println("Enter a category to score your hand!"
                           + "  (or -1 to quit)");
      // get an int value from the user
      kat = sc.nextInt();
      // check whether it is a 
      if ((kat == -1) 
            || ((kat >= 1) 
                  && (kat <= 6) 
                  && (yahtz.isValidCategory(kat))))
      need = false;
    }
    // now that we're out of the while loop, we know we have a good categ.
    return kat;
  }
  
  // getValidKeeperString
  
  String getValidKeeperString() {
    
    // set up connection to keyboard
    Scanner sc = new Scanner(System.in);
    // str will eventually hold the string to return
    String str = ""; 
    // need governs the while loop
    boolean need = true;
    while (need) {
      // prompt the user to enter something
      System.out.println("Enter a valid keeper string (5 chars) "
                           + "... or -1 to quit)");
      // get the next String from the keyboard
      str = sc.next();
      // if the string is okay, set need to false so we can break
      // out of the while loop
      if (str.equals("-1") || str.length() == 5) need = false;
    }
    return str;
  }
  
  // playGame
  
  void playGame() {
    
    yahtz.resetGame();
    
    // outer for loop:  6 turns (one category filled for each turn)
    for (int i=0; i<6; i++) {
      // ask the yahtzee object to create a hand for the next turn
      YahtzeeHand h = yahtz.getHandForNextTurn();
      repaint();
      // the player gets two chances to re-roll the dice
      for (int roll = 0;  roll < 2; roll++) {
        // get a valid keeper string
        String keeperStr = getValidKeeperString();
        // check if player wants to quit
        if (keeperStr.equals("-1")) {
          System.out.println("Okay... fine... ABORTING GAME!");
          return;
        }
        else {
          // ask the yahtzeehand object to roll the dice selected by player
          h.rollEm(keeperStr);
          repaint();
        }
      }
      // get a category from the player
      int cat = getValidCategory();
      // check if player wants to quit
      if (cat == -1) {
        System.out.println("Okay, fine... ABORTING GAME!");
        return;
      }
      else {
        // ask the yahtzee object to score the hand in the selected category
        yahtz.scoreHand(cat);
        repaint();
      }
    }
    System.out.println("Well... you're done!  Final Score: " +
                       yahtz.computeTotalScore());
  }
 
  
  public void actionPerformed(ActionEvent e)
  {
     System.out.println("actionPerformed!");
     for (int i=0; i<6; i++){
       if (e.getSource().equals(catButtons[i])) {
         System.out.println("Button " + i + " clicked!");
         this.yahtz.scoreHand(i+1);
       }
     }    
     
  }
}

  
  
  
  
