import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;
import java.text.ParseException;

public class Costa_BlackJack extends TablePanel {
	
    /*
     * Note from Richard Rasala.
     * 
     * 51 changed to 52 since there are 52 cards in a deck.
     */
    
	static int[] deckperm = ProbStatTools.randomPermutation(52);
	static int next=0;
	
public static void main(String[] args) 
{
	new Costa_BlackJack();
}

    private BufferedPanel yourCards
    = new BufferedPanel(600, 200);
    
    private BufferedPanel dealerCards
    = new BufferedPanel(600, 200);
    
    private TextFieldView blank =
        new TextFieldView(" ", 1);
    
      private SimpleAction dealAction =
        new SimpleAction("Show Next Card") {
            public void perform() {ShowNextCard(); }
      };
            
      private SimpleAction shuffleAction =
          new SimpleAction("Shuffle") {
              public void perform() { Shuffle(); }
      };
    
      private SimpleAction showallAction =
          new SimpleAction("Show All Shuffled") {
              public void perform() { ShowAllShuffled(); }
      };
    
    private Object[] mainStuff =
        new Object[] 
            { "Dealer Cards",
            dealerCards ,
            "Your Cards",
            yourCards ,
            dealAction, 
            shuffleAction,
            showallAction};
    
    
    private TablePanel mainPanel =
        new TablePanel(mainStuff, VERTICAL, 10, 10, CENTER);
    
    
    public Costa_BlackJack() 
    {
    	add(mainPanel);
    	addListeners();    	
    	frame("Costa_BlackJack");
    }
   
    
    // Shuffles the Deck
    public static void Shuffle()
    {    	    	
    	deckperm = ProbStatTools.randomPermutation(52);
    	next=0;
    }
    
    
    // Shows the next card in a shuffled Deck
    public void ShowNextCard()
    {
    	ImagePaintableLite nowcard = CardPictures(deckperm[next]);
    	
        //obtain sizes
        int w = yourCards.getBufferWidth();
        int h = yourCards.getBufferHeight();
        
        int w0 = nowcard.getImageWidth();
        int h0 = nowcard.getImageHeight();
        
        int wmin = 5;
        int wmax = w - w0 - wmin;
        
        int hmin = 5;
        int hmax = h - h0 - hmin;
        
        yourCards.clearSequence();
        nowcard.move(50, 50);
        yourCards.appendPaintable(nowcard);
        yourCards.repaint();
    	
       	next++;
    }
    
    public void ShowAllShuffled()
    {
        /*
         * Note from Richard Rasala
         * 
         * The loop below must use the loop index i to access the
         * deckperm not the variable next.
         *
         * If next is already greater than 0
         * because individual cards have been dealt
         * then in the course of the loop next will reach 52
         * causing an array index out of bounds exception.
         */
    	dealerCards.clearSequence();
    	
    	int x = 0;
        int y = 0;
        
    	for (int i=0; i<52; i++)
    	{
    		// ImagePaintableLite nowcard = CardPictures(deckperm[next]);
    		
            ImagePaintableLite nowcard = CardPictures(deckperm[i]);
            
    		int w = dealerCards.getBufferWidth();
            int h = dealerCards.getBufferHeight();
            
            int w0 = nowcard.getImageWidth();
            int h0 = nowcard.getImageHeight();
            
            int wmin = 5;
            int wmax = w - w0 - wmin;
            
            int hmin = 5;
            int hmax = h - h0 - hmin;
            
            nowcard.move(x, y);
            x = x + 10;
            y = y + 2;
            
            dealerCards.appendPaintable(nowcard);
            
            // next++;
    	}
    	dealerCards.repaint();
    }
    
    
    public static ImagePaintableLite CardPictures(int cardnum) {
        // obtain images of the JFitz card deck
        String cardsURL  =
            "http://www.ccs.neu.edu/jpt/images/jfitz_cards/";
        String cardsList =
            "imagelist.txt";
        
        ImagePaintableLite[] cards
            = WebImageTools.readImagesAsPaintableLite
                (cardsURL, cardsList);
        
        return cards[cardnum];
        }
 
    
    private void addListeners() {
        blank.addActionListener(dealAction);
        ;}
 }
    
      
      
        
    
    
      
    
   
    
    
    
	
    

    
   
      

    
    
    

