package fr.treeptik.cloudunit.utils;

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

import fr.treeptik.cloudunit.docker.builders.ConfigBuilder;
import fr.treeptik.cloudunit.docker.builders.ContainerBuilder;
import fr.treeptik.cloudunit.docker.builders.HostConfigBuilder;
import fr.treeptik.cloudunit.docker.model.Config;
import fr.treeptik.cloudunit.docker.model.DockerContainer;
import fr.treeptik.cloudunit.docker.model.HostConfig;

/**
 * Created by guillaume on 11/01/16.
 */
public class ContainerUtils {

    public static DockerContainer newCreateInstance(String name, String imagePath, String imageSubType, List<String> volumesFrom,
            List<String> args, List<String> rawVolumes, List<String> envs,
            Map<String, String> ports, String networkMode, String suffixCloudUnitIO) {
        HostConfig hostConfig = HostConfigBuilder.aHostConfig().withVolumesFrom(volumesFrom).withBinds(rawVolumes)
                .withPublishAllPorts(false).withPortBindings(buildPortBindingBody(ports))
                .withNetworkMode(networkMode)
                .build();
        Config config = ConfigBuilder.aConfig().withAttachStdin(Boolean.FALSE).withAttachStdout(Boolean.TRUE)
                .withHostname(name)
                .withAttachStderr(Boolean.TRUE).withCmd(args).withImage(imagePath).withHostConfig(hostConfig).withMemory(0L)
                .withMemorySwap(0L).withEnv(envs).build();
        Map<String, String> labels = new HashMap<>();
        if ("webserver".equalsIgnoreCase(imageSubType)) {
            labels.put("traefik.port", "80");
        } else {
            labels.put("traefik.port", "8080");
        }
        labels.put("traefik.backend", name);
        labels.put("traefik.frontend.rule", "Host:" + name + suffixCloudUnitIO);
        config.setLabels(labels);
        DockerContainer container = ContainerBuilder.aContainer().withName(name).withConfig(config).build();
        return container;
    }

    public static DockerContainer newStartInstance(String name, List<String> volumesFrom, List<String> volumes,
            Boolean publishAllPorts) {
        HostConfig hostConfig = HostConfigBuilder.aHostConfig().withBinds(volumes).withPrivileged(Boolean.FALSE)
                .withPublishAllPorts(publishAllPorts).withVolumesFrom(volumesFrom).build();
        Config config = ConfigBuilder.aConfig().withHostConfig(hostConfig).build();
        DockerContainer container = ContainerBuilder.aContainer().withName(name).withConfig(config).build();
        return container;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static Map<String, List<Map<String, String>>> buildPortBindingBody(Map<String, String> ports) {
        if (ports == null) {
            return null;
        }
        Map finalMap = new HashMap();
            for (String port : ports.keySet()) {
                Map<String, String> mapForHostPort = new HashMap() {
                    private static final long serialVersionUID = 1L;
                    {
                        put("HostPort", ports.get(port));
                    }
                };
                List<Map<String, String>> params = Arrays.asList(mapForHostPort);
                finalMap.put(port, params);
            }
        return finalMap;

    }

}

@@ -657,16 +657,29 @@
 eSubType
+%0A            
 , List%3CS
@@ -697,28 +697,16 @@
 mesFrom,
-%0A           
  List%3CSt
@@ -740,16 +740,29 @@
 wVolumes
+%0A            
 , List%3CS
@@ -772,17 +772,16 @@
 ng%3E envs
-,
 %0A       
@@ -776,32 +776,34 @@
 envs%0A           
+ ,
  Map%3CString, Str
@@ -1747,16 +1747,121 @@
  name);%0A
+        String subdomain = System.getenv(%22CU_SUB_DOMAIN%22) == null ? %22%22 : System.getenv(%22CU_SUB_DOMAIN%22);%0A
         
@@ -1912,16 +1912,28 @@
 + name +
+ subdomain +
  suffixC

Correct the tag rule namings for traefik