package com.jscape.inet.terminal;

/**
 * Represents terminal screen row as array of <code>Cell</code>s.
 * Corresponding to VT100 User's Manual terminal row can have
 * doubled width or doubled high (or both).
 */
public class Row {

    private final Cell[] cells;
    private boolean doubleWidth = false;
    private boolean doubleHigh = false;

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

    /**
     * Creates <code>Row</code> object giving quantity of cells.
     *
     * @param colNum number of cells.
     */
    public Row(int colNum) {
        if (colNum <= 0) {
            throw new IllegalArgumentException("Row.constructor(): wrong dimension");
        }

        this.cells = new Cell[colNum];

        for (int i = 0; i < this.cells.length; ++i) {
            this.cells[i] = new Cell();
        }
    }

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

    /**
     * Copy constructor.
     *
     * @param row <code>Row</code> object to be copied.
     */
    public Row(Row row) {
        if (row == null) {
            throw new NullPointerException("Row.constructor(): null passed");
        }

        this.doubleWidth = row.doubleWidth;
        this.doubleHigh = row.doubleHigh;
        this.cells = new Cell[row.cells.length];

        for (int i = 0; i < this.cells.length; ++i) {
            this.cells[i] = new Cell(row.cells[i]);
        }
    }

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

    /**
     * Gives access to specified cell.
     *
     * @param index number of cell to access.
     *
     * @return <code>Cell</code> object with giving number.
     */
    public Cell cellAt(int index) {
        return this.cells[index];
    }

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

    /**
     * Checks if current row has doubled width.
     *
     * @return true if current row has doubled width.
     */
    public boolean isDoubleWidth() {
        return doubleWidth;
    }

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

    /**
     * Sets current row to has doubled width.
     *
     * @param doubleWidth true if current row must have doubled width.
     */
    public void setDoubleWidth(boolean doubleWidth) {
        this.doubleWidth = doubleWidth;
    }

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

    /**
     * Checks if current row has doubled high.
     *
     * @return true if current row has doubled high.
     */
    public boolean isDoubleHigh() {
        return doubleHigh;
    }

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

    /**
     * Sets current row to has doubled high.
     *
     * @param doubleHigh true if current row must have doubled high.
     */
    public void setDoubleHigh(boolean doubleHigh) {
        this.doubleHigh = doubleHigh;
    }

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

    /**
     * Represents current row as array of <code>Cell</code> objects.
     *
     * @return array of <code>Cell</code> objects.
     */
    public Cell[] toCellArray() {
        return this.cells;
    }

    //----------------------------------------------------------------------------
    public String toString() {
        StringBuffer result = new StringBuffer();

        for (int i = 0; i < this.cells.length; ++i) {
            char ch = this.cells[i].getValue();

            if (ch > 0) {
                result.append(ch);
            }
        }

        return result.toString();
    }

}