/**
 * 
 */
package view.vueConnectionPanel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import controler.connection.ControleurConnection;

import language.Francais;
import language.Language;
import tool.security.AES;
import view.Vue;

/**
 * @author alexis
 *
 */
public class VueConnection extends Vue implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7196212627131220731L;
	private static final String cle = "7196212627131220731";
	public static VueConnection instance = null;
	
	private JTextField textUser;
	private JButton buttonOk,
					buttonCancel;
	private JLabel labelUser,
	               labelPass;
    
	private JPasswordField textPass;
	private Language lang;
        
	/**
	 * constructeur
	 */
	private VueConnection(Language lang) {
		initVariable(lang);
		initComponents();
		initListener();
		setMnemonic();
	}
	
	public static VueConnection getInstance() throws Exception{
		if(instance == null)
			throw new Exception("Variable not initialised, should you setLanguage first");
		return instance;
	}
	
	public void initVariable(Language lang){
		this.lang = lang;
		this.setLayout(new BorderLayout());
		this.setSize(300,150);
		this.setPreferredSize(new Dimension(300,150));
	}
	
	public void initComponents(){
		JPanel labelPanel = new JPanel(new GridLayout(3, 1));
	    JPanel fieldPanel = new JPanel(new GridLayout(3, 1));
	    add(labelPanel, BorderLayout.WEST);
	    add(fieldPanel, BorderLayout.EAST);
	    
	    //buttons
	    buttonOk = new JButton(lang.getConnexionOk().replaceAll("%", ""));
	    buttonCancel = new JButton(lang.getConnexionAnnuler().replaceAll("%", ""));
	    
	    //fields
	    textUser = new JTextField();
	    textPass = new JPasswordField();

	    textUser.setColumns(15);
	    textPass.setColumns(15);
	    
	    JPanel paneFlowUser = new JPanel(new FlowLayout(FlowLayout.LEFT));
	    paneFlowUser.add(textUser);
	    JPanel paneFlowPass = new JPanel(new FlowLayout(FlowLayout.LEFT));
	    paneFlowPass.add(textPass);
	    JPanel paneFlowCancel = new JPanel(new FlowLayout(FlowLayout.CENTER));
	    paneFlowCancel.add(buttonCancel);
	      
	    fieldPanel.add(paneFlowUser);
	    fieldPanel.add(paneFlowPass);
	    fieldPanel.add(paneFlowCancel);
	    
	    //labels
	    JPanel paneFlowLabUser = new JPanel(new FlowLayout(FlowLayout.RIGHT));
	    labelUser = new JLabel(lang.getConnexionUtilisateur().replaceAll("%", "")+" :", JLabel.RIGHT);
	    paneFlowLabUser.add(labelUser);
	    JPanel paneFlowLabPass = new JPanel(new FlowLayout(FlowLayout.RIGHT));
	    labelPass = new JLabel(lang.getConnexionMotdepasse().replaceAll("%", "")+" :", JLabel.RIGHT);
	    paneFlowLabPass.add(labelPass);
	    
	    labelUser.setLabelFor(textUser);
	    labelPass.setLabelFor(textPass);
	    
	    JPanel paneFlowOk = new JPanel(new FlowLayout(FlowLayout.CENTER));
	    paneFlowOk.add(buttonOk);

	    labelPanel.add(paneFlowLabUser);
	    labelPanel.add(paneFlowLabPass);
	    labelPanel.add(paneFlowOk);
	}
	
	public void initListener(){
		buttonOk.addActionListener(this);
	    buttonCancel.addActionListener(this);
	}
	
	public String getUser(){
		return textUser.getText();
	}
	
	public char[] getPassword(){
		return textPass.getPassword();
	}
	
	public Language getLang(){
		return lang;
	}
	
	public void setMnemonic(){
		buttonOk.setMnemonic(lang.getConnexionOk().charAt(lang.getConnexionOk().indexOf('%')+1));
		buttonCancel.setMnemonic(lang.getConnexionAnnuler().charAt(lang.getConnexionAnnuler().indexOf('%')+1));
	    labelUser.setDisplayedMnemonic(lang.getConnexionUtilisateur().charAt(lang.getConnexionUtilisateur().indexOf('%')+1));
	    labelPass.setDisplayedMnemonic(lang.getConnexionMotdepasse().charAt(lang.getConnexionMotdepasse().indexOf('%')+1));
	}
	
	public static void setLanguage(Language lang)
	{
		instance = new VueConnection(lang);
	}
	
	public void alertConnectionFailed(){
		JOptionPane.showMessageDialog(this,lang.getErreur_Connexion(),lang.getErreur_Connexion_Titre(),JOptionPane.ERROR_MESSAGE);
		textUser.setText("");
		textPass.setText("");
		
		textUser.requestFocus();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg) {
		if(arg.getActionCommand().equals(lang.getConnexionOk().replaceAll("%", "")))
		{
			try {
				textPass.setText(String.valueOf(AES.encrypt(cle, new String(textPass.getPassword()))));
				ControleurConnection.attemptLogin.trigger(this);
			} catch (Exception e) {
				e.printStackTrace();
				alertConnectionFailed();
			}
			textUser.setText("");
			textPass.setText("");
		}
		else // is "Annuler"
		{
			//Va chercher le jframe et le ferme
			((JFrame)this.getParent().getParent().getParent().getParent()).dispose();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame a = new JFrame();
		VueConnection con = new VueConnection(Francais.getInstance());
		a.add(con);
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.pack();
		a.setVisible(true);
	}
}
