import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
//-----------------------------------------------
public class GridPanel extends JPanel {
	private InformationPanel infoPanel;
	private ArrayList<Movie> movies;
	private JPanel cards;
	private int n = 0;

	public GridPanel(String s) {
		movies = new ArrayList<>();
		TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), s);
		title.setTitleJustification(TitledBorder.CENTER);
		title.setTitleFont(new Font(getFont().getName(), Font.BOLD, 18));
		setBorder(title);
		setLayout(new GridBagLayout());
	}
	public void addCards(JPanel c, InformationPanel ip) {
		cards = c;
		infoPanel = ip;
	}
	public void addItems(ArrayList<Movie> ms) {
		movies = ms;
		try {
			for (Movie m : movies) {
				ImageIcon img = m.getPoster();
				JButton btn = new JButton(img);
				btn.setPreferredSize(new Dimension(img.getIconWidth(), img.getIconHeight()));
				btn.setToolTipText(m.getName());

				GridBagConstraints c = new GridBagConstraints();
				c.weightx = 0.5;
				c.gridx = n % 4;
				c.gridy = n / 4;
				if (n / 4 > 0)
					c.insets = new Insets(6, 0, 0, 0);
				n++;

				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						CardLayout cardLayout = (CardLayout) cards.getLayout();
						cardLayout.show(cards, "Panel informacion");
						infoPanel.setMovie(m);
					}
				});
				this.add(btn, c);
			}
		} catch (Exception ex) {}
	}
	public void addRigidArea(int i) {
		GridBagConstraints c = new GridBagConstraints();
		c.weightx = 0.5;
		c.gridx = n%4;
		c.gridy = n/4;
		if(n/4 > 0)
			c.insets = new Insets(10,0,0,0);
		this.add(Box.createRigidArea(new Dimension(0,i)), c);
	}
}