package view.vueRechercheFacture;

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;

/**
 * Interface graphique destin� � la recherche des factures
 * @author Alfredo Toyo
 *
 */
public class VueRechercheFacture extends Formulaire {
	private static final long serialVersionUID = -4488324620208513302L;
	
	private GridBagConstraints constraint;
	
	private Language langue;
	
	private JPanel panelSearchFields;
	private JScrollPane panelScrollSearch;
	
	private JLabel labelNumero
			  	  ,labelNomClient
			  	  ,labelPrenomClient
			  	  ,labelNumeroFermeture
			  	  ,labelDate;
	
	private JValidTextField textFieldNumero
	  				  	   ,textFieldNomClient
	  				  	   ,textFieldPrenomClient
	  				  	   ,textFieldNumeroFermeture;
	
	private DateField dateFieldDate;
	
	private JEasyScrollTable table;
	private static final String NumericRegex = "[0-9]";
	
	/**
	 * Constructeur
	 * @param langue
	 * @throws Exception 
	 */
	public VueRechercheFacture(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();

		labelNumero 		 = new JLabel(langue.getRechercheFacture_Numero().replaceAll("%", ""));
		labelNomClient 		 = new JLabel(langue.getRechercheFacture_NomClient().replaceAll("%", ""));
		labelPrenomClient	 = new JLabel(langue.getRechercheFacture_PrenomClient().replaceAll("%", ""));
		labelNumeroFermeture = new JLabel(langue.getRechercheFacture_NumeroFermeture().replaceAll("%", ""));
		labelDate 			 = new JLabel(langue.getRechercheFacture_Date().replaceAll("%", ""));
		
		textFieldNumero 		 = JValidTextField.JValidTextFieldAllowCertainCharacter(NumericRegex, 6);
		textFieldNomClient 	     = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldPrenomClient    = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldNumeroFermeture = JValidTextField.JValidTextFieldAllowCertainCharacter(NumericRegex, 6);
		
		dateFieldDate = DateField.fromCalendar(Calendar.getInstance());

	    table = new JEasyScrollTable(langue.getRechercheFactureColonne());
		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.anchor  = GridBagConstraints.NORTH;
		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(labelNumero);
//		panelSearchFields.add(textFieldNumero);
		panelSearchFields.add(labelNomClient);
		panelSearchFields.add(textFieldNomClient);
		panelSearchFields.add(labelPrenomClient);
		panelSearchFields.add(textFieldPrenomClient);
		panelSearchFields.add(labelNumeroFermeture);
		panelSearchFields.add(textFieldNumeroFermeture);
		panelSearchFields.add(labelDate);
		panelSearchFields.add(dateFieldDate);
		
		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(){
//		buttonModifier.addActionListener(this);
//		buttonRecherche.addActionListener(this);
	}
	
	/**
	 * Initialisation des mn�moniques
	 */
	private void setMnemonic(){
		labelNumero.setDisplayedMnemonic(langue.getRechercheFacture_Numero().charAt(langue.getRechercheFacture_Numero().indexOf("%")+1));
		labelNumero.setLabelFor(textFieldNumero);
		
		labelNomClient.setDisplayedMnemonic(langue.getRechercheFacture_NomClient().charAt(langue.getRechercheFacture_NomClient().indexOf("%")+1));
		labelNomClient.setLabelFor(textFieldNomClient);
		
		labelPrenomClient.setDisplayedMnemonic(langue.getRechercheFacture_PrenomClient().charAt(langue.getRechercheFacture_PrenomClient().indexOf("%")+1));
		labelPrenomClient.setLabelFor(textFieldPrenomClient);
		
		labelNumeroFermeture.setDisplayedMnemonic(langue.getRechercheFacture_NumeroFermeture().charAt(langue.getRechercheFacture_NumeroFermeture().indexOf("%")+1));
		labelNumeroFermeture.setLabelFor(textFieldNumeroFermeture);
		
		labelDate.setDisplayedMnemonic(langue.getRechercheFacture_Date().charAt(langue.getRechercheFacture_Date().indexOf("%")+1));
		labelDate.setLabelFor(dateFieldDate);
		
//		buttonRecherche.setMnemonic(langue.getRechercheFacture_Recherche().charAt(langue.getRechercheFacture_Recherche().indexOf("%")+1));
//		buttonModifier.setMnemonic(langue.getRechercheFacture_Modifier().charAt(langue.getRechercheFacture_Modifier().indexOf("%")+1));
	}
	
	public void addLineToTableFacture(int numero, 
						            int numero_fermeture, 
						            String nom_client,
						            String prenom_client,
						            String date, 
						            BigDecimal depense,
						            String commentaire){
		try {
		table.addRow(new JLabel[]{new JLabel(""+numero),
									new JLabel(""+numero_fermeture),
						            new JLabel(nom_client),
						            new JLabel(prenom_client),
						            new JLabel(date),
						            new JLabel(depense.toString()),
						            new JLabel(commentaire)});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("JEasyTable.addRow mal utilisé");
		}
	}
	
	public int getSelectedFacture(){
		return Integer.valueOf(((JLabel)table.getSelectedRow()[0][0]).getText());
	}
	
	/**
	* Vider la table
	*/
	public void clearFacture(){
		table.clear();
	}
	
	/*Getter*/
	/**
	 * Retourne la valeur du champ num�ro
	 * @return text
	 */
	public String getTextFieldNumero(){
		return DataNormalizer.normalize(textFieldNumero.getText());
	}
	
	/**
	 * Retourne la valeur du champ nom client
	 * @return
	 */
	public String getTextFieldNomClient(){
		return DataNormalizer.normalize(textFieldNomClient.getText());
	}
	
	/**
	 * Retourne la valeur du champ pr�nom client
	 * @return
	 */
	public String getTextFieldPrenomClient(){
		return DataNormalizer.normalize(textFieldPrenomClient.getText());
	}
	
	/**
	 * Retourne la valeur du champ num�ro fermeture
	 * @return
	 */
	public String getTextFieldNumeroFermeture(){
		return DataNormalizer.normalize(textFieldNumeroFermeture.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 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));
	}
	
	/**
	 * Affectation d'une valeur pour le champ num�ro fermeture
	 * @param text
	 */
	public void setTextFieldNumeroFermeture(String text){
		textFieldNumeroFermeture.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");

		VueRechercheFacture fer = new VueRechercheFacture(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);
	}
}
