import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.Random;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Generate {
    /** Given a length, creates an array holding the numbers to be
     * saved into the file. */
    private static int[] createArray(int length) {
        int[] data = new int[length];
        for(int i = 0; i < data.length; i++) data[i] = i;

        Random rand = new Random();
        for(int i = 2; i <= data.length; i++) {
            int j = rand.nextInt(i);
            int t = data[i - 1];
            data[i - 1] = data[j];
            data[j] = t;
        }

        return data;
    }

    /** Saves the numbers in the given array into the file. */
    private static void saveArray(int[] data, File file) {
        // Open the file.
        PrintWriter out;
        try {
            out = new PrintWriter(new FileWriter(file));
        } catch(IOException ex) {
            System.err.println("Error opening " + file.getName() + ":");
            ex.printStackTrace();
            return;
        }

        // Write the numbers into it.
        for(int i = 0; i < data.length; i++) {
            out.println(data[i]);
        }
        if(out.checkError()) {
            System.err.println("Error while writing to file.");
            return;
        }

        // Close the file.
        out.close();
        if(out.checkError()) {
            System.err.println("Error while closing file.");
            return;
        }
    }

    /** Determine the file and length, and then use the above
     * two methods to generate and save the numbers. */
    public static void main(String[] args) {
        File file;
        int length;

        if(args.length == 0) {
            JFileChooser chooser = new JFileChooser();
            chooser.setDialogTitle("Select Output File");
            int action = chooser.showSaveDialog(null);
            if(action != JFileChooser.APPROVE_OPTION) {
                System.exit(0);
                return;
            }
            file = chooser.getSelectedFile();

            String prompt = "How many numbers in the array?";
            while(true) {
                try {
                    String str = JOptionPane.showInputDialog(null,
                            prompt, "Input Array Length",
                            JOptionPane.QUESTION_MESSAGE);
                    if(str == null) { // user pressed Cancel
                        System.exit(0);
                        return;
                    }

                    length = Integer.parseInt(str);
                    if(length > 0) {
                        break;
                    } else {
                        prompt = "Length must be positive.";
                    }
                } catch(NumberFormatException ex) {
                    prompt = "Length must be a valid integer.";
                }
            }
        } else if(args.length == 2) {
            file = new File(args[0]);
            try {
                length = Integer.parseInt(args[1]);
                if(length <= 0) {
                    System.err.println("Length must be positive.");
                    System.exit(0);
                    return;
                }
            } catch(NumberFormatException ex) {
                System.err.println("Length must be valid integer.");
                System.exit(0);
                return;
            }
        } else {
            System.out.println("usage: java Generate filename #");
            return;
        }

        int[] data = createArray(length);
        saveArray(data, file);
    }
}
