package org.josast.ApplicationSatelliteDatabaseCli.cmd;

import java.util.Vector;
import java.util.logging.Logger;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class OptionsAnalysis {
    /**
     * logger.
     */
    private Logger log = Logger.getLogger(getClass().getName());
    /**
     * List of command to execute.
     */
    private Vector<Cmd> listeCmd = new Vector<Cmd>();
    /**
     * Option.
     */
    private Options options = null;
    /**
     * line.
     */
    private CommandLine line = null;

    /**
     * @param args
     */
    public OptionsAnalysis(final String[] args) {
        options = configParameters();
        CommandLineParser parser = new DefaultParser();

        try {
            line = parser.parse(options, args);
            listeCmd = interprete(line, args);
        } catch (ParseException e) {
            log.severe(" Unrecognise option  " + e.toString());

        }
    }

    /**
     * @return list of command.
     */
    public Vector<Cmd> getCommandes() {
        return listeCmd;
    }

    /**
     * @param listeCmds
     */
    public void setListeCmd(final Vector<Cmd> listeCmds) {
        this.listeCmd = listeCmds;
    }

    private static Options configParameters() {

        final Option versionOption = Option.builder("V")

                .longOpt("Version") //

                .desc("Return application version")

                .hasArg(false)

                .required(false)

                .build();

        final Option mergeOption = Option.builder("M")

                .longOpt("Merge") //

                .desc("Merge all input folder to new Directoy to ouput xml file ")

                .hasArg(false)

                .required(false)

                .build();

        final Option checkOption = Option.builder("C")

                .longOpt("Check") //

                .desc("verify AmsatList File data")

                .hasArg(false)

                .required(false)

                .build();

        final Option updateOption = Option.builder("U")

                .longOpt("Update") //

                .desc("update AmsatList File data")

                .hasArg(false)

                .required(false)

                .build();

        final Option inputDirectoryOption = Option.builder("I")

                .longOpt("Input Directory") //

                .desc("Input directory with all input xml files (*.xml) ")

                .hasArg(true)

                .argName("InputRepository")

                .required(false)

                .build();

        final Option outputDirectoryOption = Option.builder("O")

                .longOpt("Output Directory") //

                .desc("Output directory with all input xml files (*.xml) ")

                .hasArg(true)

                .argName("OutputDirectory")

                .required(false)

                .build();

        final Option outputFileOption = Option.builder("o")

                .longOpt("OutputFile") //

                .desc("output file")

                .hasArg(true)

                .argName("OutputFile")

                .required(false)

                .build();

        final Option inputFileOption = Option.builder("i")

                .longOpt("InputFile") //

                .desc("Input file")

                .hasArg(true)

                .argName("InputFile")

                .required(false)

                .build();

        final Option helpFileOption = Option.builder("h")

                .longOpt("help")

                .desc("display help message")

                .build();

        final Options options = new Options();

        options.addOption(updateOption);
        options.addOption(inputFileOption);
        options.addOption(checkOption);
        options.addOption(versionOption);
        options.addOption(mergeOption);
        options.addOption(inputDirectoryOption);
        options.addOption(outputDirectoryOption);
        options.addOption(outputFileOption);
        options.addOption(helpFileOption);

        return options;

    }

    private Vector<Cmd> interprete(CommandLine line, String[] args) {

        Vector<Cmd> cmdList = new Vector<Cmd>();
        ParameterSingleton parameter = ParameterSingleton.getInstance();

        parameter.setOutputFile(line.getOptionValue("o"));
        log.info("Output file  dir" + line.getOptionValue("o"));
        parameter.setInputDirectory(line.getOptionValue("I"));
        log.info("Input dir" + line.getOptionValue("I"));
        parameter.setOutputDirectory(line.getOptionValue("O"));
        log.info("OUtput dir" + line.getOptionValue("O"));
        parameter.setInputFile(line.getOptionValue("InputFile", ""));
        cmdList.add(new VersionCmd());

        if (line.hasOption("help")) {
            parameter.setArg0(args[0]);
            parameter.setOption(options);
            cmdList.add(new HelpCmd());
        }

        if (line.hasOption("Version")) {
            return cmdList;
        }

        if (line.hasOption("Merge")) {
            if (parameter.getInputDirectory() == null) {
                System.err.println("Merge :  inputDirectory not defined");
            } else if (parameter.getOutputFile() == null) {
                System.err.println("Merge :  Output file name not defined");
            } else {
                MergeCmd cmd = new MergeCmd();
                cmdList.add(cmd);
            }
        }

        if (line.hasOption("Check")) {
            if (parameter.getInputFile() == null) {
                System.err.println("Check :  Input file name not defined");
            }
            CheckCmd cmd = new CheckCmd();
            cmdList.add(cmd);
        }

        if (line.hasOption("Update")) {
            if (parameter.getInputDirectory() == null) {
                System.err.println("Update :  input Directory not defined");
            } else if (parameter.getOutputDirectory() == null) {
                System.err.println("Update :  Output  Directory  not defined");
            } else {
                UpdateCmd cmd = new UpdateCmd();
                cmdList.add(cmd);
            }

        }

        return cmdList;
    }

}
