package vermilion;

import java.awt.event.KeyEvent;
import java.util.ArrayList;

/**
 * A tracker column that allows cueing patterns for machines on the master
 * timeline.
 */
public class JTrackerColumnMachine extends JTrackerColumn
{
    // TODO: Internationalise and read this array from somewhere.
    private char[] patternKeys = new char[] {
        '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
        'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
        'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
        'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/'
    };

    // The name of the machine this column is showing patterns for, not to be
    // confused with the ID of the machine instance, stored in parameterName.
    private String machineName;

    /**
     * Constructs a new column for pattern cues. We pass in the machine as
     * well as the pattern. We store only the ID of the machine on the node,
     * the name of the machine is used to find patterns.
     */
    public JTrackerColumnMachine(String colName, String machID, PatternNode pattern, PatternTrackNode commands, String machName, int trackNumber)
    {
        super(colName, machID, pattern, 10, commands, trackNumber);
        machineName = machName;
    }

    /**
     * Returns pattern cue entries from the pattern for display.
     */
    @Override
    public String getEntry(int rowNum)
    {
        ParameterPattern command = (ParameterPattern) commandList.commands.get(rowNum);

        if (command != null)
        {
            String str = command.value;
            while (str.length() < 10) str += " ";
            if (str.length() > 10) str = str.substring(0, 10);
            return str;
        }
        else return "----------";
    }

    /**
     * Interprets keypresses and inserts pattern cues.
     */
    @Override
    public boolean setEntry(int rowNum, int cursorPos, KeyEvent key)
    {
        if (key.getKeyCode() == KeyEvent.VK_BACK_SPACE ||
            key.getKeyCode() == KeyEvent.VK_DELETE)
        {
            commandList.commands.remove(rowNum);
            return true;
        }
        
        int index = -1;
        for (int x = 0; x < patternKeys.length; x++)
        {
            if (key.getKeyChar() == patternKeys[x]) index = x;
        }

        if (index == -1) return false;

        ArrayList<UsefulFileObject> patterns = Main.document.getChildren("Patterns/" + machineName);
        if (patterns == null) return false;
        if (index >= patterns.size()) return false;

        PatternNode node = (PatternNode) patterns.get(index);
        ParameterPattern newNode = new ParameterPattern(parameterName, node.patternID);

        commandList.commands.put(rowNum, newNode);

        return true;
    }

    /**
     * Returns a helper/info display message for the current column, cursor
     * position and cue if one exists.
     */
    @Override
    public String getCursorDescription(int rowNum, int cursorPos)
    {
        // TODO: Description of entry if any, cursor pos.
        return "Patterns for " + parameterName + " - Keys 1-0, q-p, a-l...";
    }
}