/*
 *   Copyright 2013 - 2019 The Original Authors
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package org.elasticsoftware.elasticactors.cluster;

import org.elasticsoftware.elasticactors.ActorSystem;
import org.elasticsoftware.elasticactors.InternalActorSystemConfiguration;
import org.elasticsoftware.elasticactors.messaging.MessageQueueFactoryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Joost van de Wijgerd
 */
public final class RemoteActorSystems {
    private static final Logger logger = LoggerFactory.getLogger(RemoteActorSystems.class);
    private final Map<String,RemoteActorSystemInstance> remoteActorSystems = new HashMap<>();

    public RemoteActorSystems(InternalActorSystemConfiguration configuration,
                              InternalActorSystems cluster,
                              @Qualifier("remoteActorSystemMessageQueueFactoryFactory") MessageQueueFactoryFactory remoteActorSystemMessageQueueFactoryFactory) {
        // create but don't initialize the remote systems (this needs to happen on the rebalancing thread)
        configuration.getRemoteConfigurations().forEach(remoteConfiguration ->
                remoteActorSystems.put(remoteConfiguration.getClusterName(),
                        new RemoteActorSystemInstance(remoteConfiguration,cluster,
                                remoteActorSystemMessageQueueFactoryFactory.create(remoteConfiguration.getClusterName()))));
    }


    public void init() throws Exception {
        // initialize the remote systems
        remoteActorSystems.forEach((key, value) -> {
            try {
                value.init();
                logger.info("Added Remote ActorSystem [{}] with {} shards on Cluster [{}]", value.getName(), value.getNumberOfShards(), key);
            } catch(Exception e) {
                logger.error("Exception while initializing Remote ActorSystem [{}]", value.getName(), e);
            }
        });
    }

    @PreDestroy
    public void destroy() {
        remoteActorSystems.forEach((s, remoteActorSystemInstance) -> remoteActorSystemInstance.destroy());
    }

    public ActorSystem get(String clusterName,String actorSystemName) {
        RemoteActorSystemInstance actorSystem = remoteActorSystems.get(clusterName);
        if(actorSystem != null && actorSystem.getName().equals(actorSystemName)) {
            return actorSystem;
        } else {
            return null;
        }
    }

    public ActorSystem get(String actorSystemName) {
        List<RemoteActorSystemInstance> instances = remoteActorSystems.values().stream()
                .filter(remoteActorSystemInstance -> remoteActorSystemInstance.getName().equals(actorSystemName))
                .collect(Collectors.toList());
        if(!instances.isEmpty()) {
            if(instances.size() > 1) {
                // cannot determine which one to use,
                throw new IllegalArgumentException("Found multiple matching Remote ActorSystems, please use ActorSystems.get(clusterName, actorSystemName");
            } else {
                return instances.get(0);
            }
        } else {
            return null;
        }
    }
}

@@ -808,16 +808,89 @@
 ration;%0A
+import org.elasticsoftware.elasticactors.RemoteActorSystemConfiguration;%0A
 import o
@@ -952,32 +952,32 @@
 FactoryFactory;%0A
-
 import org.slf4j
@@ -1121,34 +1121,8 @@
 oy;%0A
-import java.util.HashMap;%0A
 impo
@@ -1449,26 +1449,8 @@
 tems
- = new HashMap%3C%3E()
 ;%0A%0A 
@@ -1862,16 +1862,37 @@
 %0A       
+ remoteActorSystems =
  configu
@@ -1927,39 +1927,77 @@
 ns()
-.forEach(remoteConfiguration -%3E
+%0A                .stream()%0A                .collect(Collectors.toMap(
 %0A   
@@ -2001,33 +2001,41 @@
                 
-r
+        R
 emoteActorSystem
@@ -2038,20 +2038,8 @@
 stem
-s.put(remote
 Conf
@@ -2039,33 +2039,34 @@
 temConfiguration
-.
+::
 getClusterName()
@@ -2063,18 +2063,16 @@
 sterName
-()
 ,%0A      
@@ -2088,16 +2088,39 @@
         
+ remoteConfiguration -%3E
  new Rem
@@ -2146,28 +2146,94 @@
 nce(
-remoteConfiguration,
+%0A                                remoteConfiguration,%0A                                
 clus
@@ -2320,16 +2320,57 @@
 .create(
+%0A                                        
 remoteCo

Create map inside constructor