/*
 * This file is part of the DITA Open Toolkit project.
 *
 * Copyright 2019 Jarno Elovirta
 *
 * See the accompanying LICENSE file for applicable license.
 */

package org.dita.dost.invoker;

import org.apache.tools.ant.Project;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class InstallArguments extends Arguments {

    String installFile;

    @Override
    Arguments parse(final String[] arguments) {
        final Deque<String> args = new ArrayDeque<>(Arrays.asList(arguments));
        while (!args.isEmpty()) {
            final String arg = args.pop();
            if (arg.equals("install")) {
                handleSubcommandInstall(arg, args);
            } else if (isLongForm(arg, "-install")) {
                handleArgInstall(arg, args);
            } else if (isLongForm(arg, "-force")) {
                definedProps.put("force", "true");
            } else {
                parseCommonOptions(arg, args);
            }
        }
        if (msgOutputLevel < Project.MSG_INFO) {
            emacsMode = true;
        }
        return this;
    }

    /**
     * Handle the --install argument
     */
    private void handleArgInstall(final String arg, final Deque<String> args) {
        final int posEq = arg.indexOf("=");
        String value;
        if (posEq != -1) {
            value = arg.substring(posEq + 1);
        } else {
            value = args.peek();
            if (value != null && !value.startsWith("-")) {
                value = args.pop();
            } else {
                value = null;
            }
        }
        if (value != null) {
            installFile = value;
        }
    }

    private void handleSubcommandInstall(final String arg, final Deque<String> args) {
        String value;
        value = args.peek();
        if (value != null && !value.startsWith("-")) {
            value = args.pop();
        } else {
            value = null;
        }
        if (value != null) {
            installFile = value;
        }
    }

    @Override
    void printUsage(final boolean compact) {
        UsageBuilder.builder(compact)
                .usage("dita install [<file> | <url> | <id>]")
                .arguments(null, null, "file", "Install plug-in from a local ZIP file")
                .arguments(null, null, "url", "Install plug-in from a URL")
                .arguments(null, null, "id", "Install specified plug-in ID from registry")
                .options(null, "force", null, "Force-install plug-in (overwrite existing version)")
                .print();
    }

}

@@ -2167,20 +2167,18 @@
 stall %5B%3C
-file
+id
 %3E %7C %3Curl
@@ -2182,18 +2182,20 @@
 url%3E %7C %3C
-id
+file
 %3E%5D%22)%0A   
@@ -2231,20 +2231,18 @@
  null, %22
-file
+id
 %22, %22Inst
@@ -2241,32 +2241,42 @@
 %22, %22Install 
+specified 
 plug-in 
 from a local
@@ -2267,29 +2267,24 @@
 -in 
+ID 
 from 
-a local ZIP file
+registry
 %22)%0A 
@@ -2390,34 +2390,36 @@
 ts(null, null, %22
-id
+file
 %22, %22Install spec
@@ -2414,34 +2414,24 @@
 Install 
-specified 
 plug-in 
 ID from 
@@ -2422,32 +2422,37 @@
 plug-in 
-ID 
 from 
-registry
+a local ZIP file
 %22)%0A     

Reorder install subcommand argument order