import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class BuggyHangmanGUI extends JFrame implements ActionListener {
        
    private JPanel imagePanel = new JPanel();
    private JPanel wordPanel = new JPanel();
    private JLabel guess = new JLabel();
    private JLabel message = new JLabel("", 
                                        javax.swing.SwingConstants.CENTER);
        
    private final static String QUIT = "Quit";
    private final static String REPLAY = "Replay";
    private StringBuffer wordBuf = new StringBuffer("");
    private HangmanEngine engine = new HangmanEngine();
    StickDraw figure = new StickDraw();
    
    String[] alphabuttons = new String[]{"a","b","c","d","e",
                                         "f","g","h","i","j","k","l",
                                         "m","n","o","p","q","r","s",
                                         "t","u","v","w","x","y","z"};
    JButton[] letterButtons = new JButton[alphabuttons.length];

        
    public BuggyHangmanGUI() {
        setTitle("Hangman Game");
        setLayout(new GridLayout(0, 2));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        makeWordPanel();
        makeImagePanel();
        startGame();
                
        getContentPane().add(wordPanel);
        getContentPane().add(imagePanel);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
        
    private void startGame() {
        engine.setWord();
        figure.init();
        // enable all letter buttons
        for (int i=0;i<alphabuttons.length;i+=1)
            letterButtons[i].setEnabled(true);  
        message.setText("Pick a letter");
                
        for (int i=0;i<engine.getWordLength();i++) {
            wordBuf.append("*"); 
        }
        guess.setText(wordBuf.toString());
    }
        
    private void makeImagePanel() {
        imagePanel.setLayout(new BorderLayout());
        imagePanel.add(figure, BorderLayout.CENTER);
        imagePanel.add(message, BorderLayout.SOUTH);
    }
        
        
    /**
     * 
     */
    private void makeWordPanel () {
        wordPanel.setLayout(new BorderLayout());
        guess.setFont(new Font(guess.getFont().getFontName(), 
                               guess.getFont().getStyle(),
                               guess.getFont().getSize()*2));
        JPanel guessPanel = new JPanel();
        guessPanel.add(guess); 
        wordPanel.add(guessPanel, BorderLayout.NORTH);
                
        JPanel letterPanel = new JPanel(new GridLayout(5, 6));
                 
        for (int i=0;i<alphabuttons.length;i+=1)
        {
            // set the buttons of the alphabet up using the array
            letterButtons[i] = new JButton(alphabuttons[i]);
            letterPanel.add(letterButtons[i]);
            letterButtons[i].addActionListener(this);
        }
        wordPanel.add(letterPanel, BorderLayout.CENTER);
                
        JPanel actionPanel = new JPanel(new FlowLayout());
        JButton playButton = new JButton(REPLAY);
        playButton.addActionListener(this);
        actionPanel.add(playButton);
        JButton quitButton = new JButton(QUIT);
        quitButton.addActionListener(this);
        actionPanel.add(quitButton);
        wordPanel.add(actionPanel, BorderLayout.SOUTH);
    }
        
    public void actionPerformed(ActionEvent e) {
        String text = e.getActionCommand();
        
        if (text.equals(QUIT)) 
            System.exit(0);
        else if (text.equals(REPLAY)) {
            startGame();
        }
        else
            processLetter(text, e.getSource());
    }
        
    private void processLetter(String letterGuess, Object button) {
        if (engine.wordGuessed(guess.getText()))
            message.setText("YOU WIN!!! Congratulations");
        if (figure.full()) 
            message.setText("You lose.  The word was " + engine.getAnswer());
                
        if (engine.checkGuess(letterGuess)) 
            guess.setText(engine.show(letterGuess,
                                      guess.getText()));
        else 
            figure.add();
        // disable already guessed button
        ((JButton)button).setEnabled(false);
    }
                

    public static void main(String[] args) {
        new BuggyHangmanGUI( );
    }

}
