import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;


/** 
Cette classe permet d'afficher les noms des joueurs avec la photo de leur
* personnage.
@author Sebastien BISMUTH 
@author Yann LENNE
@version 1.1.6
*/

public class FenJoueurs extends Panel{
  
  /** Panneau pour l'affichage de l'image du pion du joueur 1 */
  private Panel p1;

  /** Panneau pour l'affichage de l'image du pion du joueur 2 */
  private Panel p2;

  /** Panneau pour l'affichage de l'image du pion du joueur 3 */
  private Panel p3;

  /** Panneau pour l'affichage de l'image du pion du joueur 4 */
  private Panel p4;
  private Label num1;
  private Label num2;
  private Label num3;
  private Label num4;

  /** Tableau contenant les images pour le graphique de type Java */
  private Image java[];

  /** Tableau contenant les images pour le graphique de type Femmes */
  private Image femmes[];

  /** Tableau contenant les images pour le graphique de type Femmes */
  private Image simpsons[];

   /** Classe pour le chargement des images du fond d'ecran et des pions */
  private MediaTracker tracker;

  /** Image a afficher */
  private Image image;

  /** Nombre de joueurs */
  private int nbJoueurs;

  /** Instance d'un Puissance4Graphique */
  private Puissance4Graphique pg;

  /** Graphique courant utilise par le client */
  private String graphique;

  public FenJoueurs(int nbJ, Puissance4Graphique pg){

    nbJoueurs = nbJ;
    this.pg = pg;

    // Chargement des images
    chargerImages();

    // Ajout du GridBagLayout
    initLayout();

  }
  
  private void initLayout() {
    
    // Configuration de la mise en page
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(gridbag);
    
    // Label : Joueur 1
    buildConstraints(constraints, 0, 0, 1, 1, 45, 50, "label");
    num1 = new Label("Joueur 1 : ", Label.CENTER);
    gridbag.setConstraints(num1, constraints);
    add(num1);

    // Image pour la photo du joueur 1
    buildConstraints(constraints, 1, 0, 1, 1, 5, 0, "image");
    p1 = new Panel(){
      public void paint(Graphics g){
        graphique = pg.getGraphique(); 
        if (graphique.compareTo("traditionnel") == 0){
          g.setColor(Color.yellow);
          
          g.fillOval(1, 1, getSize().width-2, getSize().height-2);
        }
        else{
          if(graphique.compareTo("java") == 0) {
            if (tracker.statusID(0, true) == MediaTracker.COMPLETE)
              g.drawImage(java[1], 1, 1, getSize().width-1, getSize().height-1, this); 
          }
          else {
            if(graphique.compareTo("femmes") == 0) {
              if (tracker.statusID(1, true) == MediaTracker.COMPLETE)
                g.drawImage(femmes[1], 1, 1, getSize().width-1, getSize().height-1, this); 
            }
            else {
              if(graphique.compareTo("simpsons") == 0) {
                if (tracker.statusID(2, true) == MediaTracker.COMPLETE) {
                  g.drawImage(simpsons[1], 1, 1, getSize().width-1, getSize().height-1, this);
                }
              }
            }
          }
        }


        
      }
    };
    gridbag.setConstraints(p1, constraints);
    add(p1);
   

    // Label : Joueur 2
    buildConstraints(constraints, 0, 1, 1, 1, 0, 50, "label");
    num2 = new Label("Joueur 2 : ", Label.CENTER);
    gridbag.setConstraints(num2, constraints);
    add(num2);
    
    // Image pour la photo du joueur 2
    buildConstraints(constraints, 1, 1, 1, 1, 0, 0, "image");
    p2 = new Panel() {
      public void paint(Graphics g){
        graphique = pg.getGraphique(); 
        if (graphique.compareTo("traditionnel") == 0){
          g.setColor(Color.red);
          
          g.fillOval(1, 1, getSize().width-2, getSize().height-2);
        }
        else{
          if(graphique.compareTo("java") == 0) {
            if (tracker.statusID(0, true) == MediaTracker.COMPLETE)
              g.drawImage(java[2], 1, 1, getSize().width-1, getSize().height-1, this); 
          }
          else {
            if(graphique.compareTo("femmes") == 0) {
              if (tracker.statusID(1, true) == MediaTracker.COMPLETE)
                g.drawImage(femmes[2], 1, 1, getSize().width-1, getSize().height-1, this); 
            }
            else {
              if(graphique.compareTo("simpsons") == 0) {
                if (tracker.statusID(2, true) == MediaTracker.COMPLETE) {
                  g.drawImage(simpsons[2], 1, 1, getSize().width-1, getSize().height-1, this);
                }
              }
            }
          }
        }


        
      }
    };

    gridbag.setConstraints(p2, constraints);
    add(p2);
 
       
    if(nbJoueurs >= 3) {
      // Label : Joueur 3
      buildConstraints(constraints, 2, 0, 1, 1, 45, 50, "label");
      num3 = new Label("Joueur 3 : ", Label.CENTER);
      gridbag.setConstraints(num3, constraints);
      add(num3);
      
      // Image pour la photo du joueur 3
      buildConstraints(constraints, 3, 0, 1, 1, 5, 0, "image");
      p3 = new Panel() {
        public void paint(Graphics g){
          graphique = pg.getGraphique();
          if (graphique.compareTo("traditionnel") == 0){
            g.setColor(Color.white);
            
            g.fillOval(1, 1, getSize().width-2, getSize().height-2);
          }
          else{
            if(graphique.compareTo("java") == 0) {
              if (tracker.statusID(0, true) == MediaTracker.COMPLETE)
                g.drawImage(java[3], 1, 1, getSize().width-1, getSize().height-1, this); 
            }
            else {
              if(graphique.compareTo("femmes") == 0) {
                if (tracker.statusID(1, true) == MediaTracker.COMPLETE)
                  g.drawImage(femmes[3], 1, 1, getSize().width-1, getSize().height-1, this); 
              }
              else {
                if(graphique.compareTo("simpsons") == 0) {
                  if (tracker.statusID(2, true) == MediaTracker.COMPLETE) {
                    g.drawImage(simpsons[3], 1, 1, getSize().width-1, getSize().height-1, this);
                  }
                }
              }
            }
          }
        }
      };
      
      
      gridbag.setConstraints(p3, constraints);
      add(p3);
    }

    if(nbJoueurs == 4) {
      // Label : Joueur 4
      buildConstraints(constraints, 2, 1, 1, 1, 0, 0, "label");
      num4 = new Label("Joueur 4 : ", Label.CENTER);
      gridbag.setConstraints(num4, constraints);
      add(num4);
      
      // Image pour la photo du joueur 4
      buildConstraints(constraints, 3, 1, 1, 1, 0, 0, "image");
      p4 = new Panel() {
        public void paint(Graphics g){
          graphique = pg.getGraphique();
          if (graphique.compareTo("traditionnel") == 0){
            g.setColor(Color.pink);
            
            g.fillOval(1, 1, getSize().width-2, getSize().height-2);
          }
          else{
            if(graphique.compareTo("java") == 0) {
              if (tracker.statusID(0, true) == MediaTracker.COMPLETE)
                g.drawImage(java[4], 1, 1, getSize().width-1, getSize().height-1, this); 
            }
            else {
              if(graphique.compareTo("femmes") == 0) {
                if (tracker.statusID(1, true) == MediaTracker.COMPLETE)
                  g.drawImage(femmes[4], 1, 1, getSize().width-1, getSize().height-1, this); 
              }
              else {
                if(graphique.compareTo("simpsons") == 0) {
                  if (tracker.statusID(2, true) == MediaTracker.COMPLETE) {
                    g.drawImage(simpsons[4], 1, 1, getSize().width-1, getSize().height-1, this);
                  }
                }
              }
            }
          }  
        }
      };


      gridbag.setConstraints(p4, constraints);
      add(p4);
    }
  }

  /** Affiche les noms des joueurs lorsqu'ils sont tous connectes */
  public void afficherNomsJoueurs(){
    Vector nomsJoueurs = pg.renvoyerNomsJoueurs();
    
    String s1 = (String)(nomsJoueurs.elementAt(0));
    String s2 = (String)(nomsJoueurs.elementAt(1));

    num1.setText(num1.getText() + s1);
    num2.setText(num2.getText() + s2);
    num1.setAlignment(Label.CENTER);
    num2.setAlignment(Label.CENTER);

    if(nbJoueurs > 2){
      String s3 = (String)(nomsJoueurs.elementAt(2));
      num3.setText(num3.getText() + s3);
      num3.setAlignment(Label.CENTER);
    }
    if(nbJoueurs == 4){
      String s4 = (String)(nomsJoueurs.elementAt(3));
      num4.setText(num4.getText() + s4);
      num4.setAlignment(Label.CENTER);
    }
  }

  public void paint(Graphics g){
    graphique = pg.getGraphique();
    p1.repaint();
    p2.repaint();
    if (nbJoueurs >= 3)
       p3.repaint();
    if (nbJoueurs == 4)
       p4.repaint();
  }
  
  /** Definition des contraintes d'un composant.
   @param gbc l'objet GridBagConstraints ou sont stockees les contraintes
   @param gx Abscisse de la premiere cellule du composant
   @param gy Ordonnee de la premiere cellule du composant
   @param gw Nombre de colonnes (en cellules) du composant
   @param gw Nombre de rangees (en cellules) du composant
   @param wx Proportion de la largeur totale de la fenetre
   @param wx Proportion de la hauteur totale de la fenetre
  */
  private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh,
                                int wx, int wy, String type){
    // Cellule d'origine
    gbc.gridx = gx;
    gbc.gridy = gy;
    
    // Nombre de cellules occupees
    gbc.gridwidth = gw;
    gbc.gridheight = gh;
    
    // pourcentage d'espace total occupe
    gbc.weightx = wx;
    gbc.weighty = wy;
    
    if (type.compareTo("label") == 0){
      // Placement du composant s'il est plus petit que la zone
      gbc.anchor = GridBagConstraints.CENTER;
    }
    else{
      if (type.compareTo("textfield") == 0){
        gbc.anchor = GridBagConstraints.CENTER;
        // Reduction du champ s'il est plus grand que la zone
        gbc.fill = GridBagConstraints.HORIZONTAL;
      }
      else {
        if(type.compareTo("image") == 0) {
          gbc.fill = GridBagConstraints.BOTH;
        }
      }
    } 
  }

  /** Procedure qui permet de charger les images initialement pour eviter le scintillement du jeu
   */
  public void chargerImages() {
    tracker = new MediaTracker(this);
    java = new Image[6];
    femmes = new Image[6];
    simpsons = new Image[6];
    
    for (int i = 0 ; i <= 5 ; i++) {
      java[i] = Toolkit.getDefaultToolkit().getImage("Images/java" + i + ".gif");
      tracker.addImage(java[i], 0);
    }
    
    for (int i = 0 ; i <= 5 ; i++) {
      femmes[i] = Toolkit.getDefaultToolkit().getImage("Images/femme" + i + ".gif");
      tracker.addImage(femmes[i], 1);
    }

    for (int i = 0 ; i <= 5 ; i++) {
      simpsons[i] = Toolkit.getDefaultToolkit().getImage("Images/simpsons" + i + ".gif");
      tracker.addImage(simpsons[i], 2);
    }

    // Chargement des images java
    try{
      tracker.waitForID(0);
    }
    catch (InterruptedException e){e.printStackTrace();}

    // Chargement des images femmes
    try{
      tracker.waitForID(1);
    }
    catch (InterruptedException e){e.printStackTrace();}

    // Chargement des images simpsons
    try{
      tracker.waitForID(2);
    }
    catch (InterruptedException e){e.printStackTrace();}
  }
  
}


