/**
 * 
 */
package view.vueRechercheFermeture;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.math.BigDecimal;
import java.util.Calendar;

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.dateField.DateField;
import view.tool.inputField.JValidTextField;
import view.tool.jEasyTable.JEasyScrollTable;
import controler.Message;

/**
 * Interface graphique destin� � la recherche des enr�gistrements de fermeture de caisse
 * @author Alfredo Toyo
 *
 */
public class VueRechercheFermeture extends Formulaire {
	private static final long serialVersionUID = 2081175141620599336L;
	
	private GridBagConstraints constraint;
	
	private Language langue;
	
	private JPanel panelSearchFields;
	private JScrollPane panelScrollSearch;
	
	private JLabel labelNumero
    			  	   ,labelDate
    			  	   ,labelNomEmploye
    			  	   ,labelPrenomEmploye
    			  	   ,labelNumeroDepot;
	
	private JValidTextField textFieldNumero
	  				  	   ,textFieldNomEmploye
	  				  	   ,textFieldPrenomEmploye
	  				  	   ,textFieldNumeroDepot;
	
	private DateField dateFieldDate;
	
	private static final String NumericRegex = "[0-9]";
	
	private JEasyScrollTable table;
	
	/**
	 * Constructeur
	 * @param langue
	 * @throws Exception 
	 */
	public VueRechercheFermeture(Language langue, Message messageDataChanged) throws Exception{
		super(messageDataChanged);
		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.getRechercheFermetureColonne());
		table.setSelectable(1);
		
		labelNumero 	   = new JLabel(langue.getRechercheFermeture_Numero().replaceAll("%", ""));
		labelDate          = new JLabel(langue.getRechercheFermeture_Date().replaceAll("%", ""));
		labelNomEmploye	   = new JLabel(langue.getRechercheFermeture_NomEmploye().replaceAll("%", ""));
		labelPrenomEmploye = new JLabel(langue.getRechercheFermeture_PrenomEmploye().replaceAll("%", ""));
		labelNumeroDepot   = new JLabel(langue.getRechercheFermeture_NumeroDepot().replaceAll("%", ""));
		
		textFieldNumero        = JValidTextField.JValidTextFieldAllowCertainCharacter(NumericRegex, 6);
		textFieldNomEmploye    = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldPrenomEmploye = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldNumeroDepot   = JValidTextField.JValidTextFieldAllowCertainCharacter(NumericRegex, 6);
		
		dateFieldDate = DateField.fromCalendar(Calendar.getInstance());
	}
	
	/**
	 * 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(labelNumero);
		panelSearchFields.add(textFieldNumero);
		panelSearchFields.add(labelDate);
		panelSearchFields.add(dateFieldDate);
		panelSearchFields.add(labelNomEmploye);
		panelSearchFields.add(textFieldNomEmploye);
		panelSearchFields.add(labelPrenomEmploye);
		panelSearchFields.add(textFieldPrenomEmploye);
		panelSearchFields.add(labelNumeroDepot);
		panelSearchFields.add(textFieldNumeroDepot);
		
		constraint.fill    = GridBagConstraints.BOTH;
		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);
	}
	
	/**
	 * Initialiser les listener pour les boutons
	 */
	private void initListener(){
//		buttonModifier.addActionListener(this);
//		buttonRecherche.addActionListener(this);
	}
	
	/**
	 * Initialisation des mn�moniques
	 */
	private void setMnemonic(){
		labelNumero.setDisplayedMnemonic(langue.getRechercheFermeture_Numero().charAt(langue.getRechercheFermeture_Numero().indexOf("%")+1));
		labelNumero.setLabelFor(textFieldNumero);
		
		labelDate.setDisplayedMnemonic(langue.getRechercheFermeture_Date().charAt(langue.getRechercheFermeture_Date().indexOf("%")+1));
		labelDate.setLabelFor(dateFieldDate);
		
		labelNomEmploye.setDisplayedMnemonic(langue.getRechercheFermeture_NomEmploye().charAt(langue.getRechercheFermeture_NomEmploye().indexOf("%")+1));
		labelNomEmploye.setLabelFor(textFieldNomEmploye);
		
		labelPrenomEmploye.setDisplayedMnemonic(langue.getRechercheFermeture_PrenomEmploye().charAt(langue.getRechercheFermeture_PrenomEmploye().indexOf("%")+1));
		labelPrenomEmploye.setLabelFor(textFieldPrenomEmploye);
		
		labelNumeroDepot.setDisplayedMnemonic(langue.getRechercheFermeture_NumeroDepot().charAt(langue.getRechercheFermeture_NumeroDepot().indexOf("%")+1));
		labelNumeroDepot.setLabelFor(textFieldNumeroDepot);

	}
	
	public void addLineToTableFermeture(String nom, 
						            int numero_depot,
						            BigDecimal montantDepot,
						            int numero_employe, 
						            String date, 
						            String commentaire){
		try {
		table.addRow(new JLabel[]{new JLabel(""+nom),
									new JLabel(""+numero_depot),
									new JLabel(""+montantDepot),
									new JLabel(""+numero_employe),
						            new JLabel(date),
						            new JLabel(commentaire)});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("JEasyTable.addRow mal utilisé");
		}
	}
	
	public int getSelectedFermeture(){
		return Integer.valueOf(((JLabel)table.getSelectedRow()[0][0]).getText());
	}
	
	/**
	* Vider la table
	*/
	public void clearFermeture(){
		table.clear();
	}
	
	/*Getter*/
	/**
	 * Retourne la valeur du champ num�ro
	 * @return
	 */
	public String getTextFieldNumero(){
		return DataNormalizer.normalize(textFieldNumero.getText());
	}
	
	/**
	 * Retourne la valeur du champ nom employ�
	 * @return
	 */
	public String getTextFieldNomEmploye(){
		return DataNormalizer.normalize(textFieldNomEmploye.getText());
	}
	
	/**
	 * Retourne la valeur du champ pr�nom employ�
	 * @return
	 */
	public String getTextFieldPrenomEmploye(){
		return DataNormalizer.normalize(textFieldPrenomEmploye.getText());
	}
	
	/**
	 * Retourne la valeur du champ num�ro de d�p�t
	 * @return
	 */
	public String getTextFieldNumeroDepot(){
		return DataNormalizer.normalize(textFieldNumeroDepot.getText());
	}
	
	/**
	 * Retourne la valeur du champ date
	 * @return
	 */
	public String getDateFieldDate(){
		return dateFieldDate.getDate().toString();
	}
	
	/*Setter*/
	/**
	 * Affectation d'une valeur pour le champ num�ro
	 * @param text
	 */
	public void setTextFieldNumero(String text){
		textFieldNumero.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ nom employ�
	 * @param text
	 */
	public void setTextFieldNomEmploye(String text){
		textFieldNomEmploye.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ pr�nom employ�
	 * @param text
	 */
	public void setTextFieldPrenomEmploye(String text){
		textFieldPrenomEmploye.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ num�ro de d�p�t
	 * @param text
	 */
	public void setTextFieldNumeroDepot(String text){
		textFieldNumeroDepot.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");

		VueRechercheFermeture fer = new VueRechercheFermeture(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);
	}
}
