package vermilion;

import java.awt.event.KeyEvent;

/**
 * A tracker column that represents a command value as a command A-Z and a value
 * 000 to 999.
 */
public class JTrackerColumnCommand extends JTrackerColumn
{
    /**
     * Constructs a new column for representing a command parameter.
     */
    public JTrackerColumnCommand(String colName, String paramName, PatternNode pattern, PatternTrackNode commands, int trackNumber)
    {
        super(colName, paramName, pattern, 4, commands, trackNumber);
    }

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

        if (command != null)
        {
            return valueToStr(command);
        }
        else return "----";
    }

    /**
     * Returns the value of a command as a string.
     */
    private String valueToStr(ParameterCommand command)
    {
        String str = String.valueOf(command.argument);
        if (str.length() == 1) {
            str = "00" + str;
        } else if (str.length() == 2) {
            str = "0" + str;
        }
        return (char) ('A' + command.command) + str;
    }

    /**
     * Interprets keypresses and creates or modifies command 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;
        }

        char keyChar = key.getKeyChar();
        keyChar = Character.toUpperCase(keyChar);
        
        if ((keyChar >= '0' && keyChar <= '9' && cursorPos > 0) || (keyChar >= 'A' && keyChar <= 'Z' && cursorPos == 0))
        {
            ParameterCommand newNode = new ParameterCommand(0, 0);
            ParameterCommand oldNode = (ParameterCommand) commandList.commands.get(rowNum);

            char[] commandChars = valueToStr(newNode).toCharArray();
            if (oldNode != null) commandChars = valueToStr(oldNode).toCharArray();

            commandChars[cursorPos] = keyChar;
            String newVal = new String(commandChars);
            
            newNode.command = commandChars[0] - 'A';
            newNode.argument = Integer.parseInt(newVal.substring(1));

            commandList.commands.put(rowNum, newNode);

            return true;
        }

        return false;
    }

    /**
     * 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
        if (cursorPos > 1)
        {
            return "Command value for " + parameterName + " - Keys 1, 2, 3, 4...";
        }
        else
        {
            return "Command name for " + parameterName + " - Keys A, B, C, D...";
        }
    }
}