package com.jscape.inet.terminal.commands;

import com.jscape.inet.terminal.Screen;
import com.jscape.inet.terminal.Screen.Cursor;
import com.jscape.inet.terminal.Terminal;

/**
 * Move the cursor to the next tab stop, or to the right margin if no further
 * tab stops are present on the line.
 */
public class HTCommand
        implements ControlCommand {

    public static final String CANONICAL_FORM = "\011";
    private static final HTCommand INSTANCE = new HTCommand();

    //----------------------------------------------------------------------------

    /**
     * Returns one and only one instance of this type.
     *
     * @return one and only one instance of this type.
     */
    public static HTCommand getInstance() {
        return INSTANCE;
    }

    //----------------------------------------------------------------------------

    /**
     * Private constructor to prevent uncontrolled creation.
     */
    private HTCommand() {
    }

    //----------------------------------------------------------------------------

    /**
     * Executes command giving parameters and <code>Terminal</code> object.
     *
     * @param params   array of <code>int</code> command parametrs.
     * @param terminal target <code>Terminal</code> object.
     */
    public void execute(int[] params, Terminal terminal) {
        Screen s = terminal.getScreen();
        Cursor cursor = s.getCursor();
        cursor.moveRight(1);

        while (!stop(cursor, s.getMaxCol())) {
            cursor.moveRight(1);
        }
    }

    //----------------------------------------------------------------------------

    /**
     * Sends command to remote host.
     *
     * @param params   command parameters.
     * @param terminal target <code>Terminal</code> object to send command.
     */
    public void send(int[] params, Terminal terminal) {
        terminal.send(CANONICAL_FORM);
    }

    //----------------------------------------------------------------------------
    private boolean stop(Cursor cursor, int colNum) {
        return (cursor.column() == colNum || cursor.getCell().isTabstop());
    }
}
