package view.vueAjoutVille;

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

import language.Francais;
import language.Language;
import view.Formulaire;
import view.tool.inputField.JValidTextField;
import controler.Message;

public class AjoutVille extends Formulaire implements DocumentListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6904083399337310705L;

	private JPanel panelFields;
	
	private GridBagConstraints 	constraint;
	
	private JLabel 		labelVille,
						labelProvince,
						labelPays;	
	
	private JValidTextField		textVille,
							    textProvince,
							    textPays;
	
	private Language lang;
	
	private boolean allowDataChangeListener = true;
	
	public AjoutVille(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();

		labelVille			= new JLabel(lang.getVilleVille().replaceAll("%", "") + " : ");
		labelProvince 		= new JLabel(lang.getVilleProvince().replaceAll("%", "") + " : ");
		labelPays 			= new JLabel(lang.getVillePays().replaceAll("%", "") + " : ");
		
		textVille 		= JValidTextField.JValidTextFieldAllowCertainCharacter("[a-zA-Z -]", 16);
		textProvince 	= JValidTextField.JValidTextFieldAllowCertainCharacter("[a-zA-Z -]", 16);
		textPays 	 	= JValidTextField.JValidTextFieldAllowCertainCharacter("[a-zA-Z -]", 16);
		
		panelFields 		= new JPanel(new GridBagLayout());
	}
	
	/**
	 * initialise tout les components
	 */
	private void initComponents()
	{
		//============Fields=============================
		constraint.anchor = GridBagConstraints.NORTHWEST;
		constraint.fill = GridBagConstraints.NONE;
		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(labelVille, constraint);
		
		constraint.fill = GridBagConstraints.NONE;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textVille, constraint);
		
		constraint.fill = GridBagConstraints.NONE;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelProvince, constraint);
		
		constraint.fill = GridBagConstraints.NONE;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(textProvince, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 2;
		constraint.insets = new Insets(0, 5, 5, 5);
		panelFields.add(labelPays, 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(textPays, constraint);

		//===========Ajout au panel principal==================
		constraint.fill = GridBagConstraints.NONE;
		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);
	}
	
	private void initListener(){
		textVille.getDocument().addDocumentListener(this);
		textProvince.getDocument().addDocumentListener(this);
		textPays.getDocument().addDocumentListener(this);
	}
	
	private void setMnemonic()
	{
		labelVille.setDisplayedMnemonic(lang.getClientNom().charAt(lang.getClientNom().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelVille.setLabelFor(textVille);
		labelProvince.setDisplayedMnemonic(lang.getClientPrenom().charAt(lang.getClientPrenom().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelProvince.setLabelFor(textProvince);
		labelPays.setDisplayedMnemonic(lang.getClientAdresse().charAt(lang.getClientAdresse().indexOf('%')+1));	//rend le label sensible au ALT+L
		labelPays.setLabelFor(textPays);
	}
	
	public void addDataToFields(String Ville, String Province, String Pays)
	{
		allowDataChangeListener = false;
		
		setTxtVille(Ville);
		setTxtProvince(Province);
		setTxtPays(Pays);
		
		allowDataChangeListener = true;
	}
	
	public void clearInfo()
	{
		allowDataChangeListener = false;
		
		textVille.setText("");
		textProvince.setText("");
		textPays.setText("");
		
		allowDataChangeListener = true;
	}
	
	@Override
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		textVille.setEditable(enabled);
		textProvince.setEditable(enabled);
		textPays.setEditable(enabled);
	}
	
	public String getTxtVille() {
		return textVille.getText();
	}

	public void setTxtVille(String txtVille) {
		this.textVille.setText(txtVille);
	}

	public String getTxtProvince() {
		return textProvince.getText();
	}

	public void setTxtProvince(String txtProvince) {
		this.textProvince.setText(txtProvince);
	}

	public String getTxtPays() {
		return textPays.getText();
	}

	public void setTxtPays(String txtPays) {
		this.textPays.setText(txtPays);
	}
	
	@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 {
			System.out.println("insert");
			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();
		}
	}
	
	public static void main(String[] args) throws Exception {
		JFrame a = new JFrame("window");
		AjoutVille norme = new AjoutVille(Francais.getInstance(),null);
		//norme.getObservable().addObserver(new Controleur());
		a.add(norme);
		
		String[] ville = {"Sherbrooke"},
				 groupe = {"Bob"},
				 telephone = {"819-829-3931"};
		
		norme.addDataToFields("sher","queb","can");
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.pack();
		a.setVisible(true);
	}
}
