package net.devlab722.ft;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ThreadLocalRandom;

public class FluffyTanuki {

    private final long endOfLineWaitTimeMs;
    private final long endOfCharMaxWaitTimeMs;
    private final long endOfCharMinWaitTimeMs;

    public FluffyTanuki(long endOfLineWaitTimeMs, long endOfCharMinWaitTimeMs, long endOfCharMaxWaitTimeMs) {
        this.endOfLineWaitTimeMs = endOfLineWaitTimeMs;
        this.endOfCharMinWaitTimeMs = endOfCharMinWaitTimeMs;
        this.endOfCharMaxWaitTimeMs = endOfCharMaxWaitTimeMs;
    }


    void readInputStream(InputStream inputStream) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        readLines(r);
    }

    private void readLines(BufferedReader r) throws IOException {
        String line;
        while ((line = r.readLine()) != null) {
            readLine(line);
            waitForDuration(endOfLineWaitTimeMs);
        }
    }

    private void readLine(String line) {
        String[] splitString = line.split("\\s+");
        printWordsAsALine(splitString);
    }

    private void printWordsAsALine(String[] splitString) {
        String spaceIfNeeded;
        for (int i = 0; i < splitString.length; i++) {
            if (i != splitString.length - 1) {
                spaceIfNeeded = " ";
            } else {
                spaceIfNeeded = "";
            }
            printCharsOfWord(splitString[i] + spaceIfNeeded);
        }
        System.out.println();
    }

    private void printCharsOfWord(String word) {
        word.chars().forEach(c -> {
            System.out.print((char) c);
            waitForDuration(getRandomWaitTimeMsBetweenChars());
        });
    }

    private void waitForDuration(long durationInMillis) {
        try {
            Thread.sleep(durationInMillis);
        } catch (InterruptedException shouldNeverHappen) {
            shouldNeverHappen.printStackTrace();
        }
    }

    private long getRandomWaitTimeMsBetweenChars() {
        return ThreadLocalRandom.current().nextLong(endOfCharMinWaitTimeMs, endOfCharMaxWaitTimeMs + 1);
    }

    public static void main(String[] args) throws IOException {
        FluffyTanuki ft = new FluffyTanuki(400L, 60L, 70L);
        ft.readInputStream(new FileInputStream(new File("mail-presentation-draft.txt")));
    }
}

@@ -1038,24 +1038,32 @@
         read
+AndPrint
 Line(line);%0A
@@ -1062,16 +1062,16 @@
 (line);%0A
-
         
@@ -1154,496 +1154,61 @@
 read
-Line(String line) %7B%0A        String%5B%5D splitString = line.split(%22%5C%5Cs+%22);%0A        printWordsAsALine(splitString);%0A    %7D%0A%0A    private void printWordsAsALine(String%5B%5D splitString) %7B%0A        String spaceIfNeeded;%0A        for (int i = 0; i %3C splitString.length; i++) %7B%0A            if (i != splitString.length - 1) %7B%0A                spaceIfNeeded = %22 %22;%0A            %7D else %7B%0A                spaceIfNeeded = %22%22;%0A            %7D%0A            printCharsOfWord(splitString%5Bi%5D + spaceIfNeeded);%0A        %7D
+AndPrintLine(String line) %7B%0A        printChars(line);
 %0A   
@@ -1233,32 +1233,32 @@
 intln();%0A    %7D%0A%0A
+
     private void
@@ -1272,14 +1272,8 @@
 hars
-OfWord
 (Str

read and print a full line in a single pass