/**
 * 
 */
package view.vueRechercheVille;

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 villes
 * @author Alfredo Toyo
 *
 */
public class VueRechercheVille extends Formulaire {
	private static final long serialVersionUID = -6743495743655698068L;
	
	private JPanel panelSearchFields;
	private JScrollPane panelScrollSearch;
	
	private Language langue;
	
	private JEasyScrollTable table;
	
	private GridBagConstraints constraint;
	
	private JLabel labelVille
	              ,labelProvince
	              ,labelPays;
	
	private JValidTextField textFieldVille
					  	   ,textFieldProvince
					  	   ,textFieldPays;
	
	private static final String AlphaNumericRegex = "[a-zA-Z 0-9]";
	
	/**
	 * Constructeur
	 * @param langue
	 * @throws Exception 
	 */
	public VueRechercheVille(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{
		panelSearchFields   = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelScrollSearch     = new JScrollPane();
		
		constraint = new GridBagConstraints();
		
	    table = new JEasyScrollTable(langue.getRechercheVilleColonne());
		
		table.setMaximumSize(new Dimension(400,800));
		table.setMinimumSize(new Dimension(400,800));
		table.setSize(new Dimension(400,800));
		table.setSelectable(1);
		
		labelVille    = new JLabel(langue.getRechercheVille_NomVille().replaceAll("%", ""));
		labelProvince = new JLabel(langue.getRechercheVille_NomProvince().replaceAll("%", ""));
		labelPays     = new JLabel(langue.getRechercheVille_NomPays().replaceAll("%", ""));
		
		textFieldVille    = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textFieldProvince = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textFieldPays     = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
	}
	
	/**
	 * 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(labelVille);
		panelSearchFields.add(textFieldVille);
		panelSearchFields.add(labelProvince);
		panelSearchFields.add(textFieldProvince);
		panelSearchFields.add(labelPays);
		panelSearchFields.add(textFieldPays);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.anchor  = GridBagConstraints.CENTER;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		add(table,constraint);
	}
	
	/**
	 * Initialiser les listener pour le bouton recherche
	 */
	private void initListener(){
//		buttonModifier.addActionListener(this);
//		buttonRecherche.addActionListener(this);
	}
	
	/**
	 * Initialisation des mn�moniques
	 */
	private void setMnemonic(){
		labelVille.setDisplayedMnemonic(langue.getRechercheVille_NomVille().charAt(langue.getRechercheVille_NomVille().indexOf("%")+1));
		labelVille.setLabelFor(textFieldVille);
		
		labelProvince.setDisplayedMnemonic(langue.getRechercheVille_NomProvince().charAt(langue.getRechercheVille_NomProvince().indexOf("%")+1));
		labelProvince.setLabelFor(textFieldProvince);
		
		labelPays.setDisplayedMnemonic(langue.getRechercheVille_NomPays().charAt(langue.getRechercheVille_NomPays().indexOf("%")+1));
		labelPays.setLabelFor(textFieldPays);
		
//		buttonModifier.setMnemonic(langue.getRechercheVille_Modifier().charAt(langue.getRechercheVille_Modifier().indexOf("%")+1));
//		buttonRecherche.setMnemonic(langue.getRechercheVille_Recherche().charAt(langue.getRechercheVille_Recherche().indexOf("%")+1));
	}
	
	public void addLineToTableVille(int numero,
									String ville, 
									String province, 
									String pays){
		try {
			table.addRow(new JLabel[]{new JLabel(""+numero),
									  new JLabel(ville),
					                  new JLabel(province),
					                  new JLabel(pays)});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("JEasyTable.addRow mal utilis�");
		}
	}
	
	public void clear(){
		table.clear();
	}
		
	public int getSelectedVille(){
		return Integer.valueOf(((JLabel)table.getSelectedRow()[0][0]).getText());
	}
		
		/**
		* 
		*/
		public void clearTableArticle(){
		table.clear();
	}

	/*getter*/
	/**
	 * Retourne la valeur du champ ville
	 * @return textFieldVille
	 */
	public String getTextFieldVille(){
		return DataNormalizer.normalize(textFieldVille.getText());
	}
	
	/**
	 * Retourne la valeur du champ province
	 * @return textFieldProvince
	 */
	public String getTextFieldProvince(){
		return DataNormalizer.normalize(textFieldProvince.getText());
	}
	
	/**
	 * Retourne la valeur du champ pays
	 * @return
	 */
	public String getTextFieldPays(){
		return DataNormalizer.normalize(textFieldPays.getText());
	}
	/* *** */
	
	/*setter*/
	/**
	 * Affectation d'une valeur pour le champ ville
	 * @param text
	 */
	public void setTextFieldVille(String text){
		textFieldVille.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ province
	 * @param text
	 */
	public void setTextFieldProvince(String text){
		textFieldProvince.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ pays
	 * @param text
	 */
	public void setTextFieldPays(String text){
		textFieldPays.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");

		VueRechercheVille fer = new VueRechercheVille(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.setSize(a.getToolkit().getScreenSize());
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.setExtendedState(JFrame.MAXIMIZED_BOTH);
		a.pack();
		a.setVisible(true);
	}
}
