////////////////////////////////////////////////////////////////////////////////////
// CardPictureStore.java
//
// Implementation of class CardPictureStore, a collection of Images indexed by
// card suit and value.

import java.util.*;
import java.applet.*;
import java.awt.*;

public class CardPictureStore
{
    Image[] images = new Image[52];
    Image   imBack, imBottom;
    Vector  suits;
    int     cxCard, cyCard;
    String  directory;
    
    public CardPictureStore(Applet a, String directory)
    {
        // Create a 1x1 Graphics to preload images
        Image iTemp = a.createImage(1, 1);
        Graphics g = iTemp.getGraphics();
        
        this.directory = directory;
        a.showStatus("Loading '" + directory + "' card images...");
        try 
        {
            suits = Suit.AllSuits();
            for (int s = 0; s < 4; s++) 
            {
                for (int cn = 0; cn < 13; cn++)
                {
                    images[s * 13 + cn] = a.getImage(a.getDocumentBase(), CardFile((Suit)suits.elementAt(s), cn + 1));
                    g.drawImage(images[s * 13 + cn], 0, 0, null);
                }
            }
            
            imBack = a.getImage(a.getDocumentBase(), directory + "/back1.gif");
            g.drawImage(imBack, 0, 0, null);
            imBottom = a.getImage(a.getDocumentBase(), directory + "/bottom.gif");
            g.drawImage(imBottom, 0, 0, null);
            do
            {
                cxCard = imBottom.getWidth(null);
                cyCard = imBottom.getHeight(null);
            }
            while ((cxCard == -1) || (cyCard == -1));
            a.showStatus("Load complete.");
        }
        catch (Exception e) 
        {
            System.err.println("Error " + e.getMessage());
            a.showStatus("Card load failed.");
        }
    }
    
    public Dimension GetCardSize()
    {
        return new Dimension(cxCard, cyCard);
    }

    public void DrawCard(Graphics g, Component c, int x, int y, Suit s, int value)
    {
        Image i = GetCardImage(s, value);
        if (i == null)
            return;
        g.drawImage(i, x, y, c);
    }
    
    public Image GetCardImage(Suit s, int value)
    {
        if ((value < 1) || (value > 13))
            return null;
        
        for (int i = 0; i < 4; i++)
        {
            if (suits.elementAt(i) == s)
                return images[i * 13 + (value - 1)];
        }
        
        return null;
    }

    protected String CardFile(Suit s, int value) throws IllegalArgumentException 
    {
        char sc;
        
        if (s.isClubs())
            sc = 'c';
        else if (s.isDiamonds())
            sc = 'd';
        else if (s.isHearts())
            sc = 'h';
        else if (s.isSpades())
            sc = 's';
        else
            throw new IllegalArgumentException("Bad card suit");

        if ((value < 1) || (value > 13))
            throw new IllegalArgumentException("Bad card number");

        if (value < 10)
            return directory + "/0" + value + sc + ".gif";
        else
            return directory + "/" + value + sc + ".gif";
    }
    
    public Image GetCardBack()
    {
        return imBack;
    }
    
    public Image GetEmptyCard()
    {
        return imBottom;
    }
}