package com.tterrag.k9.mappings.mcp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;

import com.google.common.base.Charsets;
import com.google.common.collect.Sets;
import com.tterrag.k9.mappings.MappingType;
import com.tterrag.k9.mappings.NameType;
import com.tterrag.k9.mappings.Parser;
import com.tterrag.k9.mappings.SignatureHelper;
import com.tterrag.k9.util.annotation.NonNull;
import com.tterrag.k9.util.annotation.Nullable;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class TsrgParser implements Parser<ZipFile, SrgMapping> {
    
    private static final SignatureHelper sigHelper = new SignatureHelper();
    
    private final SrgDatabase db;

    @Override
    public List<SrgMapping> parse(ZipFile zip) throws IOException {
        Set<String> staticMethods;
        List<String> lines;
        try {
            staticMethods = Sets.newHashSet(IOUtils.readLines(zip.getInputStream(zip.getEntry("config/static_methods.txt")), Charsets.UTF_8));
            lines = IOUtils.readLines(zip.getInputStream(zip.getEntry("config/joined.tsrg")), Charsets.UTF_8);
        } finally {
            zip.close();
        }
        List<SrgMapping> ret = new ArrayList<>();
        SrgMapping currentClass = null;
        for (String line : lines) {
            SrgMapping mapping;
            if (!line.startsWith("\t")) {
                String[] names = line.split(" ");
                mapping = currentClass = new SrgMapping(db, MappingType.CLASS, names[0], names[1], null, null, null, false);
            } else {
                String[] data = line.substring(1).split(" ");
                if (data.length == 2) {
                    mapping = new SrgMapping(db, MappingType.FIELD, data[0], data[1], null, null, currentClass.getIntermediate(), false);
                } else {
                    mapping = new SrgMapping(db, MappingType.METHOD, data[0], data[2], data[1], null, currentClass.getIntermediate(), staticMethods.contains(data[2])) {
    
                        private @Nullable String srgDesc;
    
                        @Override
                        public @NonNull String getIntermediateDesc() {
                            if (srgDesc == null) {
                                srgDesc = sigHelper.mapSignature(NameType.INTERMEDIATE, getOriginalDesc(), this, db);
                            }
                            return srgDesc;
                        }
                    };
                }
            }
            ret.add(mapping);
        }
        return ret;
    }

}

@@ -1426,16 +1426,377 @@
 apping;%0A
+            if (line.startsWith(%22tsrg2 %22)) %7B%0A                // TSRGv2 support, skip header line and check that this is standard name set%0A                if (!line.startsWith(%22tsrg2 obf srg%22)) %7B%0A                    throw new UnsupportedOperationException(%22Custom names in tsrgv2 is not supported yet%22);%0A                %7D%0A                continue;%0A            %7D%0A
         
@@ -1825,24 +1825,24 @@
 th(%22%5Ct%22)) %7B%0A
-
             
@@ -2014,32 +2014,62 @@
           %7D else
+ if (!line.startsWith(%22%5Ct%5Ct%22))
  %7B%0A             
@@ -2320,16 +2320,159 @@
  else %7B%0A
+                    // TSRGv2 Support%0A                    MappingType type = data%5B1%5D.startsWith(%22(%22) ? MappingType.METHOD : MappingType.FIELD;%0A
         
@@ -2512,34 +2512,20 @@
 ing(db, 
-MappingType.METHOD
+type
 , data%5B0
@@ -3083,16 +3083,16 @@
      %7D;%0A
-
         
@@ -3093,32 +3093,113 @@
               %7D%0A
+            %7D else %7B%0A                // NO-OP (params)%0A                continue;%0A
             %7D%0A  

Allow tsrg parser to process v2 files