package net.h4ckx0r.vista.panels;

import net.h4ckx0r.controlador.ControladorCurso;
import net.h4ckx0r.controlador.ControladorTipologiaSexo;
import net.h4ckx0r.modelo.*;

import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.List;

public class PanelPersona extends JPanel {

	private static final long serialVersionUID = 1L;

	private Persona personaActual;

	private JTextField tfNombre;
	private JTextField tfApellido1;
	private JTextField tfApellido2;
	private JTextField tfDNI;
	private JTextField tfDireccion;
	private JTextField tfEmail;
	private JTextField tfTelefono;
	private JComboBox<TipologiaSexo> comboBoxSexo;

	/**
	 * Create the panel.
	 */
	public PanelPersona() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.2, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblNewLabel = new JLabel("Nombre:");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		add(lblNewLabel, gbc_lblNewLabel);
		
		tfNombre = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		add(tfNombre, gbc_textField);
		tfNombre.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Primer Apellido:");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 1;
		add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		tfApellido1 = new JTextField();
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.insets = new Insets(0, 0, 5, 0);
		gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_1.gridx = 1;
		gbc_textField_1.gridy = 1;
		add(tfApellido1, gbc_textField_1);
		tfApellido1.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Segundo Apellido:");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 2;
		add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		tfApellido2 = new JTextField();
		GridBagConstraints gbc_textField_2 = new GridBagConstraints();
		gbc_textField_2.insets = new Insets(0, 0, 5, 0);
		gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_2.gridx = 1;
		gbc_textField_2.gridy = 2;
		add(tfApellido2, gbc_textField_2);
		tfApellido2.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("Sexo:");
		GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
		gbc_lblNewLabel_7.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_7.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_7.gridx = 0;
		gbc_lblNewLabel_7.gridy = 3;
		add(lblNewLabel_7, gbc_lblNewLabel_7);
		
		comboBoxSexo = new JComboBox<>();
		GridBagConstraints gbc_comboBoxSexo = new GridBagConstraints();
		gbc_comboBoxSexo.insets = new Insets(0, 0, 5, 0);
		gbc_comboBoxSexo.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBoxSexo.gridx = 1;
		gbc_comboBoxSexo.gridy = 3;
		add(comboBoxSexo, gbc_comboBoxSexo);
		
		JLabel lblNewLabel_3 = new JLabel("DNI:");
		GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
		gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_3.gridx = 0;
		gbc_lblNewLabel_3.gridy = 4;
		add(lblNewLabel_3, gbc_lblNewLabel_3);
		
		tfDNI = new JTextField();
		GridBagConstraints gbc_textField_3 = new GridBagConstraints();
		gbc_textField_3.insets = new Insets(0, 0, 5, 0);
		gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_3.gridx = 1;
		gbc_textField_3.gridy = 4;
		add(tfDNI, gbc_textField_3);
		tfDNI.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("Dirección:");
		GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
		gbc_lblNewLabel_4.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_4.gridx = 0;
		gbc_lblNewLabel_4.gridy = 5;
		add(lblNewLabel_4, gbc_lblNewLabel_4);
		
		tfDireccion = new JTextField();
		GridBagConstraints gbc_textField_4 = new GridBagConstraints();
		gbc_textField_4.insets = new Insets(0, 0, 5, 0);
		gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_4.gridx = 1;
		gbc_textField_4.gridy = 5;
		add(tfDireccion, gbc_textField_4);
		tfDireccion.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("Email:");
		GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
		gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_5.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_5.gridx = 0;
		gbc_lblNewLabel_5.gridy = 6;
		add(lblNewLabel_5, gbc_lblNewLabel_5);
		
		tfEmail = new JTextField();
		GridBagConstraints gbc_textField_5 = new GridBagConstraints();
		gbc_textField_5.insets = new Insets(0, 0, 5, 0);
		gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_5.gridx = 1;
		gbc_textField_5.gridy = 6;
		add(tfEmail, gbc_textField_5);
		tfEmail.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("Teléfono");
		GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
		gbc_lblNewLabel_6.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_6.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_6.gridx = 0;
		gbc_lblNewLabel_6.gridy = 7;
		add(lblNewLabel_6, gbc_lblNewLabel_6);
		
		tfTelefono = new JTextField();
		GridBagConstraints gbc_textField_6 = new GridBagConstraints();
		gbc_textField_6.insets = new Insets(0, 0, 5, 0);
		gbc_textField_6.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_6.gridx = 1;
		gbc_textField_6.gridy = 7;
		add(tfTelefono, gbc_textField_6);
		tfTelefono.setColumns(10);

	}

	private void setPersona(Persona persona) {
		tfNombre.setText(persona.getNombre());
		tfApellido1.setText(persona.getApellido1());
		tfApellido2.setText(persona.getApellido2());
		tfDNI.setText(persona.getDni());
		tfDireccion.setText(persona.getDireccion());
		tfEmail.setText(persona.getEmail());
		tfTelefono.setText(persona.getTelefono());
		personaActual = persona;
		actualizarSexo();
	}

	private Persona getPersona() {
		personaActual.setNombre(tfNombre.getText());
		personaActual.setApellido1(tfApellido1.getText());
		personaActual.setApellido2(tfApellido2.getText());
		personaActual.setIdTipologiaSexo(((TipologiaSexo) comboBoxSexo.getSelectedItem()).getId());
		personaActual.setDni(tfDNI.getText());
		personaActual.setDireccion(tfDireccion.getText());
		personaActual.setEmail(tfEmail.getText());
		personaActual.setTelefono(tfTelefono.getText());
		return personaActual;
	}

	public void setEstudianteActual(Estudiante estudiante) {
		setPersona(estudiante);
	}

	public Estudiante getEstudianteActual() {
		return (Estudiante) getPersona();
	}

	public void setProfesorActual(Profesor profesor) {
		setPersona(profesor);
	}

	public Profesor getProfesorActual() {
		return (Profesor) getPersona();
	}

	private void actualizarSexo() {
		List<TipologiaSexo> sexos = ControladorTipologiaSexo.getAllSexos();
		comboBoxSexo.removeAllItems();
		for (TipologiaSexo sexo : sexos) {
			comboBoxSexo.addItem(sexo);
			if (personaActual.getIdTipologiaSexo() == sexo.getId()) {
				comboBoxSexo.setSelectedItem(sexo);
			}
		}
		if (personaActual.getIdTipologiaSexo() == -1) {
			comboBoxSexo.setSelectedIndex(-1);
		}
	}

}
