package examenes.examen20250509.vista;

import examenes.examen20250509.controlador.ControladorConcierto;
import examenes.examen20250509.controlador.ControladorGrupo;
import examenes.examen20250509.controlador.ControladorSala;
import examenes.examen20250509.modelo.Concierto;
import examenes.examen20250509.modelo.Sala;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Component;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FiltradoConciertos extends JPanel {

    private static final long serialVersionUID = 1L;
    private JTextField textFieldGrupo;
    private JComboBox<Sala> comboBoxSala;
    private JTextField textField;
    private DefaultListModel<String> dlm = new DefaultListModel<>();
    private JList listaSalas;

    /**
     * Create the panel.
     */
    public FiltradoConciertos() {
        setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        add(panel, BorderLayout.NORTH);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
        gbl_panel.rowHeights = new int[]{0, 0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblNewLabel_1 = new JLabel("Sala:");
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
        gbc_lblNewLabel_1.gridx = 0;
        gbc_lblNewLabel_1.gridy = 0;
        panel.add(lblNewLabel_1, gbc_lblNewLabel_1);

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

        JLabel lblNewLabel = new JLabel("Grupo:");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
        gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 1;
        panel.add(lblNewLabel, gbc_lblNewLabel);

        textFieldGrupo = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 0, 5);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 1;
        panel.add(textFieldGrupo, gbc_textField);
        textFieldGrupo.setColumns(10);

        JButton btnNewButton = new JButton("Buscar");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cargarLista();
            }
        });
        GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
        gbc_btnNewButton.anchor = GridBagConstraints.WEST;
        gbc_btnNewButton.gridx = 2;
        gbc_btnNewButton.gridy = 1;
        panel.add(btnNewButton, gbc_btnNewButton);

        JPanel panel_1 = new JPanel();
        add(panel_1, BorderLayout.EAST);
        panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

        JButton btnNewButton_1 = new JButton("Añadir Concierto");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dlm.addElement(textField.getText());
            }
        });
        btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel_1.add(btnNewButton_1);

        textField = new JTextField();
        panel_1.add(textField);
        textField.setColumns(3);

        JSeparator separator = new JSeparator();
        panel_1.add(separator);

        JButton btnNewButton_2 = new JButton("Eliminar Concierto");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int[] listaAEliminar = listaSalas.getSelectedIndices();
                for (int i = listaAEliminar.length - 1; i >= 0; i--) {
                    dlm.removeElementAt(listaAEliminar[i]);
                }
            }
        });
        btnNewButton_2.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel_1.add(btnNewButton_2);

        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane, BorderLayout.CENTER);

        listaSalas = new JList(dlm);
        listaSalas.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        scrollPane.setViewportView(listaSalas);

        cargarAlInicio();
    }

    private void cargarAlInicio() {
        List<Sala> salas = ControladorSala.getAllSalas();
        for (Sala s : salas) {
            comboBoxSala.addItem(s);
        }

        cargarLista();

    }

    private void cargarLista() {
        dlm.clear();
        List<Concierto> conciertos = ControladorConcierto.filtrarConciertos((Sala) comboBoxSala.getSelectedItem(), textFieldGrupo.getText());
        for (Concierto c : conciertos) {
            dlm.addElement(c.getFecha().toString() + " - " + ControladorGrupo.getGrupoById(c.getGrupo_id()) + " - " + ((Sala) comboBoxSala.getSelectedItem()).getNombre());
        }
    }


}
