/*cent
 * MyFirst solo Project Spring 2014
 * Matching Card Game
 */

package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * This class creates a JOptionpane that lets the user decide what values they want in 
 * the deck.
 * 
 * @author Brian Dennis
 * @version 3-29-2014
 */
@SuppressWarnings("serial")
public class ChangeDeck extends JPanel implements ActionListener {
    
    /** the names of the values of cards. */
    private static final String[] MY_VALUE_NAMES = {"Ace", "2", "3", "4", "5",
        "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    
    /** An empty ArrayList to hold new values the player wants to play the game with. */
    private final List<String> myNewValues;
    
    /** An ArrayList to hold the check boxes representing each card. */
    private final List<JCheckBox> myCheckBoxes;
    
    /** the frame of this GUI. */
    private final MatchingGameGUI myFrame;

    /**
     * this constructor initializes variables and triggers the optionPane.
     * @param theFrame the frame where the game is played
     */
    public ChangeDeck(final MatchingGameGUI theFrame) {
        
        super();
        myFrame = theFrame;
        setLayout(new GridLayout(5, 5));
        myNewValues = new ArrayList<>();
        myCheckBoxes = createCheckBoxes();
    }
    
    /**
     * creates the check boxes in the JOptionPane.
     * 
     * @return the ArrayList that ho;lds all the check boxes
     */
    private List<JCheckBox> createCheckBoxes() {
        
        final List<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();
        
        for (int i = 0; i < MY_VALUE_NAMES.length; i++) {
            
            final JCheckBox box = new JCheckBox(MY_VALUE_NAMES[i] + "s");
            box.setSelected(false);
            box.addActionListener(this);
            add(box);
            checkBoxes.add(box);
        }
        
        final JLabel label = new JLabel("Total Cards: ");
        add(label, 13);  
        
        return checkBoxes;
    }
    
    
    /**
     * triggers the option pane.
     */
    public void triggereOptionPane() {
        
        final int choice = JOptionPane.showConfirmDialog(null, this, 
                          "Click the values you want inside your deck.", 
                          JOptionPane.OK_CANCEL_OPTION);
        
        if (choice == 0) {
            
            myFrame.dispose();
            MatchingGameMain.changeValues(myNewValues);
            //myFrame.dispatchEvent(new WindowEvent(myFrame, 
                                                  //WindowEvent.WINDOW_CLOSING));
        } 
    }
    
    /**
     * This method listens for events from when the check boxes are clicked, and performs 
     * action accordingly.
     * 
     * @param theEvent the event that is firted for the checkboxes
     */
    public void actionPerformed(final ActionEvent theEvent) {
        
        for (int i = 0; i < myCheckBoxes.size(); i++) {
            
            if (myCheckBoxes.get(i).isSelected() && !myNewValues.contains(MY_VALUE_NAMES[i])) {
                
                myNewValues.add(MY_VALUE_NAMES[i]);
                
            } else if (!myCheckBoxes.get(i).isSelected() 
                                                 && myNewValues.contains(MY_VALUE_NAMES[i])) {

                myNewValues.remove(myNewValues.indexOf(MY_VALUE_NAMES[i]));
            }
        }               
    }
}