package cc.redpen.intellij;

import cc.redpen.ConfigurationLoader;
import cc.redpen.RedPen;
import cc.redpen.RedPenException;
import cc.redpen.config.Configuration;
import cc.redpen.parser.DocumentParser;
import com.google.common.collect.ImmutableMap;
import com.intellij.psi.PsiFile;

import java.util.Map;

public class RedPenProvider {
  private static RedPenProvider instance;
  Configuration initialConfig, config;

  Map<String, DocumentParser> parsers = ImmutableMap.of(
    "PLAIN_TEXT", DocumentParser.PLAIN,
    "Markdown", DocumentParser.MARKDOWN,
    "AsciiDoc", DocumentParser.ASCIIDOC
  );

  private RedPenProvider() {
    try {
      initialConfig = new ConfigurationLoader().loadFromResource("/redpen-conf.xml");
      config = new ConfigurationLoader().loadFromResource("/redpen-conf.xml");
    }
    catch (RedPenException e) {
      throw new RuntimeException("Cannot read RedPen conf file", e);
    }
  }

  public static RedPenProvider getInstance() {
    if (instance == null) instance = new RedPenProvider();
    return instance;
  }

  public RedPen getRedPen() {
    try {
      return new RedPen(config);
    }
    catch (RedPenException e) {
      throw new RuntimeException(e);
    }
  }

  public DocumentParser getParser(PsiFile file) {
    return parsers.get(file.getFileType().getName());
  }

  public Configuration getInitialConfig() {
    return initialConfig;
  }

  public Configuration getConfig() {
    return config;
  }
}

@@ -279,16 +279,48 @@
 iFile;%0A%0A
+import java.util.LinkedHashMap;%0A
 import j
@@ -333,16 +333,16 @@
 il.Map;%0A
-
 %0Apublic 
@@ -407,16 +407,28 @@
 tance;%0A 
+ Map%3CString,
  Configu
@@ -437,23 +437,58 @@
 tion
- initialConfig,
+%3E configs = new LinkedHashMap%3C%3E();%0A  Configuration
  con
@@ -726,24 +726,18 @@
     
-initial
+add
 Config
- = 
+(
 new 
@@ -790,24 +790,25 @@
 n-conf.xml%22)
+)
 ;%0A      conf
@@ -986,32 +986,149 @@
  e);%0A    %7D%0A  %7D%0A%0A
+  private void addConfig(Configuration configuration) %7B%0A    configs.put(configuration.getKey(), configuration);%0A  %7D%0A%0A
   public static 
@@ -1570,21 +1570,25 @@
 urn 
-initialConfig
+configs.get(%22en%22)
 ;%0A  

store a map of available configurations instead of a single initial one