/*******************************************************************************
 * Copyright (c) 2017 Pivotal, Inc.
 * 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:
 *     Pivotal, Inc. - initial API and implementation
 *******************************************************************************/
package org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.springframework.ide.vscode.commons.util.StringUtil;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Provides existing Cloud Foundry client params, like target and credentials,
 * from the CLI config.json in the file system.
 * 
 *
 */
public class CfCliParamsProvider implements ClientParamsProvider {

	public static final String NO_CLI_TARGETS_FOUND_MESSAGE = "Unable to fetch information from Cloud Foundry. Please use cf CLI to configure and login to Cloud Foundry.";
	public static final String TARGET = "Target";
	public static final String REFRESH_TOKEN = "RefreshToken";
	public static final String ORGANIZATION_FIELDS = "OrganizationFields";
	public static final String SPACE_FIELDS = "SpaceFields";
	public static final String NAME = "Name";
	public static final String SSL_DISABLED = "SSLDisabled";

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.
	 * ClientParamsProvider#getParams()
	 */
	@Override
	public List<CFClientParams> getParams() throws NoTargetsException, ExecutionException {
		List<CFClientParams> params = new ArrayList<>();

		try {
			File file = getConfigJsonFile();
			if (file != null) {
				ObjectMapper mapper = new ObjectMapper();
				Map<String, Object> userData = mapper.readValue(file, Map.class);
				if (userData != null) {
					String refreshToken = (String) userData.get(REFRESH_TOKEN);
					// Only support connecting to CF via refresh token for now
					if (isRefreshTokenSet(refreshToken)) {
						CFCredentials credentials = CFCredentials.fromRefreshToken(refreshToken);
						boolean sslDisabled = (Boolean) userData.get(SSL_DISABLED);
						String target = (String) userData.get(TARGET);
						Map<String, Object> orgFields = (Map<String, Object>) userData.get(ORGANIZATION_FIELDS);
						Map<String, Object> spaceFields = (Map<String, Object>) userData.get(SPACE_FIELDS);
						if (target != null && orgFields != null && spaceFields != null) {
							String orgName = (String) orgFields.get(NAME);
							String spaceName = (String) spaceFields.get(NAME);
							params.add(new CFClientParams(target, null, credentials, orgName, spaceName, sslDisabled));
						}
					}
				}
			}
		} catch (IOException | InterruptedException e) {
			throw new ExecutionException(e);
		}

		if (params.isEmpty()) {
			throw new NoTargetsException(
					NO_CLI_TARGETS_FOUND_MESSAGE);
		} else {
			return params;
		}
	}

	private boolean isRefreshTokenSet(String token) {
		return StringUtil.hasText(token);
	}

	private File getConfigJsonFile() throws IOException, InterruptedException {
		// Support Unix systems for now
		if (!System.getProperty("os.name").toLowerCase().startsWith("win")) {
			String homeDir = getUnixHomeDir();
			if (homeDir != null) {
				if (!homeDir.endsWith("/")) {
					homeDir += '/';
				}
				String filePath = homeDir + ".cf/config.json";
				File file = new File(filePath);
				if (file.exists() && file.canRead()) {
					return file;
				}
			}
		}
		return null;
	}

	private String getUnixHomeDir() throws IOException, InterruptedException {
		return System.getProperty("user.home");
	}

}

@@ -3368,115 +3368,8 @@
 n %7B%0A
-%09%09// Support Unix systems for now%0A%09%09if (!System.getProperty(%22os.name%22).toLowerCase().startsWith(%22win%22)) %7B%0A%09
 %09%09St
@@ -3382,28 +3382,24 @@
 omeDir = get
-Unix
 HomeDir();%0A%09
@@ -3393,25 +3393,24 @@
 tHomeDir();%0A
-%09
 %09%09if (homeDi
@@ -3421,25 +3421,24 @@
  null) %7B%0A%09%09%09
-%09
 if (!homeDir
@@ -3451,25 +3451,24 @@
 ith(%22/%22)) %7B%0A
-%09
 %09%09%09%09homeDir 
@@ -3478,20 +3478,18 @@
 '/';%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09Strin
@@ -3533,17 +3533,16 @@
 on%22;%0A%09%09%09
-%09
 File fil
@@ -3565,17 +3565,16 @@
 ePath);%0A
-%09
 %09%09%09if (f
@@ -3611,17 +3611,16 @@
 ) %7B%0A%09%09%09%09
-%09
 return f
@@ -3624,22 +3624,16 @@
 n file;%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -3648,24 +3648,24 @@
 n null;%0A%09%7D%0A%0A
+
 %09private Str
@@ -3675,12 +3675,8 @@
  get
-Unix
 Home

Allow CF CLI config check on all OS