package tf.ownnote.ui.helper;

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

public class OwnNoteEditorParameters {
    // this is a singleton for everyones use
    // http://www.javaworld.com/article/2073352/core-java/simply-singleton.html
    private final static OwnNoteEditorParameters INSTANCE = new OwnNoteEditorParameters();

    // list of command line parameters we can understand
    public static enum CmdOps {
        ownCloudDir,
        lookAndFeel
    };

    public static enum LookAndFeel {
        classic,
        oneNote
    };

    // value for owncloud path, if set
    private String ownCloudDir = null;

    // value for lookAndFeel, if set
    private LookAndFeel lookAndFeel = LookAndFeel.classic;
    
    private OwnNoteEditorParameters() {
        // Exists only to defeat instantiation.
    }

    public static OwnNoteEditorParameters getInstance() {
        return INSTANCE;
    }
    
    public void init(final String [ ] args) {
        // thats all options we can handle
        Options options = new Options();
        options.addOption(
                OwnNoteEditorParameters.CmdOps.lookAndFeel.toString(), 
                OwnNoteEditorParameters.CmdOps.lookAndFeel.toString(), 
                true, 
                "Layout to use - \"classic\" or \"onenote\"");
        options.addOption(
                OwnNoteEditorParameters.CmdOps.ownCloudDir.toString(), 
                OwnNoteEditorParameters.CmdOps.ownCloudDir.toString(), 
                true, 
                "Path of ownCloud Notes directory");

        // lets parse them by code from other people
        CommandLineParser parser = new DefaultParser();
        try {
            CommandLine command = parser.parse(options, args);
            
            // get path to owncloud notes directory
            if (command.hasOption(OwnNoteEditorParameters.CmdOps.ownCloudDir.toString())) {
                ownCloudDir = command.getOptionValue(OwnNoteEditorParameters.CmdOps.ownCloudDir.toString());
                System.out.println("Option ownCloudDir found: " + ownCloudDir);
            }
            
            String laf = "";
            if (command.hasOption(OwnNoteEditorParameters.CmdOps.lookAndFeel.toString())) {
                laf = command.getOptionValue(OwnNoteEditorParameters.CmdOps.lookAndFeel.toString());
                System.out.println("Option lookAndFeel found: " + laf);
            }
            switch (laf) {
                case "classic":
                    lookAndFeel = LookAndFeel.classic;
                    break;
                case "oneNote":
                    lookAndFeel = LookAndFeel.oneNote;
                    break;
                default:
                    lookAndFeel = LookAndFeel.classic;
            }
        } catch (ParseException ex) {
            //Logger.getLogger(OwnNoteEditorParameters.class.getName()).log(Level.SEVERE, null, ex);
            // fix for issue #19: add usage screen in case of incorrect options
            help(options);
        }
    }

    public String getOwnCloudDir() {
        return ownCloudDir;
    }

    public void setOwnCloudDir(final String ownCloudDir) {
        this.ownCloudDir = ownCloudDir;
    }

    public LookAndFeel getLookAndFeel() {
        return lookAndFeel;
    }

    public void setLookAndFeel(final LookAndFeel lookAndFeel) {
        this.lookAndFeel = lookAndFeel;
    }
    
    private void help(final Options options) {
        // This prints out some help
        HelpFormatter formater = new HelpFormatter();

        formater.printHelp("OwnNoteEditor", "Valid options are:", options, "Continue using only recognized options");
        //System.exit(0);
    }    
}

@@ -2632,143 +2632,147 @@
     
-    System.out.println(%22Option lookAndFeel found: %22 + laf);%0D%0A            %7D%0D%0A            switch (laf) %7B%0D%0A                case %22classic%22:
+%7D%0D%0A            switch (laf) %7B%0D%0A                case %22classic%22:%0D%0A                    System.out.println(%22Option lookAndFeel found: %22 + laf);
 %0D%0A  
@@ -2882,24 +2882,101 @@
 %22oneNote%22:%0D%0A
+                    System.out.println(%22Option lookAndFeel found: %22 + laf);%0D%0A
             
@@ -3073,16 +3073,139 @@
 fault:%0D%0A
+                    System.out.println(%22Value %5C%22%22 + laf + %22%5C%22 for option lookAndFeel not recognized, using %5C%22classic%5C%22%22);%0D%0A
         

Fix for issue #19