import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import javax.swing.JFileChooser;

public class AutomaticPoet {
    public static void main(String[] args) {
        // Load the corpus
        File corpusFile = selectFile("Select Corpus File");
        System.out.println("Loading corpus...");
        String corpus = slurp(corpusFile);
        System.out.println("Corpus loaded.");

        // Determine the value of n
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            int n = readInt(console, "n? ");
            if(n >= 1 && n <= 10) {
                String result = generate(corpus, n);
                System.out.println("Generated string: \"" + result + "\"");
            } else {
                System.out.println("Please enter integer between 1 and 10");
            }
        }
    }

    // Displays a dialog box for selecting a file, returning what is
    // selected. Canceling the dialog terminates the program.
    private static File selectFile(String dialogTitle) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(dialogTitle);
        int result = chooser.showDialog(null, "Select");
        if(result != JFileChooser.APPROVE_OPTION) { // dialog canceled
            System.err.println("File selection canceled; program complete");
            System.exit(0);
            return null; // should never happen
        }
        return chooser.getSelectedFile();
    }

    // Slurps the contents of the indicated file into a String.
    // In the event of an I/O error, the program terminates.
    private static String slurp(File corpusFile) {
        StringBuilder ret = new StringBuilder();
        char[] buf = new char[128];
        try {
            FileReader in = new FileReader(corpusFile);
            int nbytes = in.read(buf);
            while(nbytes > 0) {
                ret.append(buf, 0, nbytes);
                nbytes = in.read(buf);
            }
            in.close();
            return ret.toString();
        } catch(IOException e) {
            System.err.println("Could not open dictionary.");
            System.exit(0);
            return null; // will never happen
        }
    }

    // Reads a single integer from the user and returns it. If the user
    // enters a non-integer, the method returns Integer.MIN_VALUE.
    private static int readInt(BufferedReader console, String prompt) {
        System.out.print(prompt);
        try {
            String line = console.readLine();
            if(line == null) System.exit(0);
            return Integer.parseInt(line);
        } catch(IOException e) {
            e.printStackTrace();
            System.exit(0);
            return -1;
        } catch(NumberFormatException e) {
            return Integer.MIN_VALUE;
        }
    }

    // Generates a random string with 100 characters based on corpus.
    // (It may have less than 100 characters occassionally.)
    private static String generate(String corpus, int n) {
        return corpus.substring(0, 100);
    }
}
