package visual.frame;

import javax.swing.*;

import control.ControlAdmin;

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

/**
 * Ventana destinada a la modificacion de usuarios
 */

public class FrameModificar extends JFrame {

	private JButton bModificar;
	private JComboBox<String> boxUsuario;
	private JTextField fContraseña;
	private JTextField fUsuario;
	private JLabel lContraseña;
	private JLabel lSelectUsuario;
	private JLabel lUsuario;
	private JPanel panel;

	/**
	 * Constructor que inicia la ventana
	 */

	public FrameModificar() {
		initComponents();
	}

	/**
	 * Inicia los componentes de la ventana y su comportamiento
	 */

	private void initComponents() {

		panel = new JPanel();
		bModificar = new JButton();
		lUsuario = new JLabel();
		lContraseña = new JLabel();
		fUsuario = new JTextField();
		fContraseña = new JTextField();
		boxUsuario = new JComboBox<>();
		lSelectUsuario = new JLabel();

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

		bModificar.setBackground(new Color(33, 150, 243));
		bModificar.setText("Modificar");
		bModificar.setContentAreaFilled(false);
		bModificar.setOpaque(true);
		bModificar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				bModificarActionPerformed(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("");

		boxUsuario.setModel(new DefaultComboBoxModel<>(new String[] {}));
		llenarBox();
		boxUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				boxUsuarioActionPerformed(evt);
			}
		});
		boxUsuarioActionPerformed(null);

		lSelectUsuario.setBackground(new Color(0, 204, 204));
		lSelectUsuario.setFont(new Font("Candara", 1, 14)); // NOI18N
		lSelectUsuario.setForeground(new Color(255, 255, 255));
		lSelectUsuario.setText("Seleccione usuario:");

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

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

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

	/**
	 * Accion que realiza el JComboBox al seleccionar un item, llena los
	 * JTextField con los datos correspondientes al usuario.
	 */

	protected void boxUsuarioActionPerformed(ActionEvent evt) {
		ControlAdmin control = new ControlAdmin();
		for (int x = 0; x < control.getRowCount(); x++) {
			if (control.getRow(x)[0].equals(boxUsuario.getSelectedItem())) {
				fContraseña.setText(control.getRow(x)[1]);
				fUsuario.setText(control.getRow(x)[0]);
			}
		}
	}

	/**
	 * Accion que se realiza al presionar el boton Modificar
	 * <p>
	 * Toma los datos ingresados y modifica los datos del usuario seleccionado
	 * en el JComboBox
	 * </p>
	 */

	protected void bModificarActionPerformed(ActionEvent evt) {
		ControlAdmin control = new ControlAdmin();
		String original = control.getRow(boxUsuario.getSelectedIndex())[0] + ";"
				+ control.getRow(boxUsuario.getSelectedIndex())[1];
		if (control.modificarUser(original, fUsuario.getText(), fContraseña.getText())) {
			new FrameAdministrador();
			setVisible(false);
			dispose();
		}
	}

	/**
	 * Llena el ComboBox con los nombres de usuario
	 */

	private void llenarBox() {

		ControlAdmin control = new ControlAdmin();
		boxUsuario.removeAllItems();
		for (int x = 0; x < control.getRowCount(); x++) {
			boxUsuario.addItem(control.getRow(x)[0]);
		}

	}
}
