/*******************************************************************************
 * 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 com.google.common.collect.Queues;

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

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: ")) {
				String[] deobfSplit = split[2].split("/");
				String deobfClass = split[2]
						.substring(0, split[2].length() - (deobfSplit[deobfSplit.length - 1].length() + 1));
				mappings.getClassByDeobf(deobfClass)
						.addMethodMapping(new MethodMapping(split[1], new Signature(split[2]), split[3]));
			}
		}

		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]);
		}
	}
}

@@ -1650,32 +1650,78 @@
 With(%22MD: %22)) %7B%0A
+%09%09%09%09String%5B%5D obfSplit = split%5B0%5D.split(%22/%22);%0A%0A
 %09%09%09%09String%5B%5D deo
@@ -1871,24 +1871,25 @@
 th() + 1));%0A
+%0A
 %09%09%09%09mappings
@@ -1963,784 +1963,137 @@
 ing(
-s
+obfS
 plit%5B
-1%5D, new Signature(split%5B2%5D), split%5B3%5D));%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return mappings;%0A%09%7D%0A%0A%09private ArgumentMapping readArgument(String%5B%5D parts) %7B%0A%09%09return new ArgumentMapping(Integer.parseInt(parts%5B1%5D), parts%5B2%5D);%0A%09%7D%0A%0A%09private ClassMapping readClass(String%5B%5D parts, boolean makeSimple) %7B%0A%09%09if (parts.length == 2) %7B%0A%09%09%09return new ClassMapping(parts%5B1%5D);%0A%09%09%7D else %7B%0A%09%09%09return new ClassMapping(parts%5B1%5D, parts%5B2%5D);%0A%09%09%7D%0A%09%7D%0A%0A%09/* TEMP */%0A%09protected FieldMapping readField(String%5B%5D parts) %7B%0A%09%09return new FieldMapping(parts%5B1%5D, new Type(parts%5B3%5D), parts%5B2%5D);%0A%09%7D%0A%0A%09private MethodMapping readMethod(String%5B%5D parts) %7B%0A%09%09if (parts.length == 3) %7B%0A%09%09%09return new MethodMapping(parts%5B1%5D, new Signature(parts%5B2
+obfSplit.length - 1%5D,%0A%09%09%09%09%09%09%09%09new Signature(split%5B1%5D), deobfSplit%5BdeobfSplit.length - 1
 %5D));%0A
+%09
 %09%09%7D
- else %7B%0A%09%09%09return new MethodMapping(parts%5B1%5D, new Signature(parts%5B3%5D), parts%5B2%5D);%0A%09%09%7D
+%0A%09%09%7D%0A%0A%09%09return mappings;
 %0A%09%7D%0A

Fix method reading