package visual.frame;

import javax.swing.*;

import control.ControlAdmin;

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

/**
 * Ventana destinada a la creacion de usuarios por parte de un administrador
 */
public class FrameCrear extends JFrame {

	private JButton bCrear;
	private JTextField fContraseña;
	private JTextField fUsuario;
	private JLabel lContraseña;
	private JLabel lUsuario;
	private JPanel panel;

	/**
	 * Constructor que inicia la ventana
	 */

	public FrameCrear() {
		initComponents();
	}

	/**
	 * Inicia los componentes de la ventana
	 */

	private void initComponents() {

		panel = new JPanel();
		bCrear = new JButton();
		lUsuario = new JLabel();
		lContraseña = new JLabel();
		fUsuario = new JTextField();
		fContraseña = new JTextField();
		
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				new FrameAdministrador();
				setVisible(false);
				dispose();
			}
		});

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

		bCrear.setBackground(new Color(33, 150, 243));
		bCrear.setText("Crear Usuario");
		bCrear.setContentAreaFilled(false);
		bCrear.setOpaque(true);
		bCrear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				bCrearActionPerformed(evt);
			}
		});

		lUsuario.setBackground(new Color(0, 204, 204));
		lUsuario.setFont(new Font("Candara", 1, 14)); // 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, 14)); // NOI18N
		lContraseña.setForeground(new Color(255, 255, 255));
		lContraseña.setText("Contraseña:");

		fUsuario.setToolTipText("");

		fContraseña.setToolTipText("");

		GroupLayout panelLayout = new GroupLayout(panel);
		panel.setLayout(panelLayout);
		panelLayout.setHorizontalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(panelLayout.createSequentialGroup().addGap(29, 29, 29)
						.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lContraseña, GroupLayout.Alignment.TRAILING).addComponent(lUsuario,
										GroupLayout.Alignment.TRAILING))
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(fContraseña, GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE).addComponent(
										fUsuario)
								.addComponent(bCrear, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE))
						.addGap(48, 48, 48)));
		panelLayout.setVerticalGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(panelLayout.createSequentialGroup().addGap(21, 21, 21)
						.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(lUsuario)
								.addComponent(fUsuario, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(panelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lContraseña).addComponent(fContraseña, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(bCrear)
						.addContainerGap(18, 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)
				.addGroup(layout.createSequentialGroup()
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGap(0, 0, 0)));

		pack();
		setVisible(true);
		setLocationRelativeTo(null);

	}

	/**
	 * Metodo que controla la accion al presionar el boton Crear
	 * <p>
	 * Si cumple las condiciones esperadas añade los datos ingresados en los
	 * JTextFields al archivo de usuario
	 * </p>
	 */

	private void bCrearActionPerformed(ActionEvent evt) {
		ControlAdmin control = new ControlAdmin();
		if (control.añadirUsuario(fUsuario.getText(), fContraseña.getText())) {
			new FrameAdministrador();
			setVisible(false);
			dispose();
		}
	}

}
