package com.jscape.inet.terminal.commands;

import com.jscape.inet.terminal.Terminal;

/**
 * CUD -- Cursor Down.
 * The CUD sequence moves the active position downward without
 * altering the column position. The number of lines moved is determined
 * by the parameter. If the parameter value is zero or one,
 * the active position is moved one line downward.
 * If the parameter value is n, the active position is moved n
 * lines downward. In an attempt is made to move the cursor below the
 * bottom margin, the cursor stops at the bottom margin.
 */
public class CUDCommand
        implements ControlCommand {

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

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

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

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

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

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

    /**
     * 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().moveDown(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());
        }
    }
}
