/********************************************************************
Class tiles_panel
 --each player's tiles

Authors: Kristi Garner, Mary Garland    
CS406: Java and Internet Programming
*******************************************************************/
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
import java.net.*;
import java.applet.Applet;


class tiles_panel extends Panel
{  
  private game_panel selected_first = null;	// selected tile
  private game_panel Grid[];			// all 7 tiles
  private board_panel bp;			// game board                  
  private Image emptyimage;			// image to be displayed when tile empty
  private int player;				// which player's tiles
  private int col_max = 7;		// 7 tiles
  private final int emptypanelheight = 27;
  private final int emptypanelwidth = 27;
  private static tileBag tbag;
  private Image tile_images[];
  private int letterPts[] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};


  /*******************************************************************/
  // constructor tiles_panel
  // parameters: i - image to be displayed when tile empty
  //             p - player number
  public tiles_panel(Image ei, int p, Image i[], board_panel b)
  {
    Grid = new game_panel[col_max];
    emptyimage = ei;
    player = p;
    tbag = new tileBag();
    tile_images = i;
    bp = b;
    this.setBackground(Color.black);
    this.setLayout(null);
    this.setSize(emptypanelwidth*col_max, emptypanelheight*1);
    add_a_panel();
    replenish();
  }

  /*******************************************************************/
  // add_a_panel- adds each tile to the panel 
  // parameters: image- image to be displayed on the tile
  //             col - location of tile
  // 		 l - this tile's letter
  //             pt_value - this letter's point value
  //		 b - board panel           
  public void add_a_panel()
  {

    int i;

    for(i = 0; i < 7; i++) {
  
      Grid[i] = new game_panel(emptyimage, 0, i, bp, this, true);
      Grid[i].setPlayerNbr(1);

      Grid[i].setEmpty(true);
      this.add(Grid[i]);
    }
  } 

  /*******************************************************************/
  // setSelectedFirst - saves the selected tile
  public void setSelectedFirst(game_panel selected)
  {
    selected_first = selected;
  } 

  /*******************************************************************/
  // getSelectedFirst - returns the selected tile 
  public game_panel getSelectedFirst()
  {
    return selected_first;
  } 

  /*******************************************************************/
  // getEmptyImage - returns the image to be displayed when a tile is empty
  public Image getEmptyImage() {
    return emptyimage;
  }


  /*******************************************************************/
  // tilesNewTurn - sets all tiles to not current
  public void tilesNewTurn() {

    int j;
   
    for(j=0; j < col_max; j++) {
      Grid[j].setCurrent(false);
    }
  }

  /*******************************************************************/
  // tilesUndoTurn - set all tiles that are current back to the original image
  //               - tiles are no longer current, and not empty
  public void tilesUndoTurn() {
    
    int j;
   
    for(j=0; j < col_max; j++) {
      if( Grid[j].getCurrent() ) {
        Grid[j].setCurrent(false);
        Grid[j].setOrigImage();
        Grid[j].setEmpty(false);
      }
    }
  }

  public boolean replenish() {
    
    int j, n;
    char c;
   
    for(j=0; j < col_max; j++) {

      if( Grid[j].isEmpty() ) {
        c = tbag.nextLetter();
        n = tbag.nextInt();
        if(c == '0') {
          System.out.println("Out of Tiles");
          return true;
        } else {
          Grid[j].setLetter(c);
          Grid[j].setImage(tile_images[n]);
          Grid[j].setBackImage(tile_images[n]);
          Grid[j].setEmpty(false);
          Grid[j].setLetterPtValue(letterPts[n]);
        }
      }
    }
    return false;
  } 


  public Image getImage(int x) {

    return tile_images[x];

  }


  public int getPoints(int x) {
  
    return letterPts[x];

  }

  public int emptyTiles() {
 
    int j, sum = 0;
   
    for(j=0; j < col_max; j++) {
      if(!Grid[j].isEmpty() ) {
        sum++;
      }
    }
    return sum;

  }

} // end class tiles_panel
