package io.github.aparnachaudhary.registrant;

import io.github.aparnachaudhary.registry.EndpointId;
import io.github.aparnachaudhary.registry.EndpointInfo;
import io.github.aparnachaudhary.registry.EndpointRegistry;
import io.github.aparnachaudhary.registry.EndpointStatus;
import io.github.aparnachaudhary.registry.event.EndpointAdded;
import io.github.aparnachaudhary.registry.event.EndpointRemoved;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Aparna
 */
@ApplicationScoped
public class ApplicationRegistrant {

    private static final Logger LOG = LoggerFactory.getLogger(ApplicationRegistrant.class);

    private static final String NODE_NAME = "jboss.node.name";
    private static final String PRODUCER_APP_NAME = "feed-producer";

    @Inject
    private EndpointRegistry endpointRegistry;

    @Resource(lookup = "java:app/AppName")
    private String appName;

    /**
     * @param init
     */
    public void onInitialize(@Observes @Initialized(ApplicationScoped.class) Object init) {
        LOG.info("Started : {}", init);
        register();
    }

    /**
     * @param init
     */
    public void onDestroy(@Observes @Destroyed(ApplicationScoped.class) Object init) {
        LOG.info("Destroyed : {}", init);
        unregister();
    }

    /**
     * @param endpointAdded
     */
    public void onEndpointAdded(@Observes EndpointAdded endpointAdded) {
        EndpointId endpointId = endpointAdded.getEndpointId();
        LOG.info("=========== endpointAdded: {} ===========", endpointId);
        register();
    }

    /**
     * @param endpointRemoved
     */
    public void onEndpointRemoved(@Observes EndpointRemoved endpointRemoved) {
        EndpointId endpointId = endpointRemoved.getEndpointId();
        LOG.info("=========== endpointRemoved: {} ===========", endpointId);
        if (!endpointRegistry.existsDependencies(getLocalEndpoint())) {
            unregister();
        }
    }

    private void register() {
        EndpointInfo endpointInfo = getLocalEndpoint();
        if (endpointRegistry.existsDependencies(endpointInfo)) {
            endpointRegistry.addEndpoint(endpointInfo);
        } else {
            LOG.warn("All dependent Services are not yet available for {}", endpointInfo.getEndpointId());
        }
    }

    private void unregister() {
        EndpointInfo consumerEndpointInfo = endpointRegistry.getEndpoint(getLocalEndpoint().getEndpointId());
        if (consumerEndpointInfo != null) {
            endpointRegistry.removeEndpoint(getLocalEndpoint().getEndpointId());
        }
    }

    private EndpointInfo getLocalEndpoint() {
        EndpointId endpointId = EndpointId.EndpointIdBuilder.newBuilder()
                .setNodeName(System.getProperty(NODE_NAME)).setAppName(appName)
                .createEndpointId();

        Set<EndpointId> endpoints = new HashSet<>();

        InitialContext initialContext = null;
        try {
            initialContext = new InitialContext();
            String dependencies = (String) initialContext.lookup("java:app/serviceDependency");
            if (dependencies != null) {
                LOG.info("$$$$$$$$$ {}", dependencies);
                EndpointId dependencyId = EndpointId.EndpointIdBuilder.newBuilder()
                        .setAppName(dependencies)
                        .createEndpointId();
                endpoints.add(dependencyId);
            }
        } catch (NameNotFoundException e) {
            LOG.info("No dependencies defined");
        } catch (NamingException e) {
            e.printStackTrace();
        }

        return EndpointInfo.EndpointInfoBuilder.newBuilder()
                .setEndpointId(endpointId)
                .setStatus(EndpointStatus.STARTING)
                .setDependencies(endpoints)
                .createEndpointInfo();
    }

}

@@ -1301,16 +1301,50 @@
 pName;%0A%0A
+    private String dependencies;%0A%0A
     /**%0A
@@ -1494,32 +1494,441 @@
 d : %7B%7D%22, init);%0A
+        InitialContext initialContext = null;%0A        try %7B%0A            initialContext = new InitialContext();%0A            dependencies = (String) initialContext.lookup(%22java:app/serviceDependency%22);%0A        %7D catch (NameNotFoundException e) %7B%0A            LOG.info(%22No dependencies defined%22);%0A        %7D catch (NamingException e) %7B%0A            throw new IllegalStateException(%22Failed to intialize%22);%0A        %7D%0A
         register
@@ -2325,36 +2325,113 @@
   LOG.info(%22
-=========== 
+-------------------------------------------------------------------%22);%0A        LOG.info(%22
 endpointAdde
@@ -2431,36 +2431,24 @@
 intAdded: %7B%7D
- ===========
 %22, endpointI
@@ -2443,32 +2443,121 @@
 %22, endpointId);%0A
+        LOG.info(%22-------------------------------------------------------------------%22);%0A
         register
@@ -2779,20 +2779,97 @@
 fo(%22
-=========== 
+-------------------------------------------------------------------%22);%0A        LOG.info(%22
 endp
@@ -2887,20 +2887,8 @@
 : %7B%7D
- ===========
 %22, e
@@ -2891,32 +2891,121 @@
 %22, endpointId);%0A
+        LOG.info(%22-------------------------------------------------------------------%22);%0A
         if (!end
@@ -3270,49 +3270,235 @@
     
-endpointRegistry.addEndpoint(endpointInfo
+LOG.info(%22*******************************************************************%22);%0A            endpointRegistry.addEndpoint(endpointInfo);%0A            LOG.info(%22*******************************************************************%22
 );%0A 
@@ -3543,19 +3543,18 @@
 All 
-dependent S
+required s
 ervi
@@ -3838,74 +3838,260 @@
     
-endpointRegistry.removeEndpoint(getLocalEndpoint().getEndpointId()
+LOG.info(%22*******************************************************************%22);%0A            endpointRegistry.removeEndpoint(getLocalEndpoint().getEndpointId());%0A            LOG.info(%22*******************************************************************%22
 );%0A 
@@ -4402,219 +4402,8 @@
 );%0A%0A
-        InitialContext initialContext = null;%0A        try %7B%0A            initialContext = new InitialContext();%0A            String dependencies = (String) initialContext.lookup(%22java:app/serviceDependency%22);%0A    
     
@@ -4426,36 +4426,32 @@
 cies != null) %7B%0A
-    
             LOG.
@@ -4494,28 +4494,24 @@
             
- 
-   
 EndpointId d
@@ -4582,28 +4582,24 @@
             
-   
- 
 .setAppName(
@@ -4624,36 +4624,32 @@
                 
-    
 .createEndpointI
@@ -4657,36 +4657,32 @@
 ();%0A            
- 
-   
 endpoints.add(de
@@ -4698,186 +4698,8 @@
 d);%0A
-            %7D%0A        %7D catch (NameNotFoundException e) %7B%0A            LOG.info(%22No dependencies defined%22);%0A        %7D catch (NamingException e) %7B%0A            e.printStackTrace();%0A
     

fix dependencylogic