package org.icij.extract.cli;

import me.tongfei.progressbar.ProgressBar;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;

import org.icij.events.Monitorable;
import org.icij.events.listeners.ConsoleProgressListener;
import org.icij.extract.cli.tasks.HelpTask;
import org.icij.extract.cli.tasks.VersionTask;
import org.icij.extract.tasks.*;
import org.icij.task.DefaultTask;
import org.icij.task.DefaultTaskFactory;
import org.icij.task.MonitorableTask;
import org.icij.task.transformers.CommonsTransformer;

import java.util.Arrays;

/**
 * The main class for running the commandline application.
 *
 * @author Matthew Caruana Galizia <mcaruana@icij.org>
 * @since 1.0.0-beta
 */
public class Main {

	public static final DefaultTaskFactory taskFactory = new DefaultTaskFactory();

	/**
	 * Attempts to parse the given commandline arguments and executes the appropriate runner, exiting with an
	 * appropriate status.
	 *
	 * @param args the commandline arguments
	 */
	public static void main(final String[] args) {
		taskFactory.addTask("clean-report", CleanReportTask.class);
		taskFactory.addTask("commit", CommitTask.class);
		taskFactory.addTask("copy", CopyTask.class);
		taskFactory.addTask("delete", DeleteTask.class);
		taskFactory.addTask("dump-queue", DumpQueueTask.class);
		taskFactory.addTask("dump-report", DumpReportTask.class);
		taskFactory.addTask("load-queue", LoadQueueTask.class);
		taskFactory.addTask("load-report", LoadReportTask.class);
		taskFactory.addTask("queue", QueueTask.class);
		taskFactory.addTask("rehash", RehashTask.class);
		taskFactory.addTask("replace-report", ReplaceReportTask.class);
		taskFactory.addTask("rollback", RollbackTask.class);
		taskFactory.addTask("spew", SpewTask.class);
		taskFactory.addTask("tag", TagTask.class);
		taskFactory.addTask("wipe-queue", WipeQueueTask.class);
		taskFactory.addTask("wipe-report", WipeReportTask.class);
		taskFactory.addTask("help", HelpTask.class);
		taskFactory.addTask("version", VersionTask.class);

		try {
			parse(args);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(2);
			return;
		}

		System.exit(0);
	}

	private static void parse(final String[] args) throws Exception {
		if (null == args || 0 == args.length) {
			new HelpTask().run();
			return;
		}

		final String command = args[0];
		final DefaultTask task;

		try {
			task = taskFactory.getTask(command);
		} catch (IllegalArgumentException e) {
			System.err.println(String.format("Sorry, \"%s\" is not an Extract command.", command));
			System.exit(1);
			return;
		}

		final Options options = new CommonsTransformer().apply(task.options());
		final CommandLine line = new DefaultParser().parse(options, Arrays.copyOfRange(args, 1, args.length));
		final ProgressBar progressBar;

		if (task instanceof Monitorable) {
			progressBar = new ProgressBar(command, 0);

			((Monitorable) task).addListener(new ConsoleProgressListener(progressBar));
			progressBar.start();
		} else {
			progressBar = null;
		}

		if (line.getArgs().length > 0) {
			task.run(line.getArgs());
		} else {
			task.run();
		}

		if (null != progressBar) {
			progressBar.stop();
		}
	}
}

@@ -101,103 +101,9 @@
 cli.
-Options;%0Aimport org.apache.commons.cli.CommandLine;%0Aimport org.apache.commons.cli.DefaultParser
+*
 ;%0A%0Ai
@@ -2629,13 +2629,21 @@
 Line
- line
+Parser parser
  = n
@@ -2664,99 +2664,269 @@
 er()
-.parse(options, Arrays.copyOfRange(args, 1, args.length));%0A%09%09final ProgressBar progressBar;
+;%0A%09%09final CommandLine line;%0A%09%09final ProgressBar progressBar;%0A%0A%09%09try %7B%0A%09%09%09line = parser.parse(options, Arrays.copyOfRange(args, 1, args.length));%0A%09%09%7D catch (UnrecognizedOptionException e) %7B%0A%09%09%09System.err.println(e.getMessage());%0A%09%09%09System.exit(1);%0A%09%09%09return;%0A%09%09%7D
 %0A%0A%09%09

Print the error message rather than a stack trace when the option is uncrecognised