package pt.mashashi.javaroles.register;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import javassist.CannotCompileException;
import javassist.CtClass;
import pt.mashashi.javaroles.RoleBus;

public class CmdCloseClass implements Cmd{
		
		private final static List<CmdCloseClass> closes = new LinkedList<>();
		private static RoleRegister roleRegister;
		
		private CtClass clazz; 
		private String classesDir;
		private List<Cmd> dependencies;
		private boolean executed;
		
		private CmdCloseClass(){}
		private CmdCloseClass(CtClass clazz) {
			this.clazz = clazz;
			this.executed = false;
			this.dependencies = new LinkedList<Cmd>();
		}
		
		public static CmdCloseClass neu(RoleRegister roleRegister, CtClass clazz){
			CmdCloseClass.roleRegister = roleRegister;
			Iterator<CmdCloseClass> i = closes.iterator();
			CmdCloseClass n = null;
			
			List<CmdCloseClass> dependencies = new LinkedList<>();
			
			findCommand: {
				while(i.hasNext()){
					CmdCloseClass close = i.next();
					
					if(close.clazz.equals(clazz)){
						n = close;
						break findCommand;
					}
					
					{ // BLOCK Write only the deeper class the other will be written on the class hierarchy to
						if(close.clazz.subclassOf(clazz)){
							//System.out.println(close.clazz.getName()+" "+clazz.getName());
							//close.executed = true;
							dependencies.add(close);
						}/*else if(clazz.subclassOf(close.clazz)){
							return close;
						}*/
					}
				}
				n = new CmdCloseClass(clazz);
				closes.add(n);
			}
			for(CmdCloseClass d : dependencies){
				d.dependencies.add(n);
			}
			return n;
		}
		
		public void cmd(){
			if(!executed){
				
				{ // BLOCK Execute dependencies
					for(Cmd d : dependencies){
						d.cmd();
					}
				}
				
				String clazzName = clazz.getName();
				System.out.println(clazz.getName());
				try {
					if(classesDir!=null){
						clazz.writeFile(classesDir);
					}
					clazz.toClass();
					
					// Sometimes when saving classes it happens that we want to save a class that was saved previously indirectly 
					// by saving one that extends the current.
					
				} catch (CannotCompileException|IOException e) {
					Logger.getLogger(RoleBus.class.getName()).debug("error processing class: "+clazzName+" "+e.getMessage());
					e.printStackTrace();
					throw new RuntimeException();
					
				}
				executed = true;
			}
		}
	}
@@ -403,55 +403,11 @@
 ();%0A
-%09%09private static RoleRegister roleRegister;%0A
 %09%09%0A
+
 %09%09pr
@@ -774,54 +774,8 @@
 z)%7B%0A
-%09%09%09CmdCloseClass.roleRegister = roleRegister;%0A
 %09%09%09I
@@ -1092,16 +1092,56 @@
 %09%09%09%09
+ 
 %0A%09%09%09%09%09
-%7B 
+if(close.clazz.subclassOf(clazz))%7B%0A%09%09%09%09%09%09
 // B
@@ -1235,258 +1235,32 @@
 %09%09%09%09
-if(close.clazz.subclassOf(clazz))%7B%0A%09%09%09%09%09%09%09//System.out.println(close.clazz.getName()+%22 %22+clazz.getName());%0A%09%09%09%09%09%09%09//close.executed = true;%0A%09%09%09%09%09%09%09dependencies.add(close);%0A%09%09%09%09%09%09%7D/*else if(clazz.subclassOf(close.clazz))%7B%0A%09%09%09%09%09%09%09return close;%0A%09%09%09%09%09%09%7D*/
+dependencies.add(close);
 %0A%09%09%09
@@ -1560,24 +1560,24 @@
 %0A%09%09%09%09%7D%0A%09%09%09%09%0A
+
 %09%09%09%09String c
@@ -1608,49 +1608,8 @@
 ();%0A
-%09%09%09%09System.out.println(clazz.getName());%0A
 %09%09%09%09

Remove debug system.out.println