package com.kodcu.config;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Created by Hakan on 5/19/2015.
 */
public class FileConfiguration {

    private final Logger logger = LoggerFactory.getLogger(FileConfiguration.class);
    private final String[] args;

    public FileConfiguration(String[] args) {
        this.args = args;
    }

    public YamlConfiguration getFileContent() {
        String fileName = args[0];
        YamlConfiguration config = null;
        try {
            File configFile = new File(fileName);
            String content = FileUtils.readFileToString(configFile, "utf-8");
            Yaml yaml = new Yaml();
            config = yaml.loadAs(content, YamlConfiguration.class);
            if (args.length > 1)
                this.checkConfigArguments(config);
        } catch (Exception e) {
            logger.error(e.getMessage(), e.fillInStackTrace());
        }

        return config;
    }

    private void checkConfigArguments(YamlConfiguration config) {
        Class<?> clazz = config.getClass();
        final Method[] methodList = clazz.getDeclaredMethods();

        for (int i = 1; i < args.length; i++) {
            final String[] confParameter = args[i].split(":");
            switch (confParameter.length) {
                case 2:
                    String key = String.join("", "set", Character.toUpperCase(confParameter[0].charAt(0)) + confParameter[0].substring(1));
                    String value = confParameter[1];
                    boolean coupling = Arrays.asList(methodList).stream().anyMatch(method -> {
                        String methodName = method.getName();
                        if (methodName.equals(key)) {
                            try {
                                Object param = value;
                                Class[] types = method.getParameterTypes();

                                if (types[0].getName().equals("boolean"))
                                    param = Boolean.valueOf(value);
                                else if (types[0].getName().equals("int")) {
                                    try {
                                        param = Integer.valueOf(value);
                                    } catch (NumberFormatException e){
                                        logger.error("Invalid value for parameter");
                                        System.exit(-1);
                                    }
                                }

                                method.setAccessible(true);
                                method.invoke(config, param);

                            } catch (Exception ex) {
                                logger.error(ex.getMessage(), ex.fillInStackTrace());
                                return false;
                            }
                            return true;
                        } else
                            return false;
                    });
                    if (!coupling) {
                        logger.error("Incorrect parameter. Pass the correct parameter name.");
                        System.exit(-1);
                    }
            }
        }
    }

}

@@ -187,27 +187,53 @@
 ava.
-lang.reflect.Method
+util.List;%0Aimport java.util.stream.Collectors
 ;%0Aim
@@ -247,22 +247,29 @@
 va.util.
-Arrays
+stream.Stream
 ;%0A%0A/**%0A 
@@ -854,145 +854,182 @@
     
-config = yaml.loadAs(content, YamlConfiguration.class);%0A            if (args.length %3E 1)%0A                this.checkConfigArguments(config
+if (args.length %3E 1)%0A                config = this.checkConfigArguments(yaml, content);%0A            else%0A                config = yaml.loadAs(content, YamlConfiguration.class
 );%0A 
@@ -1180,12 +1180,25 @@
 ate 
-void
+YamlConfiguration
  che
@@ -1223,186 +1223,136 @@
 Yaml
-Configuration config) %7B%0A        Class%3C?%3E clazz = config.getClass();%0A        final Method%5B%5D methodList = clazz.getDeclaredMethods();%0A%0A        for (int i = 1; i %3C args.length; i++)
+ yaml, String content) %7B%0A        List%3CString%3E extraParams = Stream.of(args).filter(arg -%3E arg.split(%22:%22).length == 2).map(arg -%3E
  %7B%0A 
@@ -1400,12 +1400,8 @@
  arg
-s%5Bi%5D
 .spl
@@ -1425,1946 +1425,323 @@
     
-switch (confParameter.length) %7B%0A                case 2:%0A                    String key = String.join(%22%22, %22set%22, Character.toUpperCase(confParameter%5B0%5D.charAt(0)) + confParameter%5B0%5D.substring(1));%0A                    String value = confParameter%5B1%5D;%0A                    boolean coupling = Arrays.asList(methodList).stream().anyMatch(method -%3E %7B%0A                        String methodName = method.getName();%0A                        if (methodName.equals(key)) %7B%0A                            try %7B%0A                                Object param = value;%0A                                Class%5B%5D types = method.getParameterTypes();%0A%0A                                if (types%5B0%5D.getName().equals(%22boolean%22))%0A                                    param = Boolean.valueOf(value);%0A                                else if (types%5B0%5D.getName().equals(%22int%22)) %7B%0A                                    try %7B%0A                                        param = Integer.valueOf(value);%0A                                    %7D catch (NumberFormatException e)%7B%0A                                        logger.error(%22Invalid value for parameter%22);%0A                                        System.exit(-1);%0A                                    %7D%0A                                %7D%0A%0A                                method.setAccessible(true);%0A                                method.invoke(config, param);%0A%0A                            %7D catch (Exception ex) %7B%0A                                logger.error(ex.getMessage(), ex.fillInStackTrace());%0A                                return false;%0A                            %7D%0A                            return true;%0A                        %7D else%0A                            return false;%0A                    %7D);%0A                    if (!coupling) %7B%0A                        logger.error(%22Incorrect parameter. Pass the correct parameter name.%22);%0A                        System.exit(-1);%0A                    %7D%0A            %7D%0A        %7D
+String key = confParameter%5B0%5D;%0A            String value = confParameter%5B1%5D;%0A            return String.join(%22: %22, key, value);%0A        %7D).collect(Collectors.toList());%0A        extraParams.add(0, content);%0A        content = String.join(%22%5Cn%22, extraParams);%0A        return yaml.loadAs(content, YamlConfiguration.class);
 %0A   

Refactor checkConfigArguments