package de.baumato.loc.printer;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.stream.Stream;

final class Animator extends Thread {

	private static final String SYMOBLS = "|/⎯\\";

	@Override
	public void run() {
		Iterator<Character> infiniteSymbols = Stream.iterate(0, i -> (i + 1) % SYMOBLS.length())
			.map(i -> SYMOBLS.charAt(i))
			.iterator();
		while (!isInterrupted()) {
			try {
				System.out.write(toProgress(infiniteSymbols));
				Thread.sleep(100);
			} catch (IOException e) {
				interrupt();
			} catch (InterruptedException e) {
				interrupt();
			}
		}
	}

	private static byte[] toProgress(Iterator<Character> infiniteSymbols) {
		char c = infiniteSymbols.next().charValue();
		String progress = c + " \r";
		return progress.getBytes(StandardCharsets.UTF_8);
	}

	public void startAnimation() {
		start();
	}

	public void stopAnimation() {
		if (!isAlive()) {
			return;
		}
		interrupt();
		try {
			this.join();
		} catch (InterruptedException e) {
			interrupt();
		}
		try {
			System.out.write("\r".getBytes());
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}
}

@@ -416,30 +416,23 @@
 map(
-i -%3E 
 SYMOBLS
-.
+::
 charAt
-(i)
 )%0A%09%09
@@ -585,42 +585,10 @@
 ion 
-e) %7B%0A%09%09%09%09interrupt();%0A%09%09%09%7D catch (
+%7C 
 Inte

support easier reading by combining catch blocks and by using method ref