package com.jdbernard.teammaker

import java.awt.Color
import java.awt.Component
import javax.swing.EmptyBorder
import javax.swing.JLabel
import javax.swing.JList
import javax.swing.JPanel
import javax.swing.LineBorder
import javax.swing.ListCellRenderer

public class PlayerListCellRenderer extends JPanel implements ListCellRenderer {

    boolean showStats
    boolean colored

    private PlayerChooser chooser
    private JLabel nameLabel
    private JLabel statsLabel
    private static def linedBorder = new LineBorder(Color.BLACK)
    private static def emptyBorder = new EmptyBorder(1)

    private def colors = [Color.getHSBColor(0.32f, 1f, 1f),
        Color.getHSBColor(0.24f,1f,1f), Color.getHSBColor(0.16f, 1f, 1f),
        Color.getHSBColor(0.08f, 1f, 1f), Color.getHSBColor(0.0f, 1f, 1f)]

    public PlayerListCellRenderer(Map params) {
        showStats = params.showStats ?: false
        colored = params.colored ?: false
        chooser = params.chooser ?: WeightedChooser.getInstance()

        setLayout(new BorderLayout())
        setOpaque(colored)

        nameLabel = new JLabel()
        nameLabel.opaque = false
        nameLabel.horizontalAlignment = JLabel.LEADING
        add(nameLabel, BorderLayout.WEST)

        if (showStats) {
            statsLabel = new JLabel()
            statsLabel.horizontalAlignment = JLabel.TRAILING,
            statsLabel.opaque = false
            add(statsLabel, BorderLayout.EAST)
        }
    }

    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {

        assert value instanceof Player

        nameLabel.setText(value.name)

        if (isSelected) setBorder(linedBorder)
        else setBorder(emptyBorder)
        
        if (showStats) 
            statsLabel.text = value.gamesSat

        if (colored) {
            def c = colors[Math.min(4, value.gamesSat)]
            setBackground(c)
        }

        return this
    }
}
