/*******************************************************************************
 * Copyright (c) 2012-2016 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.api.machine;

import com.google.common.base.Strings;
import com.google.gwt.user.client.Window;

import org.eclipse.che.api.machine.shared.Constants;
import org.eclipse.che.api.machine.shared.dto.MachineDto;
import org.eclipse.che.api.machine.shared.dto.ServerDto;
import org.eclipse.che.ide.util.loging.Log;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * Describe development machine instance.
 * Must contains all information that need to communicate with dev machine such as links, type, environment variable and etc.
 *
 * @author Vitalii Parfonov
 */
public class DevMachine {

    private final MachineDto devMachineDescriptor;

    private final Map<String, DevMachineServer> servers;

    private final Map<String, String> runtimeProperties;

    private final Map<String, String> envVariables;

    public DevMachine(@NotNull MachineDto devMachineDescriptor) {
        this.devMachineDescriptor = devMachineDescriptor;

        Map<String, ServerDto> serverDtoMap = devMachineDescriptor.getRuntime().getServers();
        servers = new HashMap<>(serverDtoMap.size());
        for (String s : serverDtoMap.keySet()) {
            servers.put(s, new DevMachineServer(serverDtoMap.get(s)));
        }
        runtimeProperties = devMachineDescriptor.getRuntime().getProperties();
        envVariables = devMachineDescriptor.getRuntime().getEnvVariables();

    }

    public Map<String, String> getEnvVariables() {
        return envVariables;
    }


    public Map<String, String> getRuntimeProperties() {
        return runtimeProperties;
    }

    public String getType() {
        return devMachineDescriptor.getConfig().getType();
    }

    public String getWsAgentWebSocketUrl() {
        DevMachineServer server = getServer(Constants.WSAGENT_REFERENCE);
        if (server != null) {
            String url = server.getUrl();
            String extUrl = url.substring(url.indexOf(':'), url.length());
            boolean isSecureConnection = Window.Location.getProtocol().equals("https:");
            return (isSecureConnection ? "wss" : "ws") + extUrl + "/ws/" + getWorkspace();
        } else {
            //should not be
            String message = "Reference " + Constants.WSAGENT_REFERENCE + " not found in DevMachine description";
            Log.error(getClass(), message);
            throw new RuntimeException(message);
        }
    }

    /**
     *
     * @return return base URL to the ws agent REST services. URL will be always without trailing slash
     */
    public String getWsAgentBaseUrl() {
        DevMachineServer server = getServer(Constants.WSAGENT_REFERENCE);
        if (server != null) {
            String url = server.getUrl();
            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }
            return url;
        } else {
            //should not be
            String message = "Reference " + Constants.WSAGENT_REFERENCE + " not found in DevMachine description";
            Log.error(getClass(), message);
            throw new RuntimeException(message);
        }
    }

    public Map<String, DevMachineServer> getServers() {
        return servers;
    }




    public DevMachineServer getServer(String reference) {
        if (!Strings.isNullOrEmpty(reference)) {
            for (DevMachineServer server : servers.values()) {
                if (reference.equals(server.getRef())) {
                    return server;
                }
            }
        }
        return null;
    }

    public String getWorkspace(){
        return devMachineDescriptor.getWorkspaceId();
    }

    public String getId() {
        return devMachineDescriptor.getId();
    }

}

@@ -2533,34 +2533,29 @@
     
-boolean isSecureConnection
+final String protocol
  = W
@@ -2599,16 +2599,31 @@
 https:%22)
+ ? %22wss%22 : %22ws%22
 ;%0A      
@@ -2639,61 +2639,124 @@
 urn 
-(isSecureConnection ? %22wss%22 : %22ws%22) + extUrl + %22/ws/%22
+protocol%0A                   + extUrl%0A                   + (extUrl.endsWith(%22/%22) ? %22ws/%22 : %22/ws/%22)%0A                  
  + g

fix usage of wsagent websocket url in case of trailing slash (#1270)