package pl.touk.sputnik.processor.ktlint;

import com.github.shyiko.ktlint.core.KtLint;
import com.github.shyiko.ktlint.core.RuleSet;
import com.github.shyiko.ktlint.core.RuleSetProvider;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewProcessor;
import pl.touk.sputnik.review.ReviewResult;
import pl.touk.sputnik.review.filter.KotlinFilter;
import pl.touk.sputnik.review.transformer.FileNameTransformer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;

class KtlintProcessor implements ReviewProcessor {
    private static final String SOURCE_NAME = "ktlint";

    private final List<RuleSet> ruleSets;
    private final List<String> excludedRules;

    KtlintProcessor(Configuration configuration) {
        excludedRules = parseExcludedRules(configuration);
        ruleSets = findRuleSets();
    }

    private static List<RuleSet> findRuleSets() {
        List<RuleSet> ruleSets = new ArrayList<>();
        for (RuleSetProvider ruleSetProvider : ServiceLoader.load(RuleSetProvider.class)) {
            ruleSets.add(ruleSetProvider.get());
        }
        return ruleSets;
    }

    @NotNull
    private static List<String> parseExcludedRules(Configuration configuration) {
        String excludeProperty = configuration.getProperty(GeneralOption.KTLINT_EXCLUDE);
        if (excludeProperty == null) {
            return new ArrayList<>();
        } else {
            return Arrays.asList(excludeProperty.split(","));
        }
    }

    @Nullable
    @Override
    public ReviewResult process(@NotNull Review review) {
        List<String> filePaths = review.getFiles(new KotlinFilter(), new FileNameTransformer());
        return processFiles(filePaths);
    }

    private ReviewResult processFiles(List<String> filePaths) {
        ReviewResult result = new ReviewResult();
        for (String filePath : filePaths) {
            String text = readFile(filePath);
            KtLint.INSTANCE.lint(text, ruleSets, new LintErrorConverter(result, filePath, excludedRules));
        }
        return result;
    }

    private String readFile(String filePath) {
        try {
            return IOUtils.toString(new FileInputStream(new File(filePath)));
        } catch (IOException e) {
            throw new RuntimeException("Cannot read file " + filePath, e);
        }
    }

    @NotNull
    @Override
    public String getName() {
        return SOURCE_NAME;
    }
}

@@ -1199,31 +1199,24 @@
     private 
-static 
 List%3CRuleSet
@@ -1230,24 +1230,24 @@
 uleSets() %7B%0A
+
         List
@@ -1494,23 +1494,16 @@
 private 
-static 
 List%3CStr
@@ -1737,20 +1737,9 @@
    %7D
- else %7B%0A    
+%0A
     
@@ -1788,34 +1788,24 @@
 plit(%22,%22));%0A
-        %7D%0A
     %7D%0A%0A    @

Remove statics and unnecessary else