package Controleurs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Modèle.DonneesXML;
import Modèle.Groupe;
import Modèle.Probleme;
import Modèle.Utilisateur;
import Vue.IHMAccueil;

public class EcouteurConnexion implements ActionListener {
	protected JTextField TFLogin;
	protected JPasswordField pf;
	protected JLabel JLErreur;
	protected boolean connect;
	protected Vector<Groupe> groupes;
	protected Vector<Probleme> pb;
	protected DonneesXML DXML;
	protected JFrame jf;

	public EcouteurConnexion(JFrame jf,Vector<Groupe>groupes,Vector<Probleme>pb,DonneesXML DXML,JTextField TFLogin, JPasswordField pf, JLabel JLErreur) {
		this.jf=jf;
		this.TFLogin=TFLogin;
		this.pf = pf;
		this.JLErreur=JLErreur;
		this.groupes = groupes;
		this.pb=pb;
		this.DXML=DXML;
	}

	public void actionPerformed(ActionEvent e) {
		Utilisateur utilCourant=null;
		for (int i = 0; i < groupes.size(); i++) {
			for (int j = 0; j < groupes.get(i).getUtilisateurs().size(); j++) {
				if (groupes.get(i).getUtilisateurs().get(j).getLogin().equals(
						TFLogin.getText()))
					if (groupes.get(i).getUtilisateurs().get(j).getMotDePasse()
							.equals(new String(pf.getPassword()))) {
						connect = true;
						utilCourant = groupes.get(i).getUtilisateurs().get(j);
					}
			}
		}
		if (connect){
			jf.dispose();
			new IHMAccueil(groupes,pb,DXML,utilCourant);
		}
		else
			JLErreur.setText("Login ou mot de passe invalide. Réessayez :");
	}

}
