package terminal;

import java.util.ArrayList;
import java.util.List;

public class TerminalHistory {

    private List<String> history;
    private int lineIndex;

    public TerminalHistory() {
        this.history = new ArrayList<>();
        this.history.add("");
        this.lineIndex = 0;
    }

    public void addLine(String line) {
        history.set(lastIndex(), line);
        history.add("");
        lineIndex = lastIndex();
    }

    private int lastIndex() {
        return history.size() - 1;
    }

    public void updateCurrentLine(String line) {
        history.set(lineIndex, line);
    }

    public String getPreviousLine() {
        if (isBeginning())
            return history.get(lineIndex);

        return history.get(--lineIndex);
    }

    public boolean isBeginning() {
        return lineIndex == 0;
    }

    public String getNextLine() {
        if (isEnd())
            return history.get(lineIndex);

        return history.get(++lineIndex);
    }

    public boolean isEnd() {
        return lineIndex == lastIndex();
    }

}
