package ste.pendu;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;

/**
 * Le PenduBean!
 *
 * @author <a href="mailto:arnaud.vandyck@ulg.ac.be">Arnaud Vandyck</a>
 * @version 1.0
 */
public class PenduBean extends JPanel
  implements PropertyChangeListener
{

  public static final String MOT_EN_COURS = "Mot en cours: ";
  public static final String LETTRES_ENTREES = "Lettres Entrées: ";

  private PenduModele modele;
  
  private PenduPerduVeto perdu = null;

  private class MonEcouteur implements ActionListener
  {
    
    public MonEcouteur()
    {
    }

    public void actionPerformed (ActionEvent action)
    {
      String s = ( (JButton)action.getSource() ).getText();
      PenduBean.this.modele.addLetter ( s.charAt (0) );
      ( (JButton)action.getSource() ).setEnabled(false);
    }
    
  }
  
  public PenduBean()
  {
    super (new BorderLayout ());
    this.modele = new PenduModele();
    this.modele.addVetoableChangeListener(new PenduGagneVeto());
    this.perdu = new PenduPerduVeto (7);
    this.modele.addVetoableChangeListener(this.perdu);
    this.add (this.getMotEnCours(), BorderLayout.NORTH);
    this.add (this.getBoutons(), BorderLayout.CENTER);
    this.add (this.getLettresEntrees(), BorderLayout.SOUTH);
    this.modele.addPropertyChangeListener(PenduModele.STATE, this);
  }

  public void propertyChange (PropertyChangeEvent evt)
  {
    Container parent = this.getParent();
    if (((Integer)evt.getOldValue()).intValue()==PenduModele.WON)
      {
        //JOptionPane.showMessageDialog(null, "Gagné!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showInternalMessageDialog(parent, "Gagné!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
      }
    else
      {
        //JOptionPane.showMessageDialog(null, "Perdu!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showInternalMessageDialog(parent, "Perdu!", "Pendu!", JOptionPane.INFORMATION_MESSAGE);
      }
  }

  protected Component getMotEnCours()
  {
    JPanel panel = new JPanel ();
    final JLabel motEnCours = new JLabel (PenduBean.MOT_EN_COURS,
                                          SwingConstants.CENTER);
    this.modele.addPropertyChangeListener
      (new PropertyChangeListener()
        {
          public void propertyChange(PropertyChangeEvent evt)
          {
            motEnCours.setText
              (PenduBean.MOT_EN_COURS + PenduBean.this.modele.getWord());
          }
        });
    panel.add (motEnCours);
    return panel;
  }

  protected Component getBoutons()
  {
    MonEcouteur me = new MonEcouteur();
    JPanel panelPrinc = new JPanel();
    JPanel panel = new JPanel(new GridLayout(5,6));
    for (char i = 'a'; i<= 'z'; i++)
      {
        JButton b = new JButton ( String.valueOf(i) );
        b.addActionListener (me);
        b.setEnabled(false);
        panel.add (b);
      }
    panelPrinc.add(panel);
    return panelPrinc;
  }

  protected Component getLettresEntrees()
  {
    JPanel panel = new JPanel ();
    final JLabel lettresEntrees = new JLabel (PenduBean.LETTRES_ENTREES,
                                              SwingConstants.CENTER);
    this.modele.addPropertyChangeListener
      (new PropertyChangeListener()
        {
          public void propertyChange(PropertyChangeEvent evt)
          {
            lettresEntrees.setText
              (PenduBean.LETTRES_ENTREES +
               new String(PenduBean.this.modele.getLetters()));
          }
        });
    panel.add (lettresEntrees);
    return panel;
  }

  /**
   * Get the Word value.
   * @return the Word value.
   */
  public String getWord()
  {
    return this.modele.getWord();
  }

  /**
   * Set the Word value the game can begin.
   * @param newWord The new Word value.
   */
  public void setWord (String newWord)
  {
    this.modele.setWord(newWord);
    parseComponents(this.getComponents());
  }

  private void parseComponents(Component[] c)
  {
    if (c!=null && c.length>0)
      {
        for (int i=0; i<c.length; i++)
          {
            if (c[i] instanceof Container)
              {
                c[i].setEnabled(true);
                parseComponents(((Container)c[i]).getComponents());
              }
            else
              {
                c[i].setEnabled(true);
              }
          }
      }
  }

  /**
   * Get the invalid letters.
   * @return the Letter value.
   */
  public char[] getLetters()
  {
    return this.modele.getLetters();
  }

  /**
   * Add a letter.
   * @param newLetter The new Letter value.
   */
  public void addLetter (char newLetter)
  {
    this.modele.addLetter(newLetter);
  }

  /**
   * Number of bad try.
   *
   * @return Number of bad try.
   */
  public int getBadTry()
  {
    return this.modele.getBadTry();
  }

  /**
   * Number of good try.
   *
   * @return Number of good try.
   */
  public int getGoodTry()
  {
    return this.modele.getGoodTry();
  }

  /**
   * Number of already tried Tries.
   *
   * @return Number of already Try.
   */
  public int getAlreadyTry()
  {
    return this.modele.getAlreadyTry();
  }

  

}
