package com.jscape.inet.terminal.commands;

import com.jscape.inet.terminal.Terminal;

/**
 * CUF -- Cursor Forward.
 * The CUF sequence moves the active position to the right.
 * The distance moved is determined by the parameter.
 * A parameter value of zero or one moves the active position
 * one position to the right. A parameter value of n moves the
 * active position n positions to the right.
 * If an attempt is made to move the cursor to the right of the right margin,
 * the cursor stops at the right margin.
 */
public class CUFCommand
        implements ControlCommand {

    public static final String CANONICAL_FORM = "\033[C";
    private static final CUFCommand INSTANCE = new CUFCommand();

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

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

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

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

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

    /**
     * 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) {
        int dst = (params != null && params.length > 0) ? params[0] : 1;
        dst = (dst == 0) ? 1 : dst;
        terminal.getScreen().getCursor().moveRight(dst);
    }

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

    /**
     * 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) {
        if (params == null || params.length == 0) {
            terminal.send(CANONICAL_FORM);
        } else {
            StringBuffer b =
                    new StringBuffer(CANONICAL_FORM).insert(2, params[0]);
            terminal.send(b.toString());
        }
    }
}
