/**
 * 
 */
package view.vueGestionEmploye;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import language.Francais;
import language.Language;
import tool.regEx.DataNormalizer;
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 VueGestionModifierUtilisateur extends VueGestionAjouterUtilisateur{
	private static final long serialVersionUID = 3362945498783166394L;
	
	private int utilisateurId;
	private int employeId;

	private JPanel panelInfoGestionEmploye
	  			  ,panelInfoManageFields
	  			  ,panelInfoAreaCommentaire;
	
	private JSeparator separator;

	private GridBagConstraints constraint;
	
	private Language langue;
	
	private JLabel labelInfoNom
				  ,labelInfoPrenom
				  ,labelInfoCommentaire
				  ,labelInfoNomUtilisateur
			  	  ,labelInfoGroupeUtilisateur
			  	  ,labelInfoActif;
	
	private JValidTextField textfieldInfoNom
	 				  	   ,textfieldInfoPrenom
	 				  	   ,textfieldUtilisateur
	 				  	   ,textfieldInfoNomUtilisateur
	 				  	   ,textfieldInfoGroupeUtilisateur;
	
	private JValidScrollTextArea textAreaInfoCommentaire;
	
	private JCheckBox chkBoxInfoActif;
	
	private static final String AlphaNumericRegex = "[a-zA-Z0-9 -]";
	
	/**
	 * Constructeur
	 * @param langue
	 */
	public VueGestionModifierUtilisateur(Language langue, Message messageDataChanged) {
		super(langue,messageDataChanged);
		this.langue = langue;
		initVariable();
		initComponents();
	}
	
	/**
	 * Initialisation des attributs de la classe
	 */
	private void initVariable(){
		panelInfoGestionEmploye  = new JPanel(new GridBagLayout());
		panelInfoManageFields    = new JPanel(new GridBagLayout());
		panelInfoAreaCommentaire = new JPanel(new GridBagLayout());
		
		separator = new JSeparator();
		
		constraint = new GridBagConstraints();
		
		labelInfoNom         = new JLabel(langue.getGestionUtilisateur_nomEmploye().replaceAll("%", ""));
		labelInfoPrenom      = new JLabel(langue.getGestionUtilisateur_prenomEmploye().replaceAll("%", ""));
		labelInfoCommentaire = new JLabel(langue.getGestionUtilisateur_commentaireEmploye().replaceAll("%", ""));

		textfieldInfoNom        = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textfieldInfoPrenom     = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textfieldUtilisateur    = JValidTextField.JValidTextFieldAllowCertainCharacter(AlphaNumericRegex, 16);
		textAreaInfoCommentaire = JValidScrollTextArea.JValidTextFieldAllowCertainCharacter(4, AlphaNumericRegex);
	
		labelInfoNomUtilisateur    = new JLabel(langue.getGestionUtilisateur_NomUtilisateur().replaceAll("%", ""));
		labelInfoGroupeUtilisateur = new JLabel(langue.getGestionUtilisateur_GroupeUtilisateur().replaceAll("%", ""));
		labelInfoActif             = new JLabel(langue.getGestionUtilisateur_Actif().replaceAll("%", ""));
		
		textfieldInfoNomUtilisateur    = JValidTextField.JValidTextFieldWithoutText(16);
		textfieldInfoGroupeUtilisateur = JValidTextField.JValidTextFieldWithoutText(16);
		
		chkBoxInfoActif = new JCheckBox();
		
		utilisateurId = 0;
		employeId = 0;
		
		setEnabled(false);
	}
	
	/**
	 * Initialisation des composants du formulaire
	 */
	private void initComponents(){ 
		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 = 5;
		panelInfoGestionEmploye.add(separator,constraint);
		
		/******************CHAMPS NON 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;
		constraint.gridwidth = 1;
		panelInfoManageFields.add(labelInfoNom,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;
		panelInfoManageFields.add(textfieldInfoNom,constraint);
		textfieldInfoNom.setEditable(false);
		
		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   = 2;
		constraint.gridy   = 0;
		panelInfoManageFields.add(labelInfoPrenom,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   = 3;
		constraint.gridy   = 0;
		panelInfoManageFields.add(textfieldInfoPrenom,constraint);
		textfieldInfoPrenom.setEditable(false);
		
		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;
		panelInfoManageFields.add(labelInfoNomUtilisateur,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   = 1;
		constraint.gridy   = 1;
		panelInfoManageFields.add(textfieldInfoNomUtilisateur,constraint);
		textfieldInfoNomUtilisateur.setEnabled(false);
		
		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   = 2;
		constraint.gridy   = 1;
		panelInfoManageFields.add(labelInfoGroupeUtilisateur,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   = 3;
		constraint.gridy   = 1;
		panelInfoManageFields.add(textfieldInfoGroupeUtilisateur,constraint);
		textfieldInfoGroupeUtilisateur.setEnabled(false);
		
		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;
		panelInfoManageFields.add(labelInfoActif,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   = 1;
		constraint.gridy   = 2;
		panelInfoManageFields.add(chkBoxInfoActif,constraint);
		chkBoxInfoActif.setEnabled(false);
		
		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;
		panelInfoAreaCommentaire.add(labelInfoCommentaire,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;
		panelInfoAreaCommentaire.add(textAreaInfoCommentaire,constraint);
		textAreaInfoCommentaire.setEnabled(false);
		
		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   = 2;
		add(panelInfoGestionEmploye,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   = 1;
		panelInfoGestionEmploye.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;
		panelInfoGestionEmploye.add(panelInfoAreaCommentaire,constraint);
		/******************FIN CHAMPS NON EDITABLES**********************/
	}
	
	public int getUtilisateurId() {
		return utilisateurId;
	}

	public void setUtilisateurId(int utilisateurId) {
		this.utilisateurId = utilisateurId;
	}
	
	public int getEmployeId() {
		return employeId;
	}

	public void setEmployeId(int employeId) {
		this.employeId = employeId;
	}
	
	/* setters on read only fields*/
	/**
	 * Affectation d'une valeur pour le champ nom
	 * @param text
	 */
	public void setTextFieldInfoNom(String text){
		textfieldInfoNom.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ prénom
	 * @param text
	 */
	public void setTextFieldInfoPrenom(String text){
		textfieldInfoPrenom.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ commentaire
	 * @param text
	 */
	public void setTextAreaInfoCommentaire(String text)
	{		
		textAreaInfoCommentaire.setText(DataNormalizer.normalize(text));
	}
	
	public void setTextFieldUtilisateur(String text)
	{
		textfieldUtilisateur.setText(text);
	}
	
	/* setters on read only fields */
	/**
	 * Affectation d'une valeur pour le champ nom utilisateur
	 * @param text
	 */
	public void setTextfieldInfoNomUtilisateur(String text){
		textfieldInfoNomUtilisateur.setText(DataNormalizer.normalize(text));
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean textfieldInfoNomUtilisateurIsValid(){
		return textfieldInfoNomUtilisateur.isValid();
	}
	
	/**
	 * Affectation d'une valeur pour le champ groupe utilisateur lecture seule
	 * @param text
	 */
	public void setTextfieldInfoGroupeUtilisateur(String text){
		textfieldInfoGroupeUtilisateur.setText(DataNormalizer.normalize(text));
	}
	
	/**
	 * Affectation d'une valeur pour le champ actif
	 * @param checked
	 */
	public void setChkBoxInfoActif(boolean checked) {
		chkBoxInfoActif.setSelected(checked);
	}
	
	/**
	 * 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, String groupeSelected, Boolean actif){
		super.AddDataToFields(utilisateur, nom, prenom, commentaire, groupe, actif);
		setSelectedGroupeUtilisateur(groupeSelected);
		setTextfieldInfoNomUtilisateur(utilisateur);
		setTextFieldInfoNom(nom);
		setTextFieldInfoPrenom(prenom);
		setTextAreaInfoCommentaire(commentaire);
		setTextfieldInfoGroupeUtilisateur(groupeSelected);
		setChkBoxInfoActif(actif);
	}
	
	/**
	 * Éfface les informations de l'article en lecture seule
	 */
	public void ClearInfo(){
		super.ClearInfo();
		setTextFieldNom("");
		setTextFieldPrenom("");
		setTextAreaCommentaire("");
		setTextFieldInfoNom("");
		setTextFieldInfoPrenom("");
		setTextAreaInfoCommentaire("");
		setTextfieldNomUtilisateur("");
		setTextfieldInfoNomUtilisateur("");
		setTextfieldInfoGroupeUtilisateur("");
		setChkBoxInfoActif(false);
		utilisateurId = 0;
		employeId = 0;
	}

	/**
	 * 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");
		VueGestionModifierUtilisateur norme = new VueGestionModifierUtilisateur(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);
	}
}