package org.jboss.as.arquillian.container;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jboss.arquillian.container.spi.Container;
import org.jboss.arquillian.container.spi.event.container.BeforeDeploy;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.test.spi.context.ClassContext;
import org.jboss.arquillian.test.spi.event.suite.AfterClass;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.as.arquillian.api.ServerSetupTask;

/**
 * @author Stuart Douglas
 */
public class ServerSetupObserver {


    @Inject
    private Instance<ManagementClient> managementClient;

    @Inject
    private Instance<ClassContext> classContextInstance;

    private Set<ContainerClassHolder> alreadyRun = new HashSet<ContainerClassHolder>();

    private final List<ManagementClient> active = new ArrayList<ManagementClient>();
    private final List<ServerSetupTask> current = new ArrayList<ServerSetupTask>();

    public synchronized void handleBeforeDeployment(@Observes BeforeDeploy event, Container container) throws Exception {

        final ClassContext classContext = classContextInstance.get();
        if (classContext == null) {
            return;
        }

        final Class<?> currentClass = classContext.getActiveId();
        final ContainerClassHolder holder = new ContainerClassHolder(container.getName(), currentClass);
        if (alreadyRun.contains(holder)) {
            return;
        }
        alreadyRun.add(holder);
        ServerSetup setup = currentClass.getAnnotation(ServerSetup.class);
        if (setup == null) {
            return;
        }
        final Class<? extends ServerSetupTask>[] classes = setup.value();
        if (current.isEmpty()) {
            for(Class<? extends ServerSetupTask> clazz : classes) {
                Constructor<? extends ServerSetupTask> ctor = clazz.getDeclaredConstructor();
                ctor.setAccessible(true);
                current.add(ctor.newInstance());
            }
        } else {
            //this should never happen
            for(int i = 0; i < current.size(); ++ i) {
                if(classes[i] != current.get(i).getClass()) {
                    throw new RuntimeException("Mismatched ServerSetupTask current is " + current + " but " + currentClass + " is expecting " + Arrays.asList(classes));
                }
            }
        }

        final ManagementClient client = managementClient.get();
        for(ServerSetupTask instance : current) {
            instance.setup(client);
        }
        active.add(client);
    }

    public synchronized void handleAfterClass(@Observes AfterClass event) throws Exception {
        try {
            for(final ManagementClient client : active) {
                for(final ServerSetupTask instance : current) {
                    instance.tearDown(client);
                }
            }
        } finally {
            active.clear();
            current.clear();
        }
    }


    private static final class ContainerClassHolder {
        private final Class<?> testClass;
        private final String name;

        private ContainerClassHolder(final String name, final Class<?> testClass) {
            this.name = name;
            this.testClass = testClass;
        }

        @Override
        public boolean equals(final Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            final ContainerClassHolder that = (ContainerClassHolder) o;

            if (name != null ? !name.equals(that.name) : that.name != null) return false;
            if (testClass != null ? !testClass.equals(that.testClass) : that.testClass != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = testClass != null ? testClass.hashCode() : 0;
            result = 31 * result + (name != null ? name.hashCode() : 0);
            return result;
        }
    }


}

@@ -253,16 +253,89 @@
 tainer;%0A
+import org.jboss.arquillian.container.spi.event.container.AfterUnDeploy;%0A
 import o
@@ -622,69 +622,8 @@
 xt;%0A
-import org.jboss.arquillian.test.spi.event.suite.AfterClass;%0A
 impo
@@ -1023,93 +1023,8 @@
 );%0A%0A
-    private final List%3CManagementClient%3E active = new ArrayList%3CManagementClient%3E();%0A
     
@@ -1100,24 +1100,52 @@
 upTask%3E();%0A%0A
+    private int count = 0;%0A%0A
     public s
@@ -1924,16 +1924,17 @@
      for
+ 
 (Class%3C?
@@ -2248,16 +2248,17 @@
      for
+ 
 (int i =
@@ -2283,17 +2283,16 @@
 ze(); ++
- 
 i) %7B%0A   
@@ -2306,16 +2306,17 @@
       if
+ 
 (classes
@@ -2638,16 +2638,17 @@
      for
+ 
 (ServerS
@@ -2736,26 +2736,15 @@
     
-active.add(client)
+count++
 ;%0A  
@@ -2813,19 +2813,28 @@
 fter
-Class event
+UnDeploy afterDeploy
 ) th
@@ -2883,70 +2883,9 @@
  for
-(final ManagementClient client : active) %7B%0A                for
+ 
 (fin
@@ -2933,36 +2933,32 @@
                 
-    
 instance.tearDow
@@ -2963,34 +2963,32 @@
 own(
-client);%0A                %7D
+managementClient.get());
 %0A   
@@ -3034,24 +3034,32 @@
     
-active.clear();%0A
+if (--count == 0) %7B%0A    
     
@@ -3079,24 +3079,38 @@
 nt.clear();%0A
+            %7D%0A
         %7D%0A  

Fix bug with ServerSetupObserver