package client.control.connexion;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import client.Application;
import client.ApplicationHelper;
import client.model.group.Groupe;
import client.model.user.Utilisateur;
import client.view.admin.gestionGroup.PanelGestionGroupe;
import client.view.admin.gestionUser.PanelCreerUser;
import client.view.connexion.PanelConnexionCreerUser;

/**
 * <u><b>Explication générale de la classe :</b></u>
 * <P>
 * Cette classe permet de gérer l'évènement lié au bouton inscription
 * 
 * @author Dream Team - ING2
 */
public class EcouteurInscription implements ActionListener {

	// Variables permettant de définir les ActionCommand
	public static final String VALIDER = "VALIDER";
	public static final String ANNULER = "ANNULER";

	public EcouteurInscription() {}
	/**
	 * <u><i>Explication générale de la méthode :</i></u> <P> Cette méthode permet
	 * de gérer les évènements liés à la souris
	 * 
	 * @param e
	 *            Evènement souris
	 * 
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(VALIDER)) {
			if (PanelCreerUser.getInstance().getNom().getText().length() > 0
					&& PanelCreerUser.getInstance().getLogin().getText()
							.length() > 0
					&& PanelCreerUser.getInstance().getPassword().getText()
							.length() > 0
					&& (PanelCreerUser.getInstance().getGroupBox()
							.getSelectedIndex() != 0)) {

				String login = PanelCreerUser.getInstance().getLogin()
						.getText();
				String password = PanelCreerUser.getInstance().getPassword()
						.getText();
				String group = PanelCreerUser.getInstance().getGroupBox()
						.getSelectedItem().toString();

				int idUser = Application.getInstance().sizeOfArrayUser();

				Utilisateur user = new Utilisateur(login, password);

				if (!ApplicationHelper.UserExists(user)) {

					// On ajoute le group nous permettant de le récupérer lors
					// de l'acceptation du membre
					user.ajouterGroupe(new Groupe(group));

					Application.getInstance().addUserPending(idUser, user);

					PanelGestionGroupe.getInstance().remplirUserPendingBox();
					PanelGestionGroupe.getInstance().updateUI();

					JOptionPane.showMessageDialog(null,
							"Vous êtes maintenant inscrit",
							"Confirmation d'inscription",
							JOptionPane.INFORMATION_MESSAGE);

					// TODO coup de réseau !
					throw new UnsupportedOperationException(
							"Méthode createUserInConnexion non encore implémenté");
				} else {
					JOptionPane.showMessageDialog(PanelConnexionCreerUser
							.getInstance(), "Cet utilisateur existe déjà",
							"Erreur", JOptionPane.ERROR_MESSAGE);
				}
			} else {
				JOptionPane
						.showMessageDialog(
								null,
								"Vous n'avez pas bien rempli les champs Nom/Login/Password",
								"Erreur", JOptionPane.ERROR_MESSAGE);
			}
		} else if (e.getActionCommand().equals(ANNULER)) {
			int option = JOptionPane.showConfirmDialog(null,
					"Voulez-vous arrêter l'inscription ?",
					"Arrêt de l'Inscription", JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE);

			if (option != JOptionPane.NO_OPTION
					&& option != JOptionPane.CLOSED_OPTION) {

			}
		}
	}
}
