package net.h4ckx0r.vista.panels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.*;

import net.h4ckx0r.controlador.ControladorCurso;
import net.h4ckx0r.modelo.Curso;
import net.h4ckx0r.modelo.Materia;

public class PanelMaterias extends JPanel {

    private static final long serialVersionUID = 1L;

    private Materia materiaActual;
    private JTextField tfNombre;
    private JTextField tfAcronimo;
    private JComboBox<Curso> comboBoxCurso;

    /**
     * Create the panel.
     */
    public PanelMaterias() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0, 0};
        gridBagLayout.rowHeights = new int[]{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, 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("Acrónimo:");
        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);

        tfAcronimo = 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(tfAcronimo, gbc_textField_1);
        tfAcronimo.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("Curso:");
        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);

        comboBoxCurso = new JComboBox<>();
        GridBagConstraints gbc_comboBox = new GridBagConstraints();
        gbc_comboBox.insets = new Insets(0, 0, 5, 0);
        gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
        gbc_comboBox.gridx = 1;
        gbc_comboBox.gridy = 2;
        add(comboBoxCurso, gbc_comboBox);
    }

    public Materia getMateriaActual() {
        materiaActual.setNombre(tfNombre.getText());
        materiaActual.setAcronimo(tfAcronimo.getText());
        materiaActual.setCurso_id(((Curso) comboBoxCurso.getSelectedItem()).getId());
        return materiaActual;
    }

    public void setMateriaActual(Materia materiaActual) {
        this.materiaActual = materiaActual;
        tfNombre.setText(materiaActual.getNombre());
        tfAcronimo.setText(materiaActual.getAcronimo());
        actualizarCursos();
    }

    private void actualizarCursos() {
        List<Curso> cursos = ControladorCurso.getAllCursos();
        comboBoxCurso.removeAllItems();
        for (Curso curso : cursos) {
            comboBoxCurso.addItem(curso);
            if (materiaActual.getCurso_id() == curso.getId()) {
                comboBoxCurso.setSelectedItem(curso);
            }
        }
        if (materiaActual.getCurso_id() == -1) {
            comboBoxCurso.setSelectedIndex(-1);
        }
    }
}