package visual.frame;

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

import control.ControlLogin;

/**
 * Ventana en la cual se realiza el registro de usuarios.
 */
public class FrameRegistro extends JFrame {

	private JButton bRegistrarse;
	private JButton bSalir;
	private JPanel bar;
	private JLabel bienvenido;
	private JPasswordField fContraseña;
	private JTextField fUsuario;
	private JPasswordField fVerificar;
	private JLabel lContraseña;
	private JLabel lUsuario;
	private JLabel lVerificiar;
	private JLabel pam;
	private JPanel panel;

	/**
	 * Constructor que inicia la ventana
	 */

	public FrameRegistro() {
		initComponents();
	}

	/**
	 * Metodo que inicia los componentes 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ña = new JPasswordField();
		bRegistrarse = new JButton();
		bSalir = new JButton();
		fVerificar = new JPasswordField();
		lVerificiar = new JLabel();

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setBackground(new Color(21, 21, 21));
		setMinimumSize(null);

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

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

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

		bar.setBackground(new Color(255, 255, 255));
		bar.setPreferredSize(new 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 Color(0, 204, 204));
		lUsuario.setFont(new Font("Candara", 1, 18)); // NOI18N
		lUsuario.setForeground(new Color(255, 255, 255));
		lUsuario.setText("Usuario");

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

		bRegistrarse.setText("Registrarse");
		bRegistrarse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				bRegistrarseActionPerformed(evt);
			}
		});

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

		lVerificiar.setBackground(new Color(0, 204, 204));
		lVerificiar.setFont(new Font("Candara", 1, 18)); // NOI18N
		lVerificiar.setForeground(new Color(255, 255, 255));
		lVerificiar.setText("Verificar Contraseña");

		GroupLayout panelLayout = new GroupLayout(panel);
		panel.setLayout(panelLayout);
		panelLayout.setHorizontalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(GroupLayout.Alignment.TRAILING, panelLayout.createSequentialGroup().addGap(140, 140, 140)
						.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(bar, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE)
								.addComponent(lUsuario)
								.addComponent(fUsuario, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
								.addComponent(lContraseña)
								.addComponent(fContraseña, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
								.addComponent(bRegistrarse, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
								.addComponent(bSalir, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
								.addComponent(fVerificar, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
								.addComponent(pam).addComponent(lVerificiar).addComponent(bienvenido))
						.addGap(140, 140, 140)));
		panelLayout.setVerticalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(panelLayout
				.createSequentialGroup().addGap(27, 27, 27)
				.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)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).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ña, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(lVerificiar)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(fVerificar, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).addGap(28, 28, 28)
				.addComponent(bRegistrarse).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(bSalir).addGap(31, 31, 31)));

		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();
		}

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

	}

	/**
	 * Accion que se realiza al presionarse el boton Registrarse
	 * <p>
	 * Usa a ControlLogin para añadir el usuario al archivo, si las
	 * comprobaciones salen correctas, cierra la ventana de registro y vuelve a
	 * la ventana login
	 * </p>
	 */

	private void bRegistrarseActionPerformed(ActionEvent evt) {

		ControlLogin control = new ControlLogin();
		if (control.añadirUsuario(fUsuario.getText(), String.valueOf(fContraseña.getPassword()),
				String.valueOf(fVerificar.getPassword()))) {
			FrameLogin login = new FrameLogin();
			setVisible(false);
			dispose();
		}

	}
	
	/**
	 * Accion que se realiza al presionar al boton Salir.
	 * <p>
	 * Vuelve a la ventana login y cierra la actual
	 * </p>
	 */
	
	private void bSalirActionPerformed(ActionEvent evt) {

		FrameLogin login = new FrameLogin();
		setVisible(false);
		dispose();
	}
}
