package interfaz;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


import negocio.Usuario;


import org.hibernate.Session;
import org.hibernate.Transaction;

import persistencia.UtilidadHibernate;

import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComboBox;
import java.awt.Dimension;
import javax.swing.JPasswordField;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;

public class Login extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JButton jButton = null;

	private JLabel jLabel = null;

	private JTextField jTextField = null;
	
	public Principal padrePrincipal;

	private JLabel jLabel1 = null;

	private JPasswordField jPasswordField = null;

	/**
	 * @param owner
	 */
	public Login(Principal owner) {
		super(owner);
		padrePrincipal = owner;
		initialize();
	}
	
	public class PresionarTeclaEnter extends KeyAdapter{
		public void keyPressed (KeyEvent e){
			if (e.getKeyCode() == KeyEvent.VK_ENTER){
				Aceptar();
			}
		}
		
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(355, 145);
		this.setModal(false);
		this.rootPane.setDefaultButton(jButton);
		if (padrePrincipal.idioma.equals("español"))
			this.setTitle("Login");
		else
			this.setTitle("Login");
		this.setContentPane(getJContentPane());
		this.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyReleased(java.awt.event.KeyEvent e) {
				int code = e.getKeyCode();
				if (code == KeyEvent.VK_ENTER){
					Aceptar();
				}
					
			}
		});
	}
	
	private void Aceptar(){
		try{
			if((jTextField.getText().trim()).length()<=0){
				if (padrePrincipal.idioma.equals("español"))
					throw new Exception("El nombre está vacío.");
				else
					throw new Exception("The name is empty.");
			}else if (jPasswordField.getText().trim().length()<=0){
				if (padrePrincipal.idioma.equals("español"))
					throw new Exception("La contraseña está vacía.");
				else
					throw new Exception("The password is empty.");
			}
			else{
				
				//Creamos la sesión para almacenar el nombre del proyecto en la BD
				Session session=null;
				
				session = UtilidadHibernate.getSessionFactory().openSession();

				Usuario user = new Usuario();
				user.setLogin(jTextField.getText().trim());
				user.setPassword(jPasswordField.getText().trim());
				
				List messages = session.createQuery("from Usuario where login = '"+user.getLogin()+"' AND password = '"+user.getPassword()+"'").list();
				Iterator iter = messages.iterator();
				if (messages.size()>=1){
					padrePrincipal.usuario = (Usuario) iter.next();
					if (padrePrincipal.usuario.getRol().equals("Implanter")){
						SeleccionarMarco ventana = new SeleccionarMarco(padrePrincipal, true);
						ventana.setLocationRelativeTo(padrePrincipal);
						ventana.setVisible(true);
						ventana.setModal(true);
						Login.this.dispose();
					}
					else if (padrePrincipal.usuario.getRol().equals("Developer")){
						padrePrincipal.dispose();
						Principal thisClass = new Principal(true, padrePrincipal.usuario);
						thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						thisClass.jMenuItem9.setEnabled(false);
						thisClass.setVisible(true);
					}
				}
				else{
					if (padrePrincipal.idioma.equals("español"))
						throw new Exception("Usuario incorrecto.");
					else
						throw new Exception("wrong user.");
				}
				session.close();
				
			}
		}	
		catch (Exception exp) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(null, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.EAST);
			jContentPane.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new FlowLayout());
			jPanel.setBackground(SystemColor.controlHighlight);
			jPanel.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.lightGray));
			jPanel.add(getJButton(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.fill = GridBagConstraints.BOTH;
			gridBagConstraints12.gridy = 1;
			gridBagConstraints12.weightx = 1.0;
			gridBagConstraints12.insets = new Insets(5, 5, 0, 15);
			gridBagConstraints12.gridx = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.insets = new Insets(5, 20, 0, 0);
			gridBagConstraints11.anchor = GridBagConstraints.EAST;
			gridBagConstraints11.gridy = 1;
			jLabel1 = new JLabel();
			if (padrePrincipal.idioma.equals("español"))
				jLabel1.setText("Contraseña:");
			else
				jLabel1.setText("Password:");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(5, 5, 0, 15);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(5, 20, 0, 0);
			gridBagConstraints.anchor = GridBagConstraints.EAST;
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			if (padrePrincipal.idioma.equals("español"))
				jLabel.setText("Usuario:");
			else
				jLabel.setText("User:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBackground(new Color(238, 238, 238));
			jPanel1.add(jLabel, gridBagConstraints);
			jPanel1.add(getJTextField(), gridBagConstraints1);
			jPanel1.add(jLabel1, gridBagConstraints11);
			jPanel1.add(getJPasswordField(), gridBagConstraints12);
			
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.addKeyListener(new PresionarTeclaEnter());
			if (padrePrincipal.idioma.equals("español"))
				jButton.setText("Aceptar");
			else
				jButton.setText("Accept");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Aceptar();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
		}
		return jTextField;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
		}
		return jPasswordField;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
