package com.madhur.uml_parser;

import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;
// TODO check whether its anti-pattern to use 'import something.*', if not, why so?

// TODO check style guide for Java, something similar to PEP8 maybe

public class Facade
{
    private String inputFolder, outputFolder;
    private boolean classDiagram=false, seqDiagram=false;

    private Facade(){
    }

    public static void main( String[] args ){
        Facade f = new Facade();
        f.parseArguments(args);
        if (f.classDiagram){
            f.genClassDiagram();
        }
        if (f.seqDiagram){
            f.genSeqDiagram();
        }
    }

    private void parseArguments(String[] args)
    {
        Options options = new Options();

        Option classDiagramOpt = new Option("c", "Generate class diagram(s)");
        options.addOption( classDiagramOpt );

        Option seqDiagramOpt = new Option("s", "Generate sequence diagram(s)");
        options.addOption( seqDiagramOpt );

        Option inputOpt = new Option("i", "inputFolder", true, "input folder path");
        inputOpt.setRequired(true);
        options.addOption(inputOpt);

        Option outputOpt = new Option("o", "outputFolder", true, "output folder path");
        outputOpt.setRequired(true);
        options.addOption(outputOpt);


        CommandLineParser parser = new DefaultParser();
        CommandLine line;
        try {
            line = parser.parse( options, args );

            // TODO remove below lines out ot th try catch construct
            if( line.hasOption( "c" ) ) {
                classDiagram = true;
            }
            if( line.hasOption( "s" ) ) {
                seqDiagram = true;
            }
            inputFolder = line.getOptionValue( "inputFolder" );
            outputFolder = line.getOptionValue( "outputFolder" );
        }
        catch( ParseException exp ) {
            System.err.println( "Parsing failed.  Reason: " + exp.getMessage() );
            // TODO exit
        }
        // TODO use logger
        System.out.println(String.format("Outpath: %s\nInput Folder: %s\nClass Diagram: %s\nSeq Diagram: %s",
                                         outputFolder, inputFolder, classDiagram, seqDiagram));
    }

    private void genClassDiagram(){
        ClassDiagramGenerator cdg =  new ClassDiagramGenerator(inputFolder, outputFolder);
        cdg.generate();
    }

    private void genSeqDiagram(){
        SeqDiagramGenerator sdg =  new SeqDiagramGenerator(inputFolder, outputFolder);
        sdg.generate();
    }
}

@@ -284,16 +284,55 @@
 ception;
+%0A%0Aimport java.io.FileNotFoundException;
 %0A// TODO
@@ -745,16 +745,262 @@
 (args);%0A
+%0A        // TODO there has to be a better way of expanding ~%0A        f.inputFolder = f.inputFolder.replaceFirst(%22%5E~%22, System.getProperty(%22user.home%22));%0A        f.outputFolder = f.outputFolder.replaceFirst(%22%5E~%22, System.getProperty(%22user.home%22));%0A%0A
         
@@ -2882,23 +2882,154 @@
     
-cdg.generate();
+try %7B%0A            cdg.generate();%0A        %7D catch (FileNotFoundException e) %7B%0A            e.printStackTrace();%0A            // TODO exit?%0A        %7D
 %0A   

Handle input/output path containg '~'