/*
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.errorprone;

import static com.google.common.collect.Multimaps.index;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Ordering;
import com.google.errorprone.BugPattern.Instance;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author alexeagle@google.com (Alex Eagle)
 */
public class BugPatternIndexYamlWriter {

  void dump(Collection<Instance> patterns, Writer w)
      throws IOException {
    Map<String, List<Map<String, String>>> data = new HashMap<>();

    ListMultimap<String, Instance> index = index(patterns, new Function<Instance, String>() {
      @Override
      public String apply(Instance input) {
        return input.maturity.description + " : " + input.severity;
      }});

    for (Entry<String, Collection<Instance>> entry : index.asMap().entrySet()) {
      data.put(entry.getKey(), FluentIterable
          .from(entry.getValue())
          .transform(new Function<Instance, Map<String, String>>() {
            @Override
            public Map<String, String> apply(Instance input) {
              return ImmutableMap.of("name", input.name, "summary", input.summary);
            }})
          .toSortedList(new Ordering<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> left, Map<String, String> right) {
              return left.get("name").compareTo(right.get("name"));
            }}));
    }
    new Yaml().dump(data, w);
  }
}

@@ -1484,26 +1484,17 @@
 );%0A%0A    
-ListMultim
+M
 ap%3CStrin
@@ -1496,16 +1496,27 @@
 String, 
+Collection%3C
 Instance
@@ -1516,16 +1516,17 @@
 nstance%3E
+%3E
  index =
@@ -1714,16 +1714,24 @@
      %7D%7D)
+.asMap()
 ;%0A%0A    f
@@ -1738,77 +1738,77 @@
 or (
-Entry%3CString, Collection%3CInstance%3E%3E entry : index.asMap().entr
+String key : Ordering.natural().reverse().sortedCopy(index.ke
 ySet())
+)
  %7B%0A 
@@ -1825,22 +1825,11 @@
 put(
-entry.getKey()
+key
 , Fl
@@ -1861,23 +1861,21 @@
 rom(
-entry.getValue(
+index.get(key
 ))%0A 
@@ -2124,16 +2124,27 @@
        %7D
+%0A          
 %7D)%0A     
@@ -2389,16 +2389,27 @@
        %7D
+%0A          
 %7D));%0A   

Write the bugpatterns YAML in a consistent order so the test is deterministic