
using System;
using System.Runtime.CompilerServices;

namespace lanterna.graphics {
    /**
     * This implementation of TextGraphics will take a 'proper' object and composite a view on top of it, by using a
     * top-left position and a size. Any attempts to put text outside of this area will be dropped.
     * @author Martin
     */
    class SubTextGraphics : AbstractTextGraphics {
        private TextGraphics underlyingTextGraphics;
        private TerminalPosition topLeft;
        private TerminalSize writableAreaSize;

        internal SubTextGraphics(TextGraphics underlyingTextGraphics, TerminalPosition topLeft, TerminalSize writableAreaSize) {
            this.underlyingTextGraphics = underlyingTextGraphics;
            this.topLeft = topLeft;
            this.writableAreaSize = writableAreaSize;
        }

        private TerminalPosition project(int column, int row) {
            return topLeft.withRelative(column, row);
        }

    	//@Override
        public override TextGraphics setCharacter(int columnIndex, int rowIndex, TextCharacter textCharacter) {
            TerminalSize writableArea = getSize();
            if (columnIndex < 0 || columnIndex >= writableArea.getColumns() ||
                rowIndex < 0 || rowIndex >= writableArea.getRows()) {
                return this;
            }
            TerminalPosition projectedPosition = project(columnIndex, rowIndex);
            underlyingTextGraphics.setCharacter(projectedPosition, textCharacter);
            return this;
        }

        //@Override
        public override TerminalSize getSize() {
            return writableAreaSize;
        }

        //@Override
        public override TextCharacter getCharacter(int column, int row) {
            TerminalPosition projectedPosition = project(column, row);
            return underlyingTextGraphics.getCharacter(projectedPosition.getColumn(), projectedPosition.getRow());
        }
    }
}
