package innovimax.mixthem;

import innovimax.mixthem.exceptions.ArgumentException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* <p>Mix-them command line arguments management.</p>
* @author Innovimax
* @version 1.0
*/
class Arguments {
    
    private Rule rule = null;
    private List<String> ruleParams = null;
    private File file1 = null;
    private File file2 = null;

    private void setRule(Rule rule) {
        this.rule = rule;
    }

    Rule getRule() {
        return this.rule;
    }

    void setRuleParameters(List<String> ruleParams) {
        this.ruleParams = ruleParams;
    }

    List<String> getRuleParameters() {
        return this.ruleParams;
    }

    void setFirstFile(File file1) {
        this.file1 = file1;
    }

    File getFirstFile() {
        return this.file1;
    }

    void setSecondFile(File file2) {
        this.file2 = file2;
    }

    File getSecondFile() {
        return this.file2;
    }

    static Arguments checkArguments(String[] args) throws ArgumentException { 
        Arguments mixArgs = new Arguments();
        int index = 0;
        Rule rule = getRuleArgument(args, index, "rule");
        List<String> ruleParams = null;
        if (rule != null) {
            index++;
            ruleParams = getRuleParameters(args, index, rule);
            index += ruleParams.size();
        }
        File file1 = getFileArgument(args, index, "file1");
        File file2 = getFileArgument(args, ++index, "file2");
        mixArgs.setRule(rule);
        mixArgs.setRuleParameters(ruleParams);
        mixArgs.setFirstFile(file1);
        mixArgs.setSecondFile(file2);
        return mixArgs;
    }

    private static Rule getRuleArgument(String[] args, int index, String name) throws ArgumentException {        
        Rule rule = null;
        if (args.length >= 1) {
            final String ruleString = args[index];
            if (ruleString.startsWith("-")) {
                rule = Rule.findByName(ruleString);
                if (rule == null) {
                    throw new ArgumentException(name + " argument is incorrect: " + ruleString);
                }
            }
        }
        return rule;
    }

    private static List<String> getRuleParameters(String[] args, int index, Rule rule) throws ArgumentException {
        List<String> params = new ArrayList<String>();        
        // TODO
        return params;
    }

    private static File getFileArgument(String[] args, int index, String name) throws ArgumentException {
        File file = null;
        if (args.length < index+1) {
            throw new ArgumentException(name + " argument missing.");
        } else {
            String filepath = args[index];
            file = new File(filepath);
            if (file.exists()) {
                if (!file.canRead()) {                    
                    throw new ArgumentException(name + " cannot be read: " + filepath);    
                }
            } else {
                throw new ArgumentException(name + " not found: " + filepath);
            }
        }
        return file;
    }

}

@@ -126,16 +126,43 @@
 ayList;%0A
+import java.util.Iterator;%0A
 import j
@@ -176,16 +176,16 @@
 l.List;%0A
-
 %0A/**%0A* %3C
@@ -1880,11 +1880,14 @@
 th %3E
-= 1
+ index
 ) %7B%0A
@@ -2406,24 +2406,25 @@
 ring%3E();
+%0A
         
 %0A       
@@ -2419,24 +2419,803 @@
     
-%0A        // TODO
+Iterator%3CRuleParam%3E iterator = rule.getParams().iterator();%0A        if (iterator.hasNext()) %7B%0A            RuleParam param = iterator.next();%0A            if (args.length %3E index) %7B%0A                String value = args%5Bindex%5D;%0A                if (param.checkValue(value)) %7B%0A                    params.add(value);%0A                    index++;%0A                %7D else %7B%0A                    if (param.isRequired()) %7B%0A                        throw new ArgumentException(%22%5B%22 + param.getName() + %22%5D parameter is incorrect: %22 + value);    %0A                    %7D%0A                %7D%0A            %7D else %7B%0A                if (param.isRequired()) %7B%0A                    throw new ArgumentException(%22%5B%22 + param.getName() + %22%5D parameter is required.%22);    %0A                %7D%0A            %7D            %0A        %7D     
 %0A   
@@ -3401,108 +3401,19 @@
 gth 
-%3C
+%3E
  index
-+1
 ) %7B%0A
-            throw new ArgumentException(name + %22 argument missing.%22);%0A        %7D else %7B%0A
     
@@ -3784,32 +3784,32 @@
  %22 + filepath);%0A
-
             %7D%0A  
@@ -3798,32 +3798,119 @@
 ;%0A            %7D%0A
+        %7D else %7B%0A            throw new ArgumentException(name + %22 argument missing.%22);%0A
         %7D%0A      

Update Arguments.java