/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.parser;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * This ObservedInputStream will update its registered listeners about the reading process of the underlying file.
 * Every time when the underlying input stream is read the registered listeners will be informed and updated with the
 * new progress.
 * 
 * @author Marcel Bruse
 */
public class ObservedInputStream extends FilterInputStream {

	/** List of registered progress listeners. */
	private ArrayList<InputStreamListener> listeners = new ArrayList<>();
	
	/** The number of bytes of the file to be read. */
	private long fileLength;
	
	/** The number of bytes read so far. */
	private long location;
	
	/**
	 * Instantiates the ObservedInputStream with a given file handle.
	 * 
	 * @param file The file that should be loaded.
	 * @throws FileNotFoundException If there is on file, you will get some of this.
	 */
	public ObservedInputStream(File file) throws FileNotFoundException {
		super(new BufferedInputStream(new FileInputStream(file)));
		fileLength = file.length();
	}
	
	public ObservedInputStream(String fileName) throws FileNotFoundException {
		this(new File(fileName));
	}
	
	public ObservedInputStream(InputStream in, long fileLength) {
		super(new BufferedInputStream(in));
		this.fileLength = fileLength;
	}

	/**
	 * Calls all the registered listeners if the file loading process makes any progress.
	 * It calculates and passes the progress in percent. 
	 */
	private void updateProgress() {
		for (InputStreamListener l : listeners) {
			l.updateProgress((float) location / fileLength);
		}
	}
	
	/**
	 * Calls super and counts the number of read bytes. 
	 * 
	 * @see FilterInputStream
	 */
	@Override
	public int read() throws IOException {
		final int data = super.read();
		if (data != -1) {
			location++;
			updateProgress();
		}
		return data;
	}
	
	/**
	 * Calls super and counts the number of read bytes. 
	 * 
	 * @see FilterInputStream
	 */
	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		final int byteCount = super.read(b, off, len);
		if (byteCount != -1) {
			location += byteCount;
			updateProgress();
		}
		return byteCount;
	}
	
	/**
	 * Registers listeners. Every registered listener will get informed if the file loading process makes any
	 * progress.
	 *  
	 * @param ps The progress listener to be registered.
	 */
	public void addListener(InputStreamListener ps) {
		listeners.add(ps);
	}

}
