package org.helioviewer.jhv.io;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;

import org.helioviewer.jhv.JHVGlobals;
import org.helioviewer.jhv.base.logging.Log;
import org.helioviewer.jhv.gui.ImageViewerGui;
import org.helioviewer.jhv.layers.ImageLayer;

public class CommandLine {

    private static String[] arguments;
    private static String usageMessage;

    public static void setArguments(String[] args) {
        arguments = args;
        usageMessage = "The following command-line options are available: \n\n" +
        "-jpx   JPX_REQUEST_URL\n" + "       Allows users to pass a JPX request URL for a JPX movie which will be opened upon program start. The option can be used multiple times." + "\n\n" +
        "-jpip  JPIP_URL\n" + "       Allows users to pass a JPIP URL of a JP2 or JPX image to be opened upon program start. The option can be used multiple times.";
    }

    public static void load() {
        // -jpx
        for (URI uri : getURIOptionValues("jpx")) {
            JHVGlobals.getExecutorService().execute(new LoadURITask(ImageLayer.createImageLayer(), uri));
        }
        // -jpip
        for (URI uri : getJPIPOptionValues()) {
            JHVGlobals.getExecutorService().execute(new LoadURITask(ImageLayer.createImageLayer(), uri));
        }
        // -request: works only for default server
        for (URI uri : getURIOptionValues("request")) {
            JHVGlobals.getExecutorService().execute(new LoadJSONTask(ImageLayer.createImageLayer(), uri));
        }
        // -state
        for (String file : getOptionValues("state")) {
            ImageViewerGui.getRenderableContainer().loadScene(file);
            break;
        }
    }

    private static List<URI> getURIOptionValues(String param) {
        List<String> opts = getOptionValues(param);
        LinkedList<URI> uris = new LinkedList<>();
        for (String opt : opts) {
            try {
                URI uri = new URI(opt);
                if (uri.getScheme() == null) {
                    File f = new File(opt).getAbsoluteFile();
                    if (f.canRead()) {
                        uris.add(f.toURI());
                    } else
                        Log.error("File not found: " + opt);
                } else
                    uris.add(uri);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        return uris;
    }

    private static List<URI> getJPIPOptionValues() {
        List<String> opts = getOptionValues("jpip");
        LinkedList<URI> uris = new LinkedList<>();
        for (String opt : opts) {
            try {
                URI uri = new URI(opt);
                if ("jpip".equals(uri.getScheme()))
                    uris.add(uri);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        return uris;
    }

    /**
     * Checks whether a specific option is set
     * 
     * @param option
     *            option which to check for
     * @return true if the option was given as a command line argument, false
     *         else
     */
    public static boolean isOptionSet(String option) {
        for (String arg : arguments) {
            if (arg.equals(option)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Method that looks for options in the command line.
     * 
     * @param param
     *            name of the option.
     * @return the values associated to the option.
     * */
    public static List<String> getOptionValues(String param) {
        param = '-' + param;
        LinkedList<String> values = new LinkedList<>();
        if (arguments != null) {
            for (int i = 0; i < arguments.length; i++) {
                if (param.equals(arguments[i]) && arguments.length > i + 1) {
                    values.add(arguments[i + 1]);
                }
            }
        }
        return values;
    }

    public static String getUsageMessage() {
        return usageMessage;
    }

}

@@ -618,123 +618,86 @@
   %22-
-jpx   JPX_REQUEST_URL
+load    file location
 %5Cn%22 
-+ %22
+ 
        
-Allows users to pass a JPX request URL for a JPX movie which will be opened upon
++ %22       Load or request a supported file at
  pro
@@ -746,21 +746,16 @@
 e times.
-%22 + %22
 %5Cn%5Cn%22 +%0A
@@ -768,107 +768,93 @@
   %22-
-jpip  JPIP_URL%5Cn%22 + %22       Allows users to pass a JPIP URL of a JP2 or JPX image to be opened upon
+request request file location%5Cn%22 + %22       Load a request file and issue a request at
  pro
@@ -907,233 +907,126 @@
 mes.
-%22;
+%5Cn%5Cn%22 +
 %0A    
-%7D%0A%0A
     
-public static void load() %7B%0A        // -jpx%0A        for (URI uri : getURIOptionValues(%22jpx%22)) %7B%0A            JHVGlobals.getExecutorService().execute(new LoadURITask(ImageLayer.createImageLayer(), uri));%0A        %7D
+%22-state   state file%5Cn%22            + %22       Load state file.%22;%0A    %7D%0A%0A    public static void load() %7B
 %0A   
@@ -1034,20 +1034,20 @@
     // -
-jpip
+load
 %0A       
@@ -1061,28 +1061,27 @@
 RI uri : get
-JPIP
+URI
 OptionValues
@@ -1081,16 +1081,22 @@
 nValues(
+%22load%22
 )) %7B%0A   
@@ -1889,36 +1889,215 @@
     
-if (uri.getScheme() == null)
+String scheme = uri.getScheme();%0A                if (%22jpip%22.equals(scheme) %7C%7C %22http%22.equals(scheme) %7C%7C %22https%22.equals(scheme) %7C%7C %22file%22.equals(scheme))%0A                    uris.add(uri);%0A                else
  %7B%0A 
@@ -2350,518 +2350,8 @@
    %7D
- else%0A                    uris.add(uri);%0A            %7D catch (URISyntaxException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A        return uris;%0A    %7D%0A%0A    private static List%3CURI%3E getJPIPOptionValues() %7B%0A        List%3CString%3E opts = getOptionValues(%22jpip%22);%0A        LinkedList%3CURI%3E uris = new LinkedList%3C%3E();%0A        for (String opt : opts) %7B%0A            try %7B%0A                URI uri = new URI(opt);%0A                if (%22jpip%22.equals(uri.getScheme()))%0A                    uris.add(uri);
 %0A   

Generalize command line