/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.mapping;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Deque;
import java.util.Scanner;

import com.google.common.collect.Queues;

public class MappingsReader {
	
	public Mappings read(Reader in)
	throws IOException, MappingParseException {
		return read(new BufferedReader(in));
	}
	
	public Mappings read(BufferedReader in)
	throws IOException, MappingParseException {
		Mappings mappings = new Mappings();
		Deque<Object> mappingStack = Queues.newArrayDeque();
		
		Scanner scanner = new Scanner(in);

		while (scanner.hasNext()) {
			String line = scanner.nextLine();

			String[] split = line.substring(4).split(" ");
			if (line.startsWith("CL: ")) {
				mappings.addClassMapping(new ClassMapping(split[0], split[1]));
			} else if (line.startsWith("FD: ")) {
				String[] deobfSplit = split[1].split("/");
				String deobfClass = split[1]
						.substring(0, split[1].length() - (deobfSplit[deobfSplit.length - 1].length() + 1));
				//mappings.getClassByDeobf(deobfClass).addFieldMapping(new FieldMapping());
				// TODO:
			} else if (line.startsWith("MD: ")) {
				// TODO:
			}
		}
		
		return mappings;
	}
	
	private ArgumentMapping readArgument(String[] parts) {
		return new ArgumentMapping(Integer.parseInt(parts[1]), parts[2]);
	}
	
	private ClassMapping readClass(String[] parts, boolean makeSimple) {
		if (parts.length == 2) {
			return new ClassMapping(parts[1]);
		} else {
			return new ClassMapping(parts[1], parts[2]);
		}
	}
	
	/* TEMP */
	protected FieldMapping readField(String[] parts) {
		return new FieldMapping(parts[1], new Type(parts[3]), parts[2]);
	}
	
	private MethodMapping readMethod(String[] parts) {
		if (parts.length == 3) {
			return new MethodMapping(parts[1], new Signature(parts[2]));
		} else {
			return new MethodMapping(parts[1], new Signature(parts[3]), parts[2]);
		}
	}
}

@@ -368,10 +368,9 @@
 l%0A *
- 
 %0A
+
  * C
@@ -547,16 +547,58 @@
 pping;%0A%0A
+import com.google.common.collect.Queues;%0A%0A
 import j
@@ -726,50 +726,8 @@
 r;%0A%0A
-import com.google.common.collect.Queues;%0A%0A
 publ
@@ -752,18 +752,17 @@
 eader %7B%0A
-%09
 %0A
+
 %09public 
@@ -873,18 +873,17 @@
 n));%0A%09%7D%0A
-%09
 %0A
+
 %09public 
@@ -1053,19 +1053,17 @@
 eque();%0A
-%09%09
 %0A
+
 %09%09Scanne
@@ -1658,32 +1658,320 @@
 : %22)) %7B%0A%09%09%09%09
-// TODO:
+String%5B%5D deobfSplit = split%5B2%5D.split(%22/%22);%0A%09%09%09%09String deobfClass = split%5B2%5D%0A%09%09%09%09%09%09.substring(0, split%5B2%5D.length() - (deobfSplit%5BdeobfSplit.length - 1%5D.length() + 1));%0A%09%09%09%09mappings.getClassByDeobf(deobfClass)%0A%09%09%09%09%09%09.addMethodMapping(new MethodMapping(split%5B1%5D, new Signature(split%5B2%5D), split%5B3%5D));
 %0A%09%09%09%7D%0A%09%09%7D%0A%09%09
@@ -1972,11 +1972,9 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%09re
@@ -1987,26 +1987,25 @@
 appings;%0A%09%7D%0A
-%09
 %0A
+
 %09private Arg
@@ -2111,34 +2111,33 @@
 , parts%5B2%5D);%0A%09%7D%0A
-%09
 %0A
+
 %09private ClassMa
@@ -2321,18 +2321,17 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09/* TEMP
@@ -2456,17 +2456,16 @@
 2%5D);%0A%09%7D%0A
-%09
 %0A%09privat

Implement SRG method descriptor parsing