/**
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * 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.fusesource.fabric.openshift;

import org.fusesource.common.util.Maps;
import org.fusesource.common.util.Strings;
import org.fusesource.common.util.Systems;
import org.fusesource.fabric.api.Container;
import org.fusesource.fabric.api.ContainerAutoScaler;
import org.fusesource.fabric.api.Containers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 */
public class OpenShiftAutoScaler implements ContainerAutoScaler {
    private static final transient Logger LOG = LoggerFactory.getLogger(OpenShiftAutoScaler.class);

    private final OpenshiftContainerProvider containerProvider;

    public OpenShiftAutoScaler(OpenshiftContainerProvider containerProvider) {
        this.containerProvider = containerProvider;
    }

    @Override
    public void createContainers(String version, String profile, int count) throws Exception {
        CreateOpenshiftContainerOptions.Builder builder = createAuthoScaleOptions();
        if (builder != null) {
            // TODO this is actually generic to all providers! :)
            for (int i = 0; i < count; i++) {
                Container[] containers = containerProvider.getFabricService().getContainers();
                String name = Containers.createContainerName(containers, profile, containerProvider.getScheme());
                CreateOpenshiftContainerOptions options = builder.number(1).version(version).profiles(profile).name(name).build();
                LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
                containerProvider.create(options);
            }
        } else {
            LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration");
        }
    }

    protected CreateOpenshiftContainerOptions.Builder createAuthoScaleOptions() {
        CreateOpenshiftContainerOptions.Builder builder = CreateOpenshiftContainerOptions.builder();

        Map<String, String> properties = containerProvider.getProperties();

        String serverUrl = validateProperty(properties,
                "serverUrl",
                OpenshiftContainerProvider.PROPERTY_AUTOSCALE_SERVER_URL,
                "OPENSHIFT_BROKER_HOST",
                OpenShiftConstants.DEFAULT_SERVER_URL);

        String domain = validateProperty(properties,
                "domain",
                OpenshiftContainerProvider.PROPERTY_AUTOSCALE_DOMAIN,
                "OPENSHIFT_NAMESPACE",
                "");

        String login = validateProperty(properties,
                "login",
                OpenshiftContainerProvider.PROPERTY_AUTOSCALE_LOGIN,
                "OPENSHIFT_LOGIN",
                "");

        String password = validateProperty(properties,
                "login",
                OpenshiftContainerProvider.PROPERTY_AUTOSCALE_PASSWORD,
                "OPENSHIFT_PASSWORD",
                "");

        if (Strings.isNotBlank(domain) && Strings.isNotBlank(login) && Strings.isNotBlank(password)) {
            LOG.info("Using serverUrl: " + serverUrl + " domain: " + domain + " login: " + login + " password: " + password);
            return builder.serverUrl(serverUrl).domain(domain).login(login).password(password);
        } else {
            return null;
        }
    }


    protected String validateProperty(Map<String, String> properties, String name, String propertyName, String envVarName, String defaultValue) {
        String answer = Maps.stringValue(properties,
                propertyName,
                Systems.getEnvVar(envVarName, defaultValue));
        if (Strings.isNullOrBlank(answer)) {
            LOG.warn("No configured value for " + name + " in property " + propertyName + " or environment variable $" + envVarName);
        }
        return answer;
    }

    @Override
    public void destroyContainers(String profile, int count, List<Container> containers) {
        // TODO

    }
}

@@ -928,16 +928,64 @@
 ainers;%0A
+import org.fusesource.fabric.api.FabricService;%0A
 import o
@@ -1815,30 +1815,35 @@
     
-Container%5B%5D containers
+FabricService fabricService
  = c
@@ -1877,16 +1877,72 @@
 ervice()
+;%0A                Container%5B%5D containers = fabricService
 .getCont
@@ -2357,32 +2357,38 @@
     
-containerProvider.create
+fabricService.createContainers
 (opt
@@ -3917,35 +3917,8 @@
 ogin
- + %22 password: %22 + password
 );%0A 

use the proper createContainer API so that the container gets registered properly into ZooKeeper for #93