package be.jeffcheasey88.peeratcode.parser.java.operations;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;

public class ElseOperation extends OperationContainer{
	
	private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*).*$");
	
	public ElseOperation(){}

	@Override
	public int parse(String content, CleanerPool cleaner) throws Exception{
		System.out.println("ELSE STATEMENT");
		CleanerPool generic = new CleanerPool(
				new Cleaner("GENERIC_PARENTHESIS",'(',')'),
				new Cleaner("GENERIC_FUNCTION", '{','}'));
		content = generic.clean(content);
		
		Matcher matcher = PATTERN.matcher(content);
		matcher.matches();
		
		int index = generic.unzip(matcher.group(1), (s,p) -> s).length();
		content = generic.unzipOne(content, (s,p) -> s).substring(index);
		System.out.println("INSIDE "+content);
		int bodysize;
		if(content.startsWith("{")){
			bodysize = content.indexOf('}')+1;
			content = content.substring(1, bodysize-1);
			parse(content, cleaner, generic);
		}else{
			bodysize = parseOne(content, cleaner, generic);
		}
		
		return index+bodysize;
	}

	@Override
	public void show(int tab) {
		String start = "";
		for(int i = 0; i < tab; i++) start+="\t";
		System.out.println(start+"else{");
		for(JavaElement child : getChilds()) child.show(tab+1);
		System.out.println(start+"}");
	}

}

