package com.blamejared.mcbot.mcp;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;

import com.blamejared.mcbot.mcp.IMapping.Side;
import com.blamejared.mcbot.mcp.ISrgMapping.MappingType;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;

public class MappingDatabase {
        
    private final Multimap<MappingType, IMapping> mappings = MultimapBuilder.enumKeys(MappingType.class).arrayListValues().build();
    
    private final File zip;
    private final String mcver;
    
    public MappingDatabase(String mcver) {
        this.zip = Paths.get(".", "data", mcver, "mappings").toFile().listFiles()[0];
        this.mcver = mcver;
        try {
            reload();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }

    public void reload() throws IOException {
        mappings.clear();
        ZipFile zipfile = new ZipFile(zip);

        try {
            for (MappingType type : MappingType.values()) {
                if (type.getCsvName() != null) {
                    List<String> lines;
                    lines = IOUtils.readLines(zipfile.getInputStream(zipfile.getEntry(type.getCsvName() + ".csv")), Charsets.UTF_8);

                    lines.remove(0); // Remove header line

                    for (String line : lines) {
                        String[] info = line.split(",", -1);
                        IMapping mapping = new IMapping.Impl(type, info[0], info[1], info.length > 3 ? info[3] : "", Side.values()[Integer.valueOf(info[2])]);
                        mappings.put(type, mapping);
                    }
                }
            }
        } finally {
            zipfile.close();
        }
    }

    public List<IMapping> lookup(MappingType type, String name) {
        Collection<IMapping> mappingsForType = mappings.get(type);
        String[] hierarchy = null;
        if (name.contains(".")) {
            hierarchy = name.split("\\.");
            name = hierarchy[hierarchy.length - 1];
        }

        final String lookup = name;
        List<IMapping> ret = mappingsForType.stream().filter(m -> m.getSRG().contains(lookup) || m.getMCP().equals(lookup)).collect(Collectors.toList());
        if (hierarchy != null) {
            if (type == MappingType.PARAM) {
                ;
            } else {
                final String parent = hierarchy[0];
                ret = ret.stream().filter(m -> DataDownloader.INSTANCE.lookupSRG(type, m.getSRG(), mcver).get(0).getOwner().endsWith(parent)).collect(Collectors.toList());
            }
        }
        
        return ret;
    }
}

@@ -105,16 +105,41 @@
 .Paths;%0A
+import java.util.Arrays;%0A
 import j
@@ -155,24 +155,24 @@
 Collection;%0A
-
 import java.
@@ -286,16 +286,60 @@
 IOUtils;
+%0Aimport org.apache.commons.lang3.ArrayUtils;
 %0A%0Aimport
@@ -436,16 +436,16 @@
 ngType;%0A
-
 import c
@@ -476,16 +476,54 @@
 arsets;%0A
+import com.google.common.base.Joiner;%0A
 import c
@@ -1756,16 +1756,144 @@
 %22, -1);%0A
+                        String comment = info.length %3E 3 ? Joiner.on(',').join(ArrayUtils.subarray(info, 3, info.length)) : %22%22;%0A
         
@@ -1973,38 +1973,15 @@
 1%5D, 
-info.length %3E 3 ? info%5B3%5D : %22%22
+comment
 , Si

Fix MCP command cutting off comments at commas