import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class MusicRenderer extends JPanel implements ListCellRenderer<Music> {
	private static final long serialVersionUID = 1L;
	private static final Color DEFAULT_BACKGROUND = new JPanel().getBackground();
	private static final Font defaultFont = new Font(new JLabel().getFont().getName(), Font.PLAIN, new JLabel().getFont().getSize());
	
	private TimeLineJPanel timeLine;
	private int infoPadding = 0;
	
	public ArrayList<PatternJPanel> patterns;
	
	public MusicRenderer(TimeLineJPanel timeLine) {
		this.timeLine = timeLine;
		this.patterns = new ArrayList<>();
	}
	
	public void setInfoPadding(int infoPadding) { this.infoPadding = infoPadding; }
	
	@Override
	public Component getListCellRendererComponent(JList<? extends Music> list, Music music, int index,
			boolean isSelected, boolean cellHasFocus) {
		removeAll();
		if(music == null)
			return this;
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		JPanel infoPanel = new JPanel();
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));
		infoPanel.add(Box.createRigidArea(new Dimension(infoPadding, 0)));
		JLabel infoLabel = new JLabel("Pattern " + String.valueOf(index) + " : "
				+ "(vol : " + String.valueOf(music.getVolume()) + ", "
				+ "effect : " + MusicPath.pathNames[music.getMusicPath().getAnimation()] + ", "
				+ "effect duration : " + Util.tickToText(music.getMusicPath().getEffectDuration()) + ")");
		infoLabel.setFont(defaultFont);
		infoPanel.add(infoLabel);
		infoPanel.add(Box.createHorizontalGlue());
		
		if(isSelected) {
			infoPanel.setBackground(Color.LIGHT_GRAY);
		} else {
			infoPanel.setBackground(DEFAULT_BACKGROUND);
		}
		
		add(infoPanel);
		
		PatternJPanel pattern = new PatternJPanel(music, timeLine);
		if(patterns.size() > index)
			patterns.set(index, pattern);
		else
			patterns.add(pattern);
		add(pattern);
		
		return this;
	}
}
