package view.authentification;

import controller.authentification.Authentification_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.bddclink.web.BddcBrowserAccess;
import org.sig.fw.layout.component.*;
import org.sig.fw.layout.contener.SDialog;
import org.sig.fw.layout.pane.SOptionPane;

import java.awt.*;
import java.awt.event.*;

public class Authentification_view extends SDialog
{
	private static final long serialVersionUID = 1505542061727681505L;

	private Authentification_Controller ctrl_authentification;
	
	private STextField jtf_login;
	private SPasswordField jtf_password;
	
	private SButton jb_valider;
	private SCheckBox jcbx_save_me;
	
	public Authentification_view(Authentification_Controller ctrl_authentification, String login)
	{
		super("Authentification", true);
		this.setBounds(500, 500, 339, 164);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) 
			{
				ctrl_authentification.closeWithoutAuthentification();
			}
		});
		this.ctrl_authentification = ctrl_authentification;
		this.init(login);
		this.computePosition();
	}
	
	private void init(String login)
	{
		this.setSize(430, 200);
		this.setDefaultCloseOperation(SDialog.DISPOSE_ON_CLOSE);
		
		SLabel jl_login = new SLabel("Identifiant", SLabel.TYPE_COMMUN);
		SLabel jl_password = new SLabel("Mot de Passe", SLabel.TYPE_COMMUN);
		
		this.jtf_login = new STextField(login);
		jtf_login.addKeyListener(new KeyAdapter() 
		{
			@Override
			public void keyPressed(KeyEvent arg0)
			{
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER)
					validate_form();
			}
		});
		
		this.jtf_password = new SPasswordField("");
		jtf_password.addKeyListener(new KeyAdapter() 
		{
			@Override
			public void keyPressed(KeyEvent arg0)
			{
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER)
					validate_form();
			}
		});
		
		jcbx_save_me = new SCheckBox("Se souvenir de moi", true);
		
		this.jb_valider = new SButton("Valider", "Valider les informations de connexion");
		jb_valider.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				validate_form();
			}
		});
		jb_valider.setFont(new Font("SansSerif", Font.PLAIN, 13));
		getContentPane().setLayout(new MigLayout("", "[100px][grow]", "[][][][][grow]"));
		getContentPane().add(jl_login, "cell 0 0,grow");
		getContentPane().add(jl_password, "cell 0 1,grow");
		getContentPane().add(jtf_password, "cell 1 1,grow");
		getContentPane().add(jtf_login, "cell 1 0,grow");
		
		SLabel lblNewLabel = new SLabel("Mot de passe oublié ?");
		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				new BddcBrowserAccess().goToForgotPassword();
			}
		});
		getContentPane().add(lblNewLabel, "cell 1 2,alignx right");
		getContentPane().add(jcbx_save_me, "cell 1 3");
		getContentPane().add(jb_valider, "cell 0 4 2 1,alignx center,aligny bottom");
	}
	
	public void show_error(int show)
	{
		String message = "";
		
		if(show == -2)
		{
			message = "Vous ne faites pas partie du groupe Facturation ! ";
		}
		else if(show == -1)
		{
			message = "Identifiant ou Mot de passe incorrect ! ";
		}
		else if(show == 0)
		{
			message = "Votre compte est désactivé !\nVeuillez vous renseigner auprès de l'administrateur système !";
		}
		
		SOptionPane.showErrorMessage("Authentification", message);
	}
	
	private void validate_form()
	{
		String login = this.jtf_login.getText();
		char[] password = this.jtf_password.getPassword();
		
		if(login.equals("") || password.length == 0)
		{
			SOptionPane.showInformationMessage("Authentification", "L'identifiant et le Mot de passe ne peuvent être vides");
		}
		else
		{
			this.ctrl_authentification.authentification(login, password, jcbx_save_me.isSelected());
		}
	}
}
