package tutorialJava.examenes.examen20250509.vista;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.util.List;

import javax.swing.JTextField;

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

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PanelFiltradoConciertos extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField jtfNombreGrupo;
	private JTextField jtfAgregar;
	JComboBox<Sala> jcbSalas;
	DefaultListModel<String> dlmConciertos = 
			new DefaultListModel<String>();
	JList jlistConciertos;

	/**
	 * Create the panel.
	 */
	public PanelFiltradoConciertos() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblNewLabel = new JLabel("Filtrado de conciertos");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.gridwidth = 3;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		add(lblNewLabel, gbc_lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Sala del concierto");
		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 = 1;
		add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		jcbSalas = new JComboBox<Sala>();
		GridBagConstraints gbc_jcbSalas = new GridBagConstraints();
		gbc_jcbSalas.insets = new Insets(0, 0, 5, 5);
		gbc_jcbSalas.fill = GridBagConstraints.HORIZONTAL;
		gbc_jcbSalas.gridx = 1;
		gbc_jcbSalas.gridy = 1;
		add(jcbSalas, gbc_jcbSalas);
		
		JLabel lblNewLabel_2 = new JLabel("Nombre del grupo:");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 2;
		add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		jtfNombreGrupo = new JTextField();
		GridBagConstraints gbc_jtfNombreGrupo = new GridBagConstraints();
		gbc_jtfNombreGrupo.insets = new Insets(0, 0, 5, 5);
		gbc_jtfNombreGrupo.fill = GridBagConstraints.HORIZONTAL;
		gbc_jtfNombreGrupo.gridx = 1;
		gbc_jtfNombreGrupo.gridy = 2;
		add(jtfNombreGrupo, gbc_jtfNombreGrupo);
		jtfNombreGrupo.setColumns(10);
		
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				filtrar();
			}
		});
		GridBagConstraints gbc_btnBuscar = new GridBagConstraints();
		gbc_btnBuscar.insets = new Insets(0, 0, 5, 0);
		gbc_btnBuscar.gridx = 2;
		gbc_btnBuscar.gridy = 2;
		add(btnBuscar, gbc_btnBuscar);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridheight = 3;
		gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 3;
		add(scrollPane, gbc_scrollPane);
		
		jlistConciertos = new JList(dlmConciertos);
		scrollPane.setViewportView(jlistConciertos);
		
		JButton btnEliminar = new JButton("Eliminar");
		btnEliminar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				eliminarSeleccionados();
			}
		});
		GridBagConstraints gbc_btnEliminar = new GridBagConstraints();
		gbc_btnEliminar.anchor = GridBagConstraints.SOUTH;
		gbc_btnEliminar.insets = new Insets(0, 0, 5, 0);
		gbc_btnEliminar.gridx = 2;
		gbc_btnEliminar.gridy = 3;
		add(btnEliminar, gbc_btnEliminar);
		
		jtfAgregar = new JTextField();
		GridBagConstraints gbc_jtfAgregar = new GridBagConstraints();
		gbc_jtfAgregar.insets = new Insets(0, 0, 5, 0);
		gbc_jtfAgregar.fill = GridBagConstraints.HORIZONTAL;
		gbc_jtfAgregar.gridx = 2;
		gbc_jtfAgregar.gridy = 4;
		add(jtfAgregar, gbc_jtfAgregar);
		jtfAgregar.setColumns(10);
		
		JButton btnAgregar = new JButton("Agregar");
		btnAgregar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (jtfAgregar.getText().length() > 0) {
					dlmConciertos.addElement(jtfAgregar.getText());
				}
			}
		});
		GridBagConstraints gbc_btnAgregar = new GridBagConstraints();
		gbc_btnAgregar.gridx = 2;
		gbc_btnAgregar.gridy = 5;
		add(btnAgregar, gbc_btnAgregar);

		
		cargaTodasLasSalas();
	}

	
	/**
	 * 
	 */
	private void cargaTodasLasSalas() {
		List<Sala> salas = ControladorSala.findAll();
		for (Sala s : salas) {
			jcbSalas.addItem(s);
		}
	}

	/**
	 * 
	 */
	private void filtrar() {
		Sala salaSeleccionada = (Sala) jcbSalas.getSelectedItem();
		List<String> conciertosFiltrados =
				ControladorConcierto.findConciertosPorSalaYNombreGrupo(
						salaSeleccionada.getId(),
						jtfNombreGrupo.getText());
		dlmConciertos.removeAllElements();
		dlmConciertos.addAll(conciertosFiltrados);
	}
	
	/**
	 * 
	 */
	private void eliminarSeleccionados() {
		int selectedIndices[] = this.jlistConciertos.getSelectedIndices();
		for (int  i = selectedIndices.length - 1; i >= 0; i--) {
			System.out.println("Selected index: " + selectedIndices[i]);
			dlmConciertos.removeElementAt(selectedIndices[i]);
		}
	}
}







