package ru.nbch.indicsearchservice.util;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.util.Objects;

public class MovableBufferedReader implements Closeable {
    private final BufferedReader reader;
    private String currentLine;

    public MovableBufferedReader(BufferedReader reader) {
        this.reader = reader;
        try {
            this.currentLine = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String nextLine() {
        try {
            this.currentLine = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return this.currentLine;
    }

    public BufferedReader getReader() {
        return reader;
    }

    public String getCurrentLine() {
        return currentLine;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        MovableBufferedReader that = (MovableBufferedReader) o;
        return Objects.equals(reader, that.reader) && Objects.equals(currentLine, that.currentLine);
    }

    @Override
    public int hashCode() {
        return Objects.hash(reader, currentLine);
    }

    @Override
    public String toString() {
        return "MovableBufferedReader{" +
            "currentLine='" + currentLine + '\'' +
            '}';
    }

    @Override
    public void close() throws IOException {
        this.reader.close();
    }
}
