﻿package view.vueRechercheArticle;

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 des articles
 * @author Alfredo Toyo
 *
 */
public class VueRechercheArticle extends Formulaire {
	private static final long serialVersionUID = -5456709662163680348L;
	
	private JPanel panelSearchFields;
	private JScrollPane panelScrollSearch;
	
	private JEasyScrollTable table;
	
	private GridBagConstraints constraint;
	
	private Language lang;
	
	private JLabel labelCode
			  	  ,labelDescription
			  	  ,labelTaxable
			      ,labelActif
			      ,labelNomCategorie;
	
	private JValidTextField textFieldCode
	  				  	   ,textFieldDescription
	  				  	   ,textFieldNomCategorie;
	
	private JComboBox<String> comboBoxTaxable
	  				 		 ,comboBoxActif;
	
	/**
	 * Constructeur
	 * @param lang
	 * @throws Exception 
	 */
	public VueRechercheArticle(Language lang, Message messageDataChanged) throws Exception{
		super(messageDataChanged);
		this.lang = lang;
		initVariable();
		initComponents();
		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(lang.getRechercheArticleColonne());
		table.setSelectable(JEasyScrollTable.SINGLE_SELECTION);
		
		labelCode 		  = new JLabel(lang.getRechercheArticle_Code().replaceAll("%", ""));
		labelDescription  = new JLabel(lang.getRechercheArticle_Description().replaceAll("%", ""));
		labelTaxable	  = new JLabel(lang.getRechercheArticle_Taxable().replaceAll("%", ""));
		labelActif		  = new JLabel(lang.getRechercheArticle_Actif().replaceAll("%", ""));
		labelNomCategorie = new JLabel(lang.getRechercheArticle_NomCategorie().replaceAll("%", ""));
		
		textFieldCode 		  = JValidTextField.JValidTextFieldWithoutText(6);
		textFieldDescription  = JValidTextField.JValidTextFieldWithoutText(16);
		textFieldNomCategorie = JValidTextField.JValidTextFieldWithoutText(16);
		
		comboBoxTaxable = new JComboBox<String>();
		comboBoxTaxable.addItem("");
		comboBoxTaxable.addItem(lang.getAnswerOui());
		comboBoxTaxable.addItem(lang.getAnswerNon());
		comboBoxActif   = new JComboBox<String>();
		comboBoxActif.addItem("");
		comboBoxActif.addItem(lang.getAnswerOui());
		comboBoxActif.addItem(lang.getAnswerNon());
		
		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));
	}
	
	/**
	 * Initialisation des composants du formulaire
	 */
	private void initComponents(){
		panelSearchFields.add(labelCode);
		panelSearchFields.add(textFieldCode);
		panelSearchFields.add(labelDescription);
		panelSearchFields.add(textFieldDescription);
		panelSearchFields.add(labelTaxable);
		panelSearchFields.add(comboBoxTaxable);
		panelSearchFields.add(labelActif);
		panelSearchFields.add(comboBoxActif);
		panelSearchFields.add(labelNomCategorie);
		panelSearchFields.add(textFieldNomCategorie);
		
		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);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.insets  = new Insets(0, 5, 0, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 3.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		add(table,constraint);
	}
	
	/**
	 * Initialisation des mnémoniques
	 */	
	private void setMnemonic(){
		labelCode.setDisplayedMnemonic(lang.getRechercheArticle_Code().charAt(lang.getRechercheArticle_Code().indexOf("%")+1));
		labelCode.setLabelFor(textFieldCode);
		
		labelDescription.setDisplayedMnemonic(lang.getRechercheArticle_Description().charAt(lang.getRechercheArticle_Description().indexOf("%")+1));
		labelDescription.setLabelFor(textFieldDescription);
		
		labelTaxable.setDisplayedMnemonic(lang.getRechercheArticle_Taxable().charAt(lang.getRechercheArticle_Taxable().indexOf("%")+1));
		labelTaxable.setLabelFor(comboBoxTaxable);
		
		labelActif.setDisplayedMnemonic(lang.getRechercheArticle_Actif().charAt(lang.getRechercheArticle_Actif().indexOf("%")+1));
		labelActif.setLabelFor(comboBoxActif);
		
		labelNomCategorie.setDisplayedMnemonic(lang.getRechercheArticle_NomCategorie().charAt(lang.getRechercheArticle_NomCategorie().indexOf("%")+1));
		labelNomCategorie.setLabelFor(textFieldNomCategorie);
	}
	
	
	public void addLineToTableArticle(int numero, 
			                   String code, 
			                   String description, 
			                   boolean taxable, 
			                   boolean actif, 
			                   String nomCategorie){
		try {
			table.addRow(new JLabel[]{new JLabel(""+numero), 
					                  new JLabel(code),
					                  new JLabel(description),
					                  new JLabel(taxable?lang.getAnswerOui():lang.getAnswerNon()),
					                  new JLabel(actif?lang.getAnswerOui():lang.getAnswerNon()),
					                  new JLabel(nomCategorie)});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("JEasyTable.addRow mal utilisé");
		}
	}
	
	public int getSelectedArticle(){
		return Integer.valueOf(((JLabel)table.getSelectedRow()[0][0]).getText());
	}
	
	/**
	 * 
	 */
	public void clearTableArticle(){
		table.clear();
	}
	
    /* getter */	
	/**
	 * Retourne la valeur du champ code
	 * @return text
	 */
	public String getTextFieldCode(){
		return DataNormalizer.normalize(textFieldCode.getText());
	}
	
	/**
	 * Retourne la valeur du champ description
	 * @return text
	 */
	public String getTextFieldDescription(){
		return DataNormalizer.normalize(textFieldDescription.getText());
	}
	
	/**
	 * Retourne la valeur du champ nom categorie
	 * @return text
	 */
	public String getTextFieldNomCategorie(){
		return DataNormalizer.normalize(textFieldNomCategorie.getText());
	}
	
	/**
	 * Retourne la valeur de l'item selectionné du champ taxable
	 * @return
	 */
	public String getSelectedTaxable() {
		if (comboBoxTaxable.getSelectedItem().toString().equals(lang.getAnswerOui()))
			return "V";
		if (comboBoxTaxable.getSelectedItem().toString().equals(lang.getAnswerNon()))
			return "F";
		return "";
	}

	/**
	 * Retourne la valeur de l'item selectionné du champ actif
	 * @return
	 */
	public String getSelectedActif() {
		if (comboBoxActif.getSelectedItem().toString().equals(lang.getAnswerOui()))
			return "V";
		if (comboBoxActif.getSelectedItem().toString().equals(lang.getAnswerNon()))
			return "F";
		return "";
	}
	
	/* setter */	
	/**
	 * Affectation d'une valeur pour le champ code
	 * @param text
	 */
	public void setTextFieldCode(String text){
		textFieldCode.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ description
	 * @param text
	 */
	public void setTextFieldDescription(String text){
		textFieldDescription.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ nom categorie
	 * @param text
	 */
	public void setTextFieldNomCategorie(String text){
		textFieldNomCategorie.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur dans le champ taxable
	 * @param selection
	 */
	public void setSelectedTaxable(String selection) {
		comboBoxTaxable.setSelectedItem(selection);
	}
	
	/**
	 * Affectation d'une valeur dans le champ actif
	 * @param selection
	 */
	public void setSelectedActif(String selection) {
		comboBoxActif.setSelectedItem(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");

		VueRechercheArticle fer = new VueRechercheArticle(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.setSize(a.getToolkit().getScreenSize());
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.setExtendedState(JFrame.MAXIMIZED_BOTH);
		a.pack();
		a.setVisible(true);
	}
}
