package vermilion;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
 * Returns the label controls that make up cells in the JPatternTable.
 */
public class JPatternTableCellRenderer implements TableCellRenderer
{
    // Colors we use to show different cell states.
    private Color normalColor = new Color(255, 255, 255);
    private Color timeSigColor = new Color(230, 230, 255);
    private Color selectedColor = new Color(230, 255, 230);
    private Color cursorColor = new Color(255, 230, 230);

    // Pointer to the table we are making labels for.
    private JPatternTable pTable;

    private JLabel[][] labelCache = new JLabel[0][];

    // We accept a pointer to something we know is a JPatternTable for the
    // owner, which makes the high coupling below slightly less dodgy.
    JPatternTableCellRenderer(JPatternTable t)
    {
        pTable = t;
    }

    /**
     * This is where we return the component for rendering a cell. We handle
     * several things here including selection and cursor as well as content.
     */
    @Override
    public Component getTableCellRendererComponent(JTable ignored, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        // TODO: Cache these labels for the current pattern?
        if (labelCache[column][row] != null) return labelCache[column][row];

        String text = (String) value;
        JLabel label = new JLabel(text, JLabel.CENTER);
        label.setOpaque(true);
        label.setFont(pTable.getFont());

        // For some reason asking JTable what cell is selected simply does not
        // work. We have to manually check which columns are selected.
        boolean colSelect = false;
        for (int x = 0; x < pTable.getSelectedColumnCount(); x++)
        {
            if (pTable.getSelectedColumns()[x] == column) colSelect = true;
        }

        if (hasFocus)
        {
            label.setBackground(cursorColor);
            
            // HACK: Hackity hack hack, hackity hack hack.
            if (pTable.columnPosition >= pTable.columns[column].columnWidth)
            {
                pTable.columnPosition = pTable.columns[column].columnWidth - 1;
            }

            // TODO: Is this necessary? (UPDATE: Yes) It isn't wise!
            pTable.gridX = column;
            pTable.gridY = row;

            // TODO: Better cursor.
            char[] arr = text.toCharArray();
            arr[pTable.columnPosition] = '#';
            label.setText(new String(arr));

        }
        // Here is where we check the cell is selected.
        else if (isSelected && colSelect)
        {
            label.setBackground(selectedColor);
        }
        else
        {
            // Read time sig of pattern and highlight each bar's first beat.
            if (row % pTable.currPattern.timeSig == 0)
            {
                label.setBackground(timeSigColor);
            }
            else
            {
                label.setBackground(normalColor);
            }
        }

        //labelCache[column][row] = label;
        return label;
    }

    /**
     * Clears the cache of label controls for the table, not yet implemented.
     */
    public void clearCache(int x, int y)
    {
        labelCache = new JLabel[x][y];
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < x; j++)
            {
                labelCache[i][j] = null;
            }
        }
    }
}