/**
 * 
 */
package view.vueGestionConstante;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import language.Francais;
import language.Language;
import tool.regEx.DataNormalizer;
import view.Formulaire;
import view.tool.inputField.JValidScrollTextArea;
import view.tool.inputField.JValidTextField;
import controler.Message;

/**
 * Interface graphique destinee a la gestion des constantes
 * @author Alfredo Toyo
 *
 */
public class VueGestionConstante extends Formulaire implements DocumentListener{
	private static final long serialVersionUID = 5733069329747238954L;
	
	private JPanel panelGestionConstante
	              ,panelManageFields
	              ,panelAreaMessageFacture
	              ,panelInfoGestionConstante
	              ,panelInfoManageFields
	              ,panelInfoAreaMessageFacture
	              ,panelContent;
	
	private JSeparator separator;

	private GridBagConstraints constraint;
	
	private Language langue;

	private JLabel labelNumeroTPS
			  	  ,labelNumeroTVQ
			  	  ,labelNeq
			  	  ,labelInfoNumeroTPS
			  	  ,labelInfoNumeroTVQ
			  	  ,labelInfoNeq
				  ,labelMessageFacture
				  ,labelInfoMessageFacture;
	
	private JValidTextField textFieldNumeroTPS
					  	   ,textFieldNumeroTVQ
					  	   ,textFieldNeq
					  	   ,textFieldInfoNumeroTPS
					  	   ,textFieldInfoNumeroTVQ
					  	   ,textFieldInfoNeq;
	
	private JValidScrollTextArea textAreaMessageFacture
	                 			,textAreaInfoMessageFacture;
	
	private static final String AlphaNumericRegex = "[a-zA-Z0-9 -]";
	
	private boolean allowDataChangeListener = true;

	/**
	 * Constructeur
	 * @param langue
	 */
	public VueGestionConstante(Language langue, Message messageDataChanged) {
		super(messageDataChanged);
		this.langue = langue;
		initVariable();
		initComponents();
		initListener();
		setMnemonic();
	}
	
	/**
	 * Initialisation des attributs de la classe
	 */
	private void initVariable()
	{
		panelGestionConstante       = new JPanel(new GridBagLayout());
		panelManageFields		    = new JPanel(new GridBagLayout());
		panelAreaMessageFacture     = new JPanel(new GridBagLayout());
		panelInfoGestionConstante   = new JPanel(new GridBagLayout());
		panelInfoManageFields		= new JPanel(new GridBagLayout());
		panelInfoAreaMessageFacture = new JPanel(new GridBagLayout());
		panelContent                = new JPanel(new GridBagLayout());
		
		separator = new JSeparator();
		
		constraint = new GridBagConstraints();

		labelNumeroTPS          = new JLabel(langue.getGestionConstante_NumeroTPS().replaceAll("%", ""));
		labelNumeroTVQ          = new JLabel(langue.getGestionConstante_NumeroTVQ().replaceAll("%", ""));
		labelNeq                = new JLabel(langue.getGestionConstante_Neq().replaceAll("%", ""));
		labelMessageFacture     = new JLabel(langue.getGestionConstante_MessageFacture().replaceAll("%", ""));
		labelInfoNumeroTPS      = new JLabel(langue.getGestionConstante_NumeroTPS().replaceAll("%", ""));
		labelInfoNumeroTVQ      = new JLabel(langue.getGestionConstante_NumeroTVQ().replaceAll("%", ""));
		labelInfoNeq            = new JLabel(langue.getGestionConstante_Neq().replaceAll("%", ""));
		labelInfoMessageFacture = new JLabel(langue.getGestionConstante_MessageFacture().replaceAll("%", ""));

		textFieldNumeroTPS         = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textFieldNumeroTVQ         = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textFieldNeq               = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textAreaMessageFacture     = JValidScrollTextArea.JValidTextFieldWithoutText(4);
		textFieldInfoNumeroTPS     = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textFieldInfoNumeroTVQ     = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textFieldInfoNeq           = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textAreaInfoMessageFacture = JValidScrollTextArea.JValidTextFieldWithoutText(4);
	}
	
	private void initListener(){
		textFieldNumeroTPS.getDocument().addDocumentListener(this);
	  	textFieldNumeroTVQ.getDocument().addDocumentListener(this);
	  	textFieldNeq.getDocument().addDocumentListener(this);
	  	textAreaMessageFacture.getDocument().addDocumentListener(this);
	}

	/**
	 * Initialisation des composants du formulaire
	 */
	private void initComponents(){
		/******************CHAMPS EDITABLES****************************/
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		panelManageFields.add(labelNumeroTPS,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.WEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 1;
		constraint.gridy   = 0;
		panelManageFields.add(textFieldNumeroTPS,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		panelManageFields.add(labelNumeroTVQ,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.WEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 1;
		constraint.gridy   = 1;
		panelManageFields.add(textFieldNumeroTVQ,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 2;
		panelManageFields.add(labelNeq,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.WEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 1;
		constraint.gridy   = 2;
		panelManageFields.add(textFieldNeq,constraint);

		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		panelAreaMessageFacture.add(labelMessageFacture,constraint);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 0.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		panelAreaMessageFacture.add(textAreaMessageFacture,constraint);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.insets  = new Insets(0, 0, 0, 0);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		panelContent.add(panelGestionConstante,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets  = new Insets(0, 0, 0, 0);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		panelGestionConstante.add(panelManageFields,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets  = new Insets(0, 0, 0, 0);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 2;
		panelGestionConstante.add(panelAreaMessageFacture,constraint);
		/******************FIN CHAMPS EDITABLES**************************/
		
		/******************CHAMPS NON EDITABLES**************************/		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		constraint.gridwidth = 10;
		panelInfoGestionConstante.add(separator,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.WEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		constraint.gridwidth = 1;
		panelInfoManageFields.add(labelInfoNumeroTPS,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.WEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 1;
		constraint.gridy   = 1;
		panelInfoManageFields.add(textFieldInfoNumeroTPS,constraint);
		textFieldInfoNumeroTPS.setEditable(false);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 2;
		panelInfoManageFields.add(labelInfoNumeroTVQ,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.WEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 1;
		constraint.gridy   = 2;
		panelInfoManageFields.add(textFieldInfoNumeroTVQ,constraint);
		textFieldInfoNumeroTVQ.setEditable(false);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 3;
		panelInfoManageFields.add(labelInfoNeq,constraint);
		
		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.WEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 1;
		constraint.gridy   = 3;
		panelInfoManageFields.add(textFieldInfoNeq,constraint);
		textFieldInfoNeq.setEditable(false);

		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		panelInfoAreaMessageFacture.add(labelInfoMessageFacture,constraint);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		panelInfoAreaMessageFacture.add(textAreaInfoMessageFacture,constraint);
		textAreaInfoMessageFacture.setEnabled(false);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets  = new Insets(0, 0, 0, 0);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		panelContent.add(panelInfoGestionConstante,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets  = new Insets(0, 0, 0, 0);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		panelInfoGestionConstante.add(panelInfoManageFields,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.insets  = new Insets(0, 0, 0, 0);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 2;
		panelInfoGestionConstante.add(panelInfoAreaMessageFacture,constraint);
		/******************FIN CHAMPS NON EDITABLES**********************/
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.insets = new Insets(0, 0, 0, 0);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		add(panelContent,constraint);
	}

	/**
	 * Initialisation des mn�moniques
	 */	
	private void setMnemonic(){
		labelNumeroTPS.setDisplayedMnemonic(langue.getGestionConstante_NumeroTPS().charAt(langue.getGestionConstante_NumeroTPS().indexOf("%")+1));
		labelNumeroTPS.setLabelFor(textFieldNumeroTPS);
		
		labelNumeroTVQ.setDisplayedMnemonic(langue.getGestionConstante_NumeroTVQ().charAt(langue.getGestionConstante_NumeroTVQ().indexOf("%")+1));
		labelNumeroTVQ.setLabelFor(textFieldNumeroTVQ);
		
		labelNeq.setDisplayedMnemonic(langue.getGestionConstante_Neq().charAt(langue.getGestionConstante_Neq().indexOf("%")+1));
		labelNeq.setLabelFor(textFieldNeq);
		
		labelMessageFacture.setDisplayedMnemonic(langue.getGestionConstante_MessageFacture().charAt(langue.getGestionConstante_MessageFacture().indexOf("%")+1));
		labelMessageFacture.setLabelFor(textAreaMessageFacture);
		
	}
	
    /* getter */
	/**
	 * Retourne la valeur du champ num�ro de TPS
	 * @return text
	 */
	public String getTextFieldNumeroTPS(){
		return DataNormalizer.normalize(textFieldNumeroTPS.getText());
	}
	
	/**
	 * Retourne la valeur du champ num�ro de TVQ
	 * @return text
	 */
	public String getTextFieldNumeroTVQ(){
		return DataNormalizer.normalize(textFieldNumeroTVQ.getText());
	}

	/**
	 * Retourne la valeur du champ du Neq
	 * @return text
	 */
	public String getTextFieldNeq(){
		return DataNormalizer.normalize(textFieldNeq.getText());
	}
	
	/**
	 * Retourne la valeur du champ message facture
	 * @return text
	 */
	public String getTextAreaMessageFacture(){
		return DataNormalizer.normalize(textAreaMessageFacture.getText());
	}
	
	/* setter */
	/**
	 * Affectation d'une valeur pour le champ num�ro de TPS
	 * @param text
	 */
	public void setTextFieldNumeroTPS(String text){
		allowDataChangeListener = false;
		textFieldNumeroTPS.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ num�ro de TVQ
	 * @param text
	 */
	public void setTextFieldNumeroTVQ(String text){
		allowDataChangeListener = false;
		textFieldNumeroTVQ.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ Neq
	 * @param text
	 */
	public void setTextFieldNeq(String text){
		allowDataChangeListener = false;
		textFieldNeq.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ message facture 
	 * @param text
	 */
	public void setTextAreaMessageFacture(String text) {
		allowDataChangeListener = false;
		textAreaMessageFacture.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/* setters on read only fields*/
	/**
	 * Affectation d'une valeur pour le champ num�ro de TPS
	 * @param text
	 */
	public void setTextFieldInfoNumeroTPS(String text){
		allowDataChangeListener = false;
		textFieldInfoNumeroTPS.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ num�ro de TVQ
	 * @param text
	 */
	public void setTextFieldInfoNumeroTVQ(String text){
		allowDataChangeListener = false;
		textFieldInfoNumeroTVQ.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ Neq
	 * @param text
	 */
	public void setTextFieldInfoNeq(String text){
		allowDataChangeListener = false;
		textFieldInfoNeq.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ message facture 
	 * @param text
	 */
	public void setTextAreaInfoMessageFacture(String text) {
		allowDataChangeListener = false;
		textAreaInfoMessageFacture.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Ajout des donn�es dans les champ en lecture seule
	 * @param tps
	 * @param tvq
	 * @param neq
	 * @param message
	 */
	public void AddDataToFields(String tps, String tvq, String neq, String message){
		allowDataChangeListener = false;
		
		setTextFieldNumeroTPS(tps);
		setTextFieldNumeroTVQ(tvq);
		setTextFieldNeq(neq);
		setTextAreaMessageFacture(message);
		setTextFieldInfoNumeroTPS(tps);
		setTextFieldInfoNumeroTVQ(tvq);
		setTextFieldInfoNeq(neq);
		setTextAreaInfoMessageFacture(message);
		
		allowDataChangeListener = true;
	}
	
	/**
	 * �fface les informations de l'article en lecture seule
	 */
	public void ClearInfo(){
		allowDataChangeListener = false;
		
		setTextFieldNumeroTPS("");
		setTextFieldNumeroTVQ("");
		setTextFieldNeq("");
		setTextAreaMessageFacture("");
		setTextFieldInfoNumeroTPS("");
		setTextFieldInfoNumeroTVQ("");
		setTextFieldInfoNeq("");
		setTextAreaInfoMessageFacture("");
		
		allowDataChangeListener = true;
	}

	public boolean isInputDataValid()
	{
		if(!textFieldNumeroTPS.isValid() || !textFieldNumeroTVQ.isValid() || !textFieldNeq.isValid())
			return false;
		
		return true;
	}
	
	@Override
	public void changedUpdate(DocumentEvent e) {
		try {
			if(allowDataChangeListener && messageDataChanged != null)
				messageDataChanged.trigger(this);
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		try {
			if(allowDataChangeListener && messageDataChanged != null)
				messageDataChanged.trigger(this);
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		try {
			if(allowDataChangeListener && messageDataChanged != null)
				messageDataChanged.trigger(this);
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	/**
	 * Le code du main est pour les fins de test d'affichage et ils peuvent �tre modifi�s o� effac�s
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame a = new JFrame("window");
		VueGestionConstante fer = new VueGestionConstante(Francais.getInstance(),null);
		fer.AddDataToFields("a", "b", "c", "d");
		a.setLayout(new GridBagLayout());
		
		GridBagConstraints contraint = new GridBagConstraints();
		contraint.fill = GridBagConstraints.NONE;
		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);
	}
}
