/*
 *  [2012] - [2017] Codenvy, S.A.
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.machine;

import org.eclipse.che.api.machine.server.model.impl.ServerImpl;
import org.eclipse.che.plugin.docker.client.json.ContainerInfo;
import org.eclipse.che.plugin.docker.client.json.PortBinding;
import org.eclipse.che.plugin.docker.machine.ServerEvaluationStrategy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Represents server evaluation strategy for Codenvy. By default, calling
 * {@link ServerEvaluationStrategy#getServers(ContainerInfo, String, Map)} will return a completed
 * {@link ServerImpl} with internal and external address set to the address of the Docker host.
 *
 * @author Alexander Garagatyi
 * @see ServerEvaluationStrategy
 */
public class CodenvyDockerServerEvaluationStrategy extends ServerEvaluationStrategy {
    @Override
    protected Map<String, String> getInternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) {
        Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts();

        return getAddressesAndPorts(internalHost, portBindings);
    }

    @Override
    protected Map<String, String> getExternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) {
        return getInternalAddressesAndPorts(containerInfo, internalHost);
    }

    // TODO move into base class
    private Map<String, String> getAddressesAndPorts(String address, Map<String, List<PortBinding>> ports) {
        Map<String, String> addressesAndPorts = new HashMap<>();
        for (Map.Entry<String, List<PortBinding>> portEntry : ports.entrySet()) {
            // there is one value always
            String port = portEntry.getValue().get(0).getHostPort();
            addressesAndPorts.put(portEntry.getKey(), address + ":" + port);
        }
        return addressesAndPorts;
    }
}

@@ -880,34 +880,8 @@
 y;%0A%0A
-import java.util.HashMap;%0A
 impo
@@ -899,16 +899,16 @@
 l.List;%0A
+
 import j
@@ -1597,36 +1597,45 @@
   return get
+ExposedPortsTo
 Address
-esAnd
 Ports(intern
@@ -1785,32 +1785,32 @@
 internalHost) %7B%0A
+
         return g
@@ -1876,534 +1876,7 @@
    %7D
-%0A%0A    // TODO move into base class%0A    private Map%3CString, String%3E getAddressesAndPorts(String address, Map%3CString, List%3CPortBinding%3E%3E ports) %7B%0A        Map%3CString, String%3E addressesAndPorts = new HashMap%3C%3E();%0A        for (Map.Entry%3CString, List%3CPortBinding%3E%3E portEntry : ports.entrySet()) %7B%0A            // there is one value always%0A            String port = portEntry.getValue().get(0).getHostPort();%0A            addressesAndPorts.put(portEntry.getKey(), address + %22:%22 + port);%0A        %7D%0A        return addressesAndPorts;%0A    %7D
 %0A%7D%0A

move method to base class