﻿/**
 * 
 */
package view.vueGestionEmploye;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
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 destiné à la gestion des employés
 * @author Alfredo Toyo
 */
public class VueGestionAjouterUtilisateur extends Formulaire implements DocumentListener, ItemListener,  ChangeListener{
	private static final long serialVersionUID = 3362945498783166394L;
	
	private JPanel panelGestionEmploye
	  			  ,panelManageFields
	  			  ,panelAreaCommentaire
	  			  ,panelContent;

	private GridBagConstraints constraint;
	
	private Language langue;
	
	private JLabel labelNom
				  ,labelPrenom
				  ,labelCommentaire
				  ,labelNomUtilisateur
			  	  ,labelGroupeUtilisateur
			  	  ,labelActif;
	
	private JValidTextField textfieldNom
	 				  	   ,textfieldPrenom
	 				  	   ,textfieldNomUtilisateur;
	
	private JValidScrollTextArea textAreaCommentaire;
	
	private JComboBox<String> cmboBoxGroupeUtilisateur;
	
	private JCheckBox chkBoxActif;
	
	private static final String AlphaNumericRegex = "[a-zA-Z0-9 -]";
	
	private boolean allowDataChangeListener = false;
	
	/**
	 * Constructeur
	 * @param langue
	 */
	public VueGestionAjouterUtilisateur(Language langue, Message messageDataChanged) {
		super(messageDataChanged);
		this.langue = langue;
		initVariable();
		initListener();
		initComponents();
		setMnemonic();
		allowDataChangeListener = true;
	}
	
	/**
	 * Initialisation des attributs de la classe
	 */
	private void initVariable(){
		panelGestionEmploye      = new JPanel(new GridBagLayout());
		panelManageFields        = new JPanel(new GridBagLayout());
		panelAreaCommentaire     = new JPanel(new GridBagLayout());
		panelContent             = new JPanel(new GridBagLayout());
		
		constraint = new GridBagConstraints();

		labelNom             = new JLabel(langue.getGestionUtilisateur_nomEmploye().replaceAll("%", ""));
		labelPrenom          = new JLabel(langue.getGestionUtilisateur_prenomEmploye().replaceAll("%", ""));
		labelCommentaire     = new JLabel(langue.getGestionUtilisateur_commentaireEmploye().replaceAll("%", ""));

		textfieldNom            = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textfieldPrenom         = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textAreaCommentaire     = JValidScrollTextArea.JValidTextFieldAllowCertainCharacter(4, AlphaNumericRegex);
		
		/*utilisateur*/
		
		constraint = new GridBagConstraints();

		labelNomUtilisateur        = new JLabel(langue.getGestionUtilisateur_NomUtilisateur().replaceAll("%", ""));
		labelGroupeUtilisateur     = new JLabel(langue.getGestionUtilisateur_GroupeUtilisateur().replaceAll("%", ""));
		labelActif                 = new JLabel(langue.getGestionUtilisateur_Actif().replaceAll("%", ""));
		
		textfieldNomUtilisateur     = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		
		cmboBoxGroupeUtilisateur     = new JComboBox<String>();
		cmboBoxGroupeUtilisateur.addItem("");
		
		chkBoxActif     = new JCheckBox();
	}
	
	private void initListener(){
		textfieldNom.getDocument().addDocumentListener(this);
	  	textfieldPrenom.getDocument().addDocumentListener(this);
	  	textfieldNomUtilisateur.getDocument().addDocumentListener(this);
		textAreaCommentaire.getDocument().addDocumentListener(this);
		cmboBoxGroupeUtilisateur.addItemListener(this);
		chkBoxActif.addChangeListener(this);
	}
	
	/**
	 * Initialisation des composants du formulaire
	 */
	private void initComponents(){ 
		/******************CHAMPS EDITABLES****************************/
		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;
		panelManageFields.add(labelNom,constraint);
		
		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   = 1;
		constraint.gridy   = 0;
		panelManageFields.add(textfieldNom,constraint);
		
		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   = 3;
		constraint.gridy   = 0;
		panelManageFields.add(labelPrenom,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 4;
		constraint.gridy   = 0;
		panelManageFields.add(textfieldPrenom,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		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   = 0;
		panelAreaCommentaire.add(labelCommentaire,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		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;
		constraint.gridwidth = 5;
		panelAreaCommentaire.add(textAreaCommentaire,constraint);
		
		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 = 1;
		panelContent.add(panelGestionEmploye,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   = 0;
		panelGestionEmploye.add(panelManageFields,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   = 3;
		constraint.gridwidth = 5;
		panelGestionEmploye.add(panelAreaCommentaire,constraint);
		
		/*Utilisateur*/
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTH;
		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;
		panelManageFields.add(labelNomUtilisateur,constraint);
		
		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   = 1;
		constraint.gridy   = 1;
		panelManageFields.add(textfieldNomUtilisateur,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 3;
		constraint.gridy   = 1;
		panelManageFields.add(labelGroupeUtilisateur,constraint);
		
		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   = 4;
		constraint.gridy   = 1;
		panelManageFields.add(cmboBoxGroupeUtilisateur,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 0.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 2;
		panelManageFields.add(labelActif,constraint);

		constraint.fill    = GridBagConstraints.NONE;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 1;
		constraint.gridy   = 2;
		panelManageFields.add(chkBoxActif,constraint);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		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);
		/******************FIN CHAMPS EDITABLES**************************/
	}
	
	/**
	 * Initialisation des mnémoniques
	 */
	private void setMnemonic(){
		labelNom.setDisplayedMnemonic(langue.getGestionUtilisateur_nomEmploye().charAt(langue.getGestionUtilisateur_nomEmploye().indexOf("%")+1));
		labelNom.setLabelFor(textfieldNom);
		
		labelPrenom.setDisplayedMnemonic(langue.getGestionUtilisateur_prenomEmploye().charAt(langue.getGestionUtilisateur_prenomEmploye().indexOf("%")+1));
		labelPrenom.setLabelFor(textfieldPrenom);
		
		labelCommentaire.setDisplayedMnemonic(langue.getGestionUtilisateur_commentaireEmploye().charAt(langue.getGestionUtilisateur_commentaireEmploye().indexOf("%")+1));
		labelCommentaire.setLabelFor(textAreaCommentaire);
		
		labelNomUtilisateur.setDisplayedMnemonic(langue.getGestionUtilisateur_NomUtilisateur().charAt(langue.getGestionUtilisateur_NomUtilisateur().indexOf("%")+1));
		labelNomUtilisateur.setLabelFor(textfieldNomUtilisateur);
		
		labelGroupeUtilisateur.setDisplayedMnemonic(langue.getGestionUtilisateur_GroupeUtilisateur().charAt(langue.getGestionUtilisateur_GroupeUtilisateur().indexOf("%")+1));
		labelGroupeUtilisateur.setLabelFor(cmboBoxGroupeUtilisateur);

		labelActif.setDisplayedMnemonic(langue.getGestionUtilisateur_Actif().charAt(langue.getGestionUtilisateur_Actif().indexOf("%")+1));
		labelActif.setLabelFor(chkBoxActif);
		chkBoxActif.setMnemonic(langue.getGestionUtilisateur_Actif().charAt(langue.getGestionUtilisateur_Actif().indexOf("%")+1));
	}
	

	/* getter */	
	/**
	 * Retourne la valeur du champ nom
	 * @return text
	 */
	public String getTextFieldNom(){
		return DataNormalizer.normalize(textfieldNom.getText());
	}
	
	/**
	 * Retourne la valeur du champ prénom
	 * @return text
	 */
	public String getTextFieldPrenom(){
		return DataNormalizer.normalize(textfieldPrenom.getText());
	}
	
	/**
	 * Retourne la valeur du champ commentaire
	 * @return text
	 */
	public String getTextAreaCommentaire(){
		return DataNormalizer.normalize(textAreaCommentaire.getText());
	}
	
	/* setter */	
	/**
	 * Affectation d'une valeur pour le champ nom
	 * @param text
	 */
	public void setTextFieldNom(String text){
		allowDataChangeListener = false;
		textfieldNom.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ prénom
	 * @param text
	 */
	public void setTextFieldPrenom(String text){
		allowDataChangeListener = false;
		textfieldPrenom.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ commentaire
	 * @param text
	 */
	public void setTextAreaCommentaire(String text){
		allowDataChangeListener = false;
		textAreaCommentaire.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/*utilisateur*/
	/* getter */	
	/**
	 * Retourne la valeur du champ Nom utlisateur
	 * @return text
	 */
	public String getTextfieldNomUtilisateur(){
		return DataNormalizer.normalize(textfieldNomUtilisateur.getText());
	}
	
	/**
	 * Valide le champ textfieldNomUtilisateur
	 * @return boolean
	 */
	public boolean getTextfieldNomUtilisateurIsValid(){
		return textfieldNomUtilisateur.isValid();
	}
	
	/**
	 * Retourne la valeur de l'item selectionn� dans la liste des utilisateurs
	 * @return text
	 */
	public String getSelectedGroupeUtilisateur(){
		if(cmboBoxGroupeUtilisateur.getItemCount() == 0)
			return "";
		
		return cmboBoxGroupeUtilisateur.getSelectedItem().toString();
	}
	
	/**
	 * Retourne false si on choisit l'item vide
	 * @return boolean
	 */
	public boolean getSelectedGroupeUtilisateurIsValid(){
		return cmboBoxGroupeUtilisateur.getSelectedItem().toString().equals("");
	}
	
	/**
	 * Retourne la valeur bool�enne du champ actif
	 * @return boolean
	 */
	public boolean getChkBoxActif(){
		return chkBoxActif.isSelected();
	}
	
	/* setter */
	/**
	 * Affectation d'une valeur pour le champ nom utilisateur
	 * @param text
	 */
	public void setTextfieldNomUtilisateur(String text){
		allowDataChangeListener = false;
		textfieldNomUtilisateur.setText(DataNormalizer.normalize(text));
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour l'ajout des items dans la liste groupe utilisateur
	 * @param selection
	 */
	public void AddElementGroupeUtilisateur(String selection){
		System.out.println(selection);
		allowDataChangeListener = false;
		cmboBoxGroupeUtilisateur.addItem(selection);
		allowDataChangeListener = true;
	}
	
	public void setSelectedGroupeUtilisateur(String selected){
		allowDataChangeListener = false;
		cmboBoxGroupeUtilisateur.setSelectedItem(selected);
		allowDataChangeListener = true;
	}
	
	/**
	 * Affectation d'une valeur pour le champ actif
	 * @param checked
	 */
	public void setChkBoxActif(boolean checked) {
		allowDataChangeListener = false;
		chkBoxActif.setSelected(checked);
		allowDataChangeListener = true;
	}
	
	/* *** */
		
	/**
	 * Ajout des données dans les champ en lecture seule
	 * @param nom
	 * @param prenom
	 * @param commentaire
	 */
	public void AddDataToFields(String utilisateur, String nom, String prenom, String commentaire, String[] groupe, Boolean actif){
		allowDataChangeListener = false;
		
		textfieldNom.setText(nom);
		textfieldPrenom.setText(prenom);
		textAreaCommentaire.setText(commentaire);
		
		/*utilisateur*/
		textfieldNomUtilisateur.setText(utilisateur);
		cmboBoxGroupeUtilisateur.removeAllItems();
		
		for(int i = 0; i < groupe.length; i++)
			cmboBoxGroupeUtilisateur.addItem(groupe[i]);
		chkBoxActif.setSelected(actif);
		
		allowDataChangeListener = true;
	}
	
	/**
	 * Éfface les informations de l'article en lecture seule
	 */
	public void ClearInfo(){
		allowDataChangeListener = false;
		
		textfieldNom.setText("");
		textfieldPrenom.setText("");
		textAreaCommentaire.setText("");
		
		/*utilisateur*/
		textfieldNomUtilisateur.setText("");
		cmboBoxGroupeUtilisateur.removeAllItems();
		chkBoxActif.setSelected(true);
		
		allowDataChangeListener = true;
	}
	
	public void clearGroupeUtilisateur(){
		allowDataChangeListener = false;
		cmboBoxGroupeUtilisateur.removeAllItems();
		allowDataChangeListener = true;
	}
	
	public boolean isInputDataValid()
	{
		if(textfieldNom.getText().isEmpty() || textfieldPrenom.getText().isEmpty() || textfieldNomUtilisateur.getText().isEmpty())
			return false;
		else
			return true;
				
	}
	
	@Override
	public void setEnabled(boolean enabled)
	{	
		allowDataChangeListener = false;
		
		super.setEnabled(enabled);
		
		textfieldNom.setEditable(enabled);
		textfieldPrenom.setEditable(enabled);
		textAreaCommentaire.setEnabled(enabled);
		
		/*utilisateur*/
		textfieldNomUtilisateur.setEditable(enabled);
		chkBoxActif.setEnabled(enabled);
		
		allowDataChangeListener = 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();
		}
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		try {
			if(allowDataChangeListener && messageDataChanged != null)
				messageDataChanged.trigger(this);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void stateChanged(ChangeEvent arg0) {
		try {
			if(allowDataChangeListener && messageDataChanged != null)
				messageDataChanged.trigger(this);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.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");
		VueGestionAjouterUtilisateur norme = new VueGestionAjouterUtilisateur(Francais.getInstance(),null);
//		norme.AddDataToFields("Toyo", "Alfredo", "mon commentaire");
		//norme.ClearInfo();
		//norme.getObservable().addObserver(new Controleur());
		a.add(norme);
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.pack();
		a.setVisible(true);
	}
}