package view.vueAjoutClient;

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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import language.Francais;
import language.Language;
import view.Formulaire;
import view.tool.JEditableComboBox.JEditableComboBox;
import view.tool.JEditableComboBox.JEditableComboBoxControler;
import view.tool.inputField.JValidScrollTextArea;
import view.tool.inputField.JValidTextField;
import view.vuePopup.JObservablePopup;
import view.vuePopup.PopupAjouterGroupe;
import view.vuePopup.PopupAjouterTelephone;
import controler.Message;

public class AjoutClient extends Formulaire implements DocumentListener, ItemListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2020281089357139460L;

	private JPanel 				panelFields;
	
	private GridBagConstraints 	constraint;
	
	private JLabel 		labelAdresse,
						labelVille,
						labelCodePostal,
						labelTelephone,
						labelPrenom,
						labelNom,
					    labelGroupe,
						labelCommentaire;
	
	private JValidTextField		textPrenom,
							    textNom,
							    textAdresse,
							    textCodePostal,
							    textVille;
	
	private JValidScrollTextArea textAreaCommentaire;
	
	private JObservablePopup 	paneTelephone;
	private JEditableComboBoxControler controleurTelephone;
	private JEditableComboBox 	comboboxTelephone;
	
	private JObservablePopup 	paneGroupe;
	private JEditableComboBoxControler controleurGroupe;
	private JEditableComboBox 	comboboxGroupe;
	
	private Language lang;
	
	private int ville_id;
	
	private boolean allowDataChangeListener = true;
	
	public AjoutClient(Language lang, Message messageDataChanged) throws Exception
	{
		super(messageDataChanged);
		initVariable(lang);
		initComponents();
		initListener();
		setMnemonic();
	}
	
	/**
	 * initialise tout les variable
	 */
	private void initVariable(Language lang)
	{
		this.lang = lang;
		
		constraint 		 	= new GridBagConstraints();

		labelAdresse 	 	= new JLabel(lang.getClientAdresse().replaceAll("%", ""));
		labelVille 		 	= new JLabel(lang.getClientVille().replaceAll("%", ""));
		labelCodePostal  	= new JLabel(lang.getClientCodePostal().replaceAll("%", ""));
		labelTelephone 	 	= new JLabel(lang.getClientNumeroTelephone().replaceAll("%", ""));
		
		labelCommentaire 	= new JLabel(lang.getClientCommentaire().replaceAll("%", ""));
		labelPrenom 	 	= new JLabel(lang.getClientPrenom().replaceAll("%", ""));
		labelNom 	     	= new JLabel(lang.getClientNom().replaceAll("%", ""));
		labelGroupe 		= new JLabel(lang.getClientGroupe().replaceAll("%", ""));
		
		textPrenom 		 	= JValidTextField.JValidTextFieldAllowCertainCharacter("[a-zA-Z -]", 20);
		textNom 		 	= JValidTextField.JValidTextFieldAllowCertainCharacter("[a-zA-Z -]", 20);
		textAdresse 	 	= JValidTextField.JValidTextFieldWithoutText(20);//JValidTextField.JValidTextFieldAllowCertainCharacter("[a-zA-Z -0-9]", 16);
		textCodePostal  	= JValidTextField.JValidTextFieldAllowCertainCharacter("[a-zA-Z\\- 0-9]", 20);
		textVille           = JValidTextField.JValidTextFieldWithoutText(20);
		textVille.setEditable(false);
		
		textAreaCommentaire = JValidScrollTextArea.JValidTextFieldWithoutText(4);
		
		paneTelephone 		= new PopupAjouterTelephone();
		controleurTelephone = new JEditableComboBoxControler(paneTelephone);
		comboboxTelephone 	= JEditableComboBox.hasPlusAndMinus(new String[]{""},paneTelephone,controleurTelephone.plus,controleurTelephone.moin);
		
		paneGroupe 		= new PopupAjouterGroupe();
		controleurGroupe = new JEditableComboBoxControler(paneGroupe);
		comboboxGroupe 	= JEditableComboBox.hasPlus(new String[]{""},paneGroupe,controleurGroupe.plus);
		
		panelFields 		= new JPanel(new GridBagLayout());
	}
	
	/**
	 * initialise tout les components
	 */
	private void initComponents()
	{
		//============Fields=============================
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridheight = 1;
		constraint.gridwidth = 1;
		constraint.gridx = 0;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelPrenom, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textPrenom, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 2;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelAdresse, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 3;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textAdresse, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelNom, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textNom, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 0;
		constraint.weighty = 1;
		constraint.gridx = 2;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelVille, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 3;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textVille, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 0;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 2;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelGroupe, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 2;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(comboboxGroupe, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 2;
		constraint.gridy = 2;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelCodePostal, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 3;
		constraint.gridy = 2;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textCodePostal, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 0;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 3;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelTelephone, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 3;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(comboboxTelephone, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridheight = 1;
		constraint.gridwidth = 2;
		constraint.gridx = 0;
		constraint.gridy = 4;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelCommentaire, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridheight = 1;
		constraint.gridwidth = 5;
		constraint.gridx = 0;
		constraint.gridy = 5;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textAreaCommentaire, constraint);
		
		//===========Ajout au panel principal==================
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 0, 0, 0);
		add(panelFields, constraint);
	}
	
	/**
	 * initialise tout les listener
	 */
	private void initListener()
	{
		textAdresse.getDocument().addDocumentListener(this);
	    textPrenom.getDocument().addDocumentListener(this);
	    textNom.getDocument().addDocumentListener(this);
	    textCodePostal.getDocument().addDocumentListener(this);
	    textVille.getDocument().addDocumentListener(this);
	    
	    textAreaCommentaire.getDocument().addDocumentListener(this);

	    comboboxTelephone.addItemListener(this);
	    comboboxGroupe.addItemListener(this);
	}
	
	private void setMnemonic()
	{
		labelNom.setDisplayedMnemonic(lang.getClientNom().charAt(lang.getClientNom().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelNom.setLabelFor(textNom);
		labelPrenom.setDisplayedMnemonic(lang.getClientPrenom().charAt(lang.getClientPrenom().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelPrenom.setLabelFor(textPrenom);
		labelAdresse.setDisplayedMnemonic(lang.getClientAdresse().charAt(lang.getClientAdresse().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelAdresse.setLabelFor(textAdresse);
		labelVille.setDisplayedMnemonic(lang.getClientVille().charAt(lang.getClientVille().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelVille.setLabelFor(textVille);
		labelCodePostal.setDisplayedMnemonic(lang.getClientCodePostal().charAt(lang.getClientCodePostal().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelCodePostal.setLabelFor(textCodePostal);
		labelCommentaire.setDisplayedMnemonic(lang.getClientCommentaire().charAt(lang.getClientCommentaire().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelCommentaire.setLabelFor(textAreaCommentaire);
		labelGroupe.setDisplayedMnemonic(lang.getClientGroupe().charAt(lang.getClientGroupe().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelGroupe.setLabelFor(comboboxGroupe);
		labelTelephone.setDisplayedMnemonic(lang.getClientNumeroTelephone().charAt(lang.getClientNumeroTelephone().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelTelephone.setLabelFor(comboboxTelephone);
	}
	
	public void addDataToFields(String Prenom, String Nom, String Adresse, String CodePostal, String Ville, String[] Groupe, String[] Telephone, String Commentaire)
	{
		allowDataChangeListener = false;
		
		setTxtPrenom(Prenom);
		setTxtNom(Nom);
		setTxtAdresse(Adresse);
		setCodePostal(CodePostal);
		setAreaParticularite(Commentaire);
		
		for(int i = 0; i < Groupe.length;i++)
		{
			addGroupe(Groupe[i]);
		}
		
		setTxtVille(Ville);
		for(int i = 0; i < Telephone.length;i++)
		{
			addTelephone(Telephone[i]);
		}
		
		allowDataChangeListener = true;
	}
	
	public void clearInfo()
	{
		allowDataChangeListener = false;
		
		textAdresse.setText("");
		textAreaCommentaire.setText("");
		textCodePostal.setText("");
		textNom.setText("");
		textPrenom.setText("");
		comboboxGroupe.removeAllItems();
		comboboxTelephone.removeAllItems();
		textVille.setText("");
		
		allowDataChangeListener = true;
	}
	
	@Override
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		
		textAdresse.setEditable(enabled);
		textAreaCommentaire.setEnabled(enabled);
		textCodePostal.setEditable(enabled);
		textNom.setEditable(enabled);
		textPrenom.setEditable(enabled);
		comboboxGroupe.setEnabled(enabled);
		comboboxTelephone.setEnabled(enabled);
		textVille.setEditable(enabled);
	}
	
	public String getTxtPrenom() {
		return textPrenom.getText();
	}

	public void setTxtPrenom(String txtPrenom) {
		this.textPrenom.setText(txtPrenom);
	}

	public String getTxtNom() {
		return textNom.getText();
	}

	public void setTxtNom(String txtNom) {
		this.textNom.setText(txtNom);
	}

	public String getTxtAdresse() {
		return textAdresse.getText();
	}

	public void setTxtAdresse(String txtAdresse) {
		this.textAdresse.setText(txtAdresse);
	}

	public String getTxtVille() {
		return textVille.getText();
	}
	
	public void setTxtVille(String value) {
		this.textVille.setText(value);
	}

	public String getCodePostal() {
		return textCodePostal.getText();
	}
	
	public void setCodePostal(String text)
	{
		this.textCodePostal.setText(text);
	}

	public String getSelectedTelephone() {
		return comboboxTelephone.getSelectedItem();
	}

	public int getCountTelephone(){
		return comboboxTelephone.getItemCount();
	}
	
	public String getTelephoneAt(int i){
		return comboboxTelephone.getItemAt(i);
	}
	
	public void addTelephone(String value) {
		this.comboboxTelephone.addItem(value);
	}
	
	public void clearTelephone(){
		this.comboboxTelephone.removeAllItems();
	}

	public String getAreaParticularite() {
		return textAreaCommentaire.getText();
	}

	public void setAreaParticularite(String areaParticularite) {
		this.textAreaCommentaire.setText(areaParticularite);
	}

	public String getSelectedGroupe() {
		if (comboboxGroupe.getSelectedItem()!=null)
			 comboboxGroupe.getSelectedItem().toString();
		return "";
	}

	public void addGroupe(String cbGroupe) {
		this.comboboxGroupe.addItem(cbGroupe);
	}
	
	public int getCountGroupe(){
		return this.comboboxGroupe.getItemCount();
	}
	
	public String getGroupeAt(int i){
		return this.comboboxGroupe.getItemAt(i);
	}
	
	public void clearGroupe(){
		this.comboboxGroupe.removeAllItems();
	}

	public int getVille_id() {
		return ville_id;
	}

	public void setVille_id(int ville_id) {
		this.ville_id = ville_id;
	}
	
	public void setVilleNom(String nom){
		textVille.setText(nom);
	}

	static void main(String[] args) throws Exception {
		JFrame a = new JFrame("window");
		AjoutClient norme = new AjoutClient(Francais.getInstance(),null);
		norme.setTxtPrenom("asd");
		//norme.getObservable().addObserver(new Controleur());
		a.add(norme);
		
		String[] groupe = {"Bob"},
				 telephone = {"819-829-3931"};
		
		norme.addDataToFields("Jonathan", "Guertin", "2697 rue des Iris", "J1R 0E4", "Sherbrooke", groupe, telephone, "Le client à déjà mangé un pirhana vivant");
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.pack();
		a.setVisible(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();
		}
	}
}
