/**
 * 
 */
package view.vueRechercheGroupe;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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;

/**
 * Interface graphique destin� � la recherche des groupes
 * @author Alfredo Toyo
 *
 */
public class VueRechercheGroupe extends Formulaire {
	private static final long serialVersionUID = -5059465316171984552L;

	private GridBagConstraints constraint;
	
	private Language langue;
	
	private JPanel panelSearchFields;
	private JScrollPane panelScrollSearch;
	
	private JLabel labelNomGroupe
    			  	   ,labelNomClient
    			  	   ,labelPrenomClient;
	
	private JValidTextField textFieldNomGroupe
	  				  	   ,textFieldNomClient
	  				  	   ,textFieldPrenomClient;
		
	private JEasyScrollTable table;
	
	/**
	 * Contructeur
	 * @param langue
	 * @throws Exception
	 */
	public VueRechercheGroupe(Language langue) throws Exception{
		super();
		this.langue = langue;
		initVariable();
		initComponents();
		initListener();
		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();

		labelNomGroupe 	  = new JLabel(langue.getRechercheGroupe_NomGroupe().replaceAll("%", ""));
		labelNomClient 	  = new JLabel(langue.getRechercheGroupe_NomClient().replaceAll("%", ""));
		labelPrenomClient = new JLabel(langue.getRechercheGroupe_PrenomClient().replaceAll("%", ""));
		
		textFieldNomGroupe 	  = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldNomClient 	  = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldPrenomClient = JValidTextField.JValidTextFieldWithoutText(16);

	    table = new JEasyScrollTable(langue.getRechercheGroupeColonne());
		table.setSelectable(1);
	}

	/**
	 * 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(5, 3, 5, 3);
		constraint.weightx = 1.0;
		constraint.weighty = 0.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		add(panelScrollSearch,constraint);
		
		panelSearchFields.add(labelNomGroupe);
		panelSearchFields.add(textFieldNomGroupe);
		panelSearchFields.add(labelNomClient);
		panelSearchFields.add(textFieldNomClient);
		panelSearchFields.add(labelPrenomClient);
		panelSearchFields.add(textFieldPrenomClient);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		add(table,constraint);
	}
	
	/**
	 * Initialiser les listener pour les boutons
	 */
	private void initListener(){
	}
	
	/**
	 * Initialisation des mn�moniques
	 */
	private void setMnemonic(){
		labelNomGroupe.setDisplayedMnemonic(langue.getRechercheGroupe_NomGroupe().charAt(langue.getRechercheGroupe_NomGroupe().indexOf("%")+1));
		labelNomGroupe.setLabelFor(textFieldNomGroupe);
		
		labelNomClient.setDisplayedMnemonic(langue.getRechercheGroupe_NomClient().charAt(langue.getRechercheGroupe_NomClient().indexOf("%")+1));
		labelNomClient.setLabelFor(textFieldNomClient);
		
		labelPrenomClient.setDisplayedMnemonic(langue.getRechercheGroupe_PrenomClient().charAt(langue.getRechercheGroupe_PrenomClient().indexOf("%")+1));
		labelPrenomClient.setLabelFor(textFieldPrenomClient);
	}

	public void addLineToTableGroupe(int numero,
										String groupe,
							            String nom, 
							            String prenom){
		try {
		table.addRow(new JLabel[]{new JLabel(""+numero),
								new JLabel(groupe),
								new JLabel(nom),
					            new JLabel(prenom)});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("JEasyTable.addRow mal utilisé");
		}
	}
	
	public int getSelectedGroupe(){
		return Integer.valueOf(((JLabel)table.getSelectedRow()[0][0]).getText());
	}
	
	/**
	* Vider la table
	*/
	public void clearGroupe(){
		table.clear();
	}
	
	/*Getter*/
	/**
	 * Retourne la valeur du champ nom groupe
	 * @return text
	 */
	public String getTextFieldNomGroupe(){
		return DataNormalizer.normalize(textFieldNomGroupe.getText());
	}
	
	/**
	 * Retourne la valeur du champ nom client
	 * @return text
	 */
	public String getTextFieldNomClient(){
		return DataNormalizer.normalize(textFieldNomClient.getText());
	}
	
	/**
	 * Retourne la valeur du champ  pr�nom client
	 * @return text
	 */
	public String getTextFieldPrenomClient(){
		return DataNormalizer.normalize(textFieldPrenomClient.getText());
	}
	
	/*Setter*/
	/**
	 * Affectation d'une valeur pour le champ  nom groupe
	 * @param text
	 */
	public void setTextFieldNomGroupe(String text){
		textFieldNomGroupe.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ nom client
	 * @param text
	 */
	public void setTextFieldNomClient(String text){
		textFieldNomClient.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ pr�nom client
	 * @param text
	 */
	public void setTextFieldPrenomClient(String text){
		textFieldPrenomClient.setText(DataNormalizer.normalize(text));
	}
	
	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");

		VueRechercheGroupe fer = new VueRechercheGroupe(Francais.getInstance());
		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);
	}
}
