/* @(#)ConcentrationOptions.java    26 September 2007 */

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;

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

/**
 * <p>The class for the Concentration game options panel.</p>
 * 
 * <p>Uses Java Power Tools 2.6.0.</p>
 * 
 * <p>Copyright, Richard Rasala, 2007.</p>
 * 
 * @author Richard Rasala
 */
public class ConcentrationOptions
    extends DisplayPanel
    implements WindowConstants
{
    
    /** The game paintable data for creating tiles. */
    protected GamePaintables gamePaintables = null;
    
    
    /** The radio panel for selecting tiles. */
    protected RadioPanel selectTiles =
        new RadioPanel(
            new String[] {
                "Use photos for tiles",
                "Use shapes for tiles",
                "Use letters for tiles",
                "Use numbers for tiles" },
            0 );
    
    
    /** The radio panel for selecting the tile grid size. */
    protected RadioPanel selectGridSize =
        new RadioPanel(
            new String[] {
                "Use 3 by 3 grid",
                "Use 4 by 4 grid",
                "Use 5 by 5 grid",
                "Use 6 by 6 grid" },
            3 );
    
    
    /** The new game action. */
    protected SimpleAction newGameAction =
        new SimpleAction("New Concentration Game") {
            public void perform() { newGame(); }
    };
    
    
    /** The new game button. */
    protected JButton newGameButton = new JButton(newGameAction);
    
    
    /** The panel background color. */
    protected Color panelBackground = Colors.white;
    
    
    /** The button background color. */
    protected Color buttonBackground = Colors.yellow;
    
    
    /** The table panel gap. */
    protected int gap = 10;
    
    
    /** The stuff for the two radio panels. */
    protected Object[] radioStuff = { selectTiles, selectGridSize };
    
    
    /** The panel for the two radio panels. */
    protected VTable radioPanel = new VTable(radioStuff, gap, 2 * gap, WEST);
    
    
    /** The stuff for the main GUI. */
    protected Object[] mainStuff = { radioPanel, newGameButton };
    
    
    /** The panel for the main GUI. */
    protected VTable mainPanel = new VTable(mainStuff, gap, gap, CENTER);
    
    
    /**
     * The counter that determines if an image error has been signaled
     * so the user need only see an error message once.
     * 
     */
    protected int imageError = 0;
    
    
    /**
     * <p>Set to true if and only if opened by <code>ConcentrationApplet</code>.</p>
     * 
     * <p>This variable is package-level so that it may only be accessed and set
     * by <code>ConcentrationApplet</code>.</p>
     */
    boolean openedFromApplet = false;
    
    
    /**
     * <p>The default constructor that uses the default
     * game image data to construct the game paintables.</p>
     */
    public ConcentrationOptions() {
        this(new GameImageData());
    }
    
    
    /**
     * <p>The constructor that uses
     * the given game image data
     * to construct the game paintables.</p>
     */
    public ConcentrationOptions(GameImageData data) {
        if (data == null)
            data = new GameImageData();
        
        gamePaintables = new GamePaintables(data);
        
        decorateGUI();
        
        addObject(mainPanel);
    }
    
    
    /**
     * <p>The constructor that uses
     * the given args
     * to set the game image data
     * to construct the game paintables.</p>
     *
     */
    public ConcentrationOptions(String[] args) {
        this(new GameImageData(args));
    }
    
    
    /**
     * <p>Return the array of <code>Paintable</code> objects that
     * correspond to the current selection in the select tiles
     * radio panel.</p>
     */
    public Paintable[] getPaintables() {
        Paintable[] paintables = null;
        
        int option = selectTiles.getSelectedIndex();
        
        switch (option) {
            case 0:
                paintables = gamePaintables.getImagePaintables();
                break;
                
            case 1:
                paintables = gamePaintables.getShapePaintables();
                break;
                
            case 2:
                paintables = gamePaintables.getLetterPaintables();
                break;
                
            case 3:
                paintables = gamePaintables.getNumberPaintables();
                break;
        }
        
        if (paintables == null) {
            if (imageError == 0) {
                imageError = 1;
                
                String message = "Concentration Images Failed to Load";
                String title = "Error";
                
                GeneralDialog.showOKDialog(message, title);
            }
            
            paintables = gamePaintables.getShapePaintables();
        }
        
        return paintables;
    }
    
    
    /**
     * <p>Return the grid size indicated by the current selection
     * in the select grid size radio panel.</p>
     * 
     * <p>The value returned will be between 3 and 6.</p>
     */
    public int getGridSize() {
        return selectGridSize.getSelectedIndex() + 3;
    }
    
    
    /**
     * Return the tile size in pixels.
     */
    public int getTileSize() {
        return gamePaintables.getTileSize();
    }
    
    
    /** Decorate the GUI. */
    protected void decorateGUI() {
        radioPanel.emptyBorder(gap);
        radioPanel.titleBorder("Concentration Options");
        
        mainPanel.emptyBorder(gap);
        mainPanel.setDeepBackground(panelBackground);
        
        newGameButton.setBackground(buttonBackground);
    }
    
    
    /**
     * <p>Launch a new Concentration game corresponding
     * to the settings in the radio panels.</p>
     */
    public void newGame() {
        Paintable[] paintables = getPaintables();
        int tileSize = getTileSize();
        int gridSize = getGridSize();
        
        ConcentrationGame game =
            new ConcentrationGame(paintables, tileSize, gridSize);
        
        JPTFrame frame = game.frame("Concentration Game");
        
        if (openedFromApplet) {
            frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }
    }
    
    
    /**
     * <p>This main method opens a new <code>ConcentrationOptions</code>
     * panel in a frame.  From that panel, one or more instances of a
     * <code>ConcentrationGame</code> may be opened.</p>
     * 
     * <p>If args are supplied, they are passed to the constructor
     * of the <code>ConcentrationOptions</code> panel.</p>
     */
    public static void main(String[] args) {
        new ConcentrationOptions(args).frame("Options", WEST);
    }
    
}

