/**
 * 
 */
package view.vueRechercheUtilisateur;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import language.Francais;
import language.Language;
import tool.regEx.DataNormalizer;
import view.Formulaire;
import view.tool.inputField.JValidTextField;
import view.tool.jEasyTable.JEasyScrollTable;
import controler.Message;

/**
 * Interface graphique destin� � la recherche ds utilisateurs
 * @author Alfredo Toyo
 *
 */
public class VueRechercheUtilisateur extends Formulaire {
	private static final long serialVersionUID = 5416684502946541257L;

	private GridBagConstraints constraint;
	
	private Language langue;
	
	private JPanel panelSearchFields;
	private JScrollPane panelScrollSearch;
	
	private JLabel labelNomUtilisateur
				  ,labelNom
				  ,labelPrenom
    			  ,labelGroupe;
	
	private JValidTextField textFieldNomUtilisateur
						   ,textFieldNom
						   ,textFieldPrenom;
	
	private JComboBox<String> comboBoxGroupe;
		
	private JEasyScrollTable table;
	
	/**
	 * Constructeur
	 * @param langue
	 * @throws Exception
	 */
	public VueRechercheUtilisateur(Language langue, Message messageDataChanged) throws Exception{
		super(messageDataChanged);
		this.langue = langue;
		initVariable();
		initComponents();
		setMnemonic();
	}

	/**
	 * Initialisation des attributs de la classe
	 * @throws Exception 
	 */
	private void initVariable() throws Exception{
		constraint = new GridBagConstraints();
		panelSearchFields = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelScrollSearch = new JScrollPane();

		labelNomUtilisateur = new JLabel(langue.getRechercheUtilisateur_NomUtilisateur().replaceAll("%", ""));
		labelNom 	        = new JLabel(langue.getRechercheUtilisateur_Nom().replaceAll("%", ""));
		labelPrenom	        = new JLabel(langue.getRechercheUtilisateur_Prenom().replaceAll("%", ""));
		labelGroupe         = new JLabel(langue.getRechercheUtilisateur_Groupe().replaceAll("%", ""));		
		
		textFieldNomUtilisateur = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldNom            = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldPrenom         = JValidTextField.JValidTextFieldWithoutText(16);
		
		comboBoxGroupe = new JComboBox<String>();

	    table = new JEasyScrollTable(langue.getRechercheUtilisateurColonne());
		table.setSelectable(JEasyScrollTable.SINGLE_SELECTION);
	}
	
	/**
	 * Initialisation des composants du formulaire
	 */
	private void initComponents(){
		panelScrollSearch.setViewportView(panelSearchFields);
		panelScrollSearch.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		panelScrollSearch.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		panelScrollSearch.setPreferredSize(new Dimension (1, 50));
		panelScrollSearch.setSize(new Dimension (1, 50));
		panelScrollSearch.setMinimumSize(new Dimension (1, 50));
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.insets  = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 0.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		add(panelScrollSearch,constraint);
		
		panelSearchFields.add(labelNomUtilisateur);
		panelSearchFields.add(textFieldNomUtilisateur);
		panelSearchFields.add(labelNom);
		panelSearchFields.add(textFieldNom);
		panelSearchFields.add(labelPrenom);
		panelSearchFields.add(textFieldPrenom);
		panelSearchFields.add(labelGroupe);
		panelSearchFields.add(comboBoxGroupe);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.insets  = new Insets(0, 5, 0, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		add(table,constraint);
	}
	
	/**
	 * Initialisation des mn�moniques
	 */
	private void setMnemonic(){
		labelNomUtilisateur.setDisplayedMnemonic(langue.getRechercheUtilisateur_NomUtilisateur().charAt(langue.getRechercheUtilisateur_NomUtilisateur().indexOf("%")+1));
		labelNomUtilisateur.setLabelFor(textFieldNomUtilisateur);
		labelNom.setDisplayedMnemonic(langue.getRechercheUtilisateur_Nom().charAt(langue.getRechercheUtilisateur_Nom().indexOf("%")+1));
		labelNom.setLabelFor(textFieldNom);
		labelPrenom.setDisplayedMnemonic(langue.getRechercheUtilisateur_Nom().charAt(langue.getRechercheUtilisateur_Nom().indexOf("%")+1));
		labelPrenom.setLabelFor(textFieldPrenom);
		
		labelGroupe.setDisplayedMnemonic(langue.getRechercheUtilisateur_Groupe().charAt(langue.getRechercheUtilisateur_Groupe().indexOf("%")+1));
		labelGroupe.setLabelFor(comboBoxGroupe);
	}

	public void addLineToTableUtilisateur(int utilisateur_p_id, 
										  String utilisateur,
										  String nom,
										  String prenom,
							              String groupe, 
							              Boolean actif,
							              String commentaire){
		try {
			table.addRow(new JLabel[]{new JLabel(Integer.toString(utilisateur_p_id)),
									  new JLabel(utilisateur),
							          new JLabel(nom),
							          new JLabel(prenom),
									  new JLabel(groupe),
									  new JLabel((actif?"oui":"non")),
									  new JLabel(commentaire)});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("JEasyTable.addRow mal utilisé");
		}
	}
	
	public void clear(){
		table.clear();
	}
		
	public int getSelectedUtilisateur(){
		return Integer.valueOf(((JLabel)table.getSelectedRow()[0][0]).getText());
	}

	/**
	* Vider la table
	*/
	public void clearUtilisateur(){
		table.clear();
		comboBoxGroupe.removeAllItems();
	}
	
	/*Getter*/
	/**
	 * Retourne la valeur de l'item selectionne du champ nom utilisateur
	 * @return textFieldNomUtilisateur
	 */
    public String getTextFieldNomUtilisateure(){
      return DataNormalizer.normalize(textFieldNomUtilisateur.getText());
    }
    
	/**
	 * Retourne la valeur du champ nom
	 * @return text
	 */
	public String getTextFieldNom(){
		return DataNormalizer.normalize(textFieldNom.getText());
	}
	
	/**
	 * Retourne la valeur du champ prenom
	 * @return text
	 */
	public String getTextFieldPrenom(){
		return DataNormalizer.normalize(textFieldPrenom.getText());
	}
	
	/**
	 * Retourne la valeur de l'item selectionn� du champ cat�gorie
	 * @return text
	 */
	public String getSelectedGroupe(){
		return comboBoxGroupe.getSelectedItem().toString();
	}

	/*Setter*/
	/**
	 * Affectation d'une valeur pour le champ nom
	 * @param text
	 */
	public void setTextFieldNom(String text){
		textFieldNom.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour l'ajout d'un item dans le champ cat�gorie
	 * @param text
	 */
	public void addGroupe(String selection){
		comboBoxGroupe.addItem(selection);
	}
	
	public boolean hasRowSelected()
	{
		return table.hasRowSelected();
	}
	
	/**
	 * Le code du main est pour les fins de test d'affichage et ils peuvent �tre modifi�s o� effac�s
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		JFrame a = new JFrame("window");

		VueRechercheUtilisateur fer = new VueRechercheUtilisateur(Francais.getInstance(),null);
		a.setLayout(new GridBagLayout());
		
		GridBagConstraints contraint = new GridBagConstraints();
		contraint.fill = GridBagConstraints.BOTH;
		contraint.weightx = 1.0;
		contraint.weighty = 1.0;
		contraint.gridx = 0;
		contraint.gridy = 0;
		
		a.add(fer,contraint);
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.pack();
		a.setVisible(true);
	}
}
