package visual.frame;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import control.ControlLogin;

/**
 * Ventana destinada a el logeo de un usuario en el sistema.
 */

public class FrameLogin extends JFrame {

	private JButton bIngresar;
	private JButton bRegistrar;
	private JButton bSalir;
	private JPanel bar;
	private JLabel bienvenido;
	private JPasswordField fContraseña1;
	private JTextField fUsuario;
	private JLabel lContraseña;
	private JLabel lUsuario;
	private JLabel pam;
	private JPanel panel;
	private JMenuBar menubar;
	private JMenu menu;

	/**
	 * Constructor que inicia la ventana.
	 */

	public FrameLogin() {
		initComponents();
	}

	/**
	 * Metodo que inicia los elementos de la ventana
	 */

	private void initComponents() {

		panel = new JPanel();
		pam = new JLabel();
		bienvenido = new JLabel();
		bar = new JPanel();
		lUsuario = new JLabel();
		fUsuario = new JTextField();
		lContraseña = new JLabel();
		fContraseña1 = new JPasswordField();
		bIngresar = new JButton();
		bRegistrar = new JButton();
		bSalir = new JButton();

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setBackground(new java.awt.Color(21, 21, 21));

		menubar = new JMenuBar();
		menu = new JMenu("Administrador");

		menu.addMenuListener(new MenuListener() {

			@Override
			public void menuSelected(MenuEvent e) {

				openAdmin();
				menu.setSelected(false);
			}

			@Override
			public void menuDeselected(MenuEvent e) {
			}

			@Override
			public void menuCanceled(MenuEvent e) {

			}
		});
		menubar.add(menu);
		setJMenuBar(menubar);

		panel.setBackground(new java.awt.Color(0, 128, 128));

		pam.setFont(new java.awt.Font("Candara", 1, 60)); // NOI18N
		pam.setForeground(new java.awt.Color(255, 255, 255));
		pam.setText("P.A.M");

		bienvenido.setFont(new java.awt.Font("Candara", 1, 18)); // NOI18N
		bienvenido.setForeground(new java.awt.Color(255, 255, 255));
		bienvenido.setText(" Bienvenido de nuevo ");

		bar.setBackground(new java.awt.Color(255, 255, 255));
		bar.setPreferredSize(new java.awt.Dimension(215, 2));

		GroupLayout barLayout = new GroupLayout(bar);
		bar.setLayout(barLayout);
		barLayout.setHorizontalGroup(
				barLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 251, Short.MAX_VALUE));
		barLayout.setVerticalGroup(
				barLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 9, Short.MAX_VALUE));

		lUsuario.setBackground(new java.awt.Color(0, 204, 204));
		lUsuario.setFont(new java.awt.Font("Candara", 1, 18)); // NOI18N
		lUsuario.setForeground(new java.awt.Color(255, 255, 255));
		lUsuario.setText("Usuario");
		lContraseña.setBackground(new java.awt.Color(0, 204, 204));
		lContraseña.setFont(new java.awt.Font("Candara", 1, 18)); // NOI18N
		lContraseña.setForeground(new java.awt.Color(255, 255, 255));
		lContraseña.setText("Contraseña");

		bIngresar.setText("Ingresar");
		bIngresar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				bIngresarActionPerformed(evt);
			}
		});

		bRegistrar.setText("Registrarse");
		bRegistrar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				bRegistrarActionPerformed(evt);
			}
		});

		bSalir.setText("Salir");
		bSalir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				bSalirActionPerformed(evt);
			}
		});

		GroupLayout panelLayout = new GroupLayout(panel);
		panel.setLayout(panelLayout);
		panelLayout.setHorizontalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(panelLayout.createSequentialGroup().addGap(95, 95, 95)
						.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(bar, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE)
								.addComponent(pam).addComponent(bRegistrar).addComponent(bienvenido)
								.addComponent(bIngresar, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
								.addComponent(bSalir, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
								.addComponent(fUsuario, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
								.addComponent(lUsuario).addComponent(lContraseña).addComponent(fContraseña1,
										GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE))
						.addContainerGap(94, Short.MAX_VALUE)));
		panelLayout.setVerticalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(panelLayout.createSequentialGroup().addGap(29, 29, 29)
						.addComponent(pam, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE).addGap(1, 1, 1)
						.addComponent(bienvenido, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(bar, GroupLayout.PREFERRED_SIZE, 9, GroupLayout.PREFERRED_SIZE).addGap(13, 13, 13)
						.addComponent(lUsuario).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(fUsuario, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(lContraseña)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(fContraseña1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18).addComponent(bIngresar)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(bRegistrar)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(bSalir)
						.addContainerGap(17, Short.MAX_VALUE)));

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(panel,
				GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(panel,
				GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		getRootPane().setDefaultButton(bIngresar);
		setUndecorated(true);
		pack();
		setVisible(true);
		setLocationRelativeTo(null);

	}

	/**
	 * Accion que se realiza al presionar el boton Ingresar
	 * <p>
	 * llama a control login y lo utiliza para comprobar si el par usuario
	 * contraseña es correcto
	 * </p>
	 */

	private void bIngresarActionPerformed(ActionEvent evt) {

		ControlLogin control = new ControlLogin();

		if (control.isUserPass(fUsuario.getText(), String.valueOf(fContraseña1.getPassword()))) {

			FramePrincipal principal = new FramePrincipal(fUsuario.getText());

			dispose();

		} else {

			JOptionPane.showMessageDialog(null, "Usuario o contraseña incorrectas", null, JOptionPane.ERROR_MESSAGE);

		}

	}

	/**
	 * Cierra el programa al presionarse el boton Salir
	 */
	private void bSalirActionPerformed(ActionEvent evt) {

		System.exit(0);

	}

	/**
	 * Abre la ventana Registro al presionarse el boton Registrar
	 */

	private void bRegistrarActionPerformed(ActionEvent evt) {

		FrameRegistro registro = new FrameRegistro();
		dispose();
	}

	/**
	 * Abre la ventana Administrador al presionarse el boton en el menu superior.
	 * Esto solo si el codigo ingresado es el correcto, el cual es 7419.
	 */
	
	private void openAdmin() {
		String opcion = JOptionPane.showInputDialog("Ingrese codigo de administrador");
		if (opcion != null) {
			if (opcion.equals("7419")) {
				new FrameAdministrador();
				setVisible(false);
				dispose();
			} else {
				JOptionPane.showMessageDialog(null, "Codigo incorrecto");
			}
		}
	}

}
