package be.jeffcheasey88.peeratcode.parser.java;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import be.jeffcheasey88.peeratcode.parser.java.Variable.MultipleDeclaratedVariable;

public class Class extends JavaElement{
	
	private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
	
	private int modifier;
	private String name;
	
	private List<JavaElement> childs;
	
	public Class(){}
	
	public int parse(String content, CleanerPool cleaner) throws Exception{
		Matcher matcher = PATTERN.matcher(content);
		matcher.matches();
		
		String[] split = matcher.group(2).split("\\s+");
		for(int i = 0; i < split.length-1; i++){
			this.modifier+=JavaParser.getModifier(split[i]);
		}
		this.name = split[split.length-1];
		
		this.childs = new ArrayList<>();
		
		content = matcher.group(3);
		Pattern empty = Pattern.compile("^\\s*$");
		while(!(empty.matcher(content).matches())){
			int quotes = indexOf(content,";");
			int braces = indexOf(content,"\\{");
			int equals = indexOf(content,"=");
			if((quotes < braces && quotes < equals) || (equals < braces)){
				Variable variable = new Variable();
				int index = variable.parse(content, cleaner);
				content = content.substring(index);
				boolean quote = content.startsWith(",");
				if(quote){
					content = content.substring(1);
					
					MultipleDeclaratedVariable multiple = new MultipleDeclaratedVariable(variable.getModifier(), variable.getType());
					multiple.addVariable(variable.getName(), variable.getValue());
					while(quote){
						variable = new Variable(variable.getModifier(), variable.getType());
						index = variable.parse(content, cleaner);
						content = content.substring(index);
						quote = content.startsWith(",");
						if(quote) content = content.substring(1);
						
						multiple.addVariable(variable.getName(), variable.getValue());
					}
					
					this.childs.add(multiple);
				}else{
					this.childs.add(variable);
				}
				
				content = content.substring(1);
			}else{
//				System.out.println("Function "+content);
				Function func = new Function();
				int index = func.parse(content, cleaner);
				this.childs.add(func);
				content = content.substring(index);
//				System.out.println("End "+content);
			}
		}
		
//		return cleaner.unzip(matcher.group(1), ((s,p) -> s)).length();
		return matcher.group(1).length();
	}
	
	private int indexOf(String value, String target){
		return value.split(target)[0].length();
	}
	
	public int getModifier(){
		return this.modifier;
	}
	
	public String getName(){
		return this.name;
	}
	
//	public List<Variable> getVariables(){
//		return this.vars;
//	}
	
	public void show(int tab){
		String start = "";
		for(int i = 0; i < tab; i++) start+="\t";
		System.out.println(start+Modifier.toString(modifier)+" "+this.name+"{");
		for(JavaElement jElement : this.childs){
			jElement.show(tab+1);
			System.out.println();
		}
		System.out.println(start+"}");
	}
}
