/*******************************************************************************
 * Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
 * 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:
 *     Bruno Medeiros - initial API and implementation
 *******************************************************************************/
package com.googlecode.goclipse.core;

import java.io.File;

import melnorme.lang.ide.core.utils.prefs.StringPreference;

import org.eclipse.core.resources.IProject;

import com.googlecode.goclipse.tooling.GoArch;
import com.googlecode.goclipse.tooling.GoEnvironment;
import com.googlecode.goclipse.tooling.GoEnvironmentConstants;
import com.googlecode.goclipse.tooling.GoOs;
import com.googlecode.goclipse.tooling.GoPath;
import com.googlecode.goclipse.tooling.GoRoot;

public class GoProjectEnvironment {
	
	public static GoRoot getEffectiveGoRoot(IProject project) {
		String prefValue = getPrefValue(GoEnvironmentPrefConstants.GO_ROOT, project, GoEnvironmentConstants.GOROOT);
		if(prefValue == null) 
			return null;
		return new GoRoot(prefValue);
	}
	
	public static GoArch getEffectiveGoArch(IProject project) {
		String prefValue = getPrefValue(GoEnvironmentPrefConstants.GO_ARCH, project, GoEnvironmentConstants.GOARCH);
		if(prefValue == null) 
			return null;
		return new GoArch(prefValue);
	}
	
	public static GoOs getEffectiveGoOs(IProject project) {
		String prefValue = getPrefValue(GoEnvironmentPrefConstants.GO_OS, project, GoEnvironmentConstants.GOOS);
		if(prefValue == null) 
			return null;
		return new GoOs(prefValue);
	}
	
	public static GoPath getEffectiveGoPath(IProject project) {
		String goPathString = getEffectiveGoPathString(project);
		if(goPathString == null) 
			return null;
		return new GoPath(goPathString);
	}
	
	public static String getEffectiveGoPathString(IProject project) {
		String goPathPref = getPrefValue(GoEnvironmentPrefConstants.GO_PATH, project, GoEnvironmentConstants.GOPATH);
		if(project == null) {
			return goPathPref;
		}
		
		// Implicitly add project location to GOPATH
		return project.getLocation().toOSString() + File.pathSeparator + goPathPref;
	}
	
	protected static String getPrefValue(StringPreference stringPref, IProject project, String envAlternative) {
		String prefValue = stringPref.get(project);
		if(prefValue == null || prefValue.isEmpty()) {
			return System.getenv(envAlternative);
		}
		return prefValue;
	}
	
	/**
	 * @param project - can be null.
	 */
	public static GoEnvironment getGoEnvironment(IProject project) {
		GoRoot goRoot = getEffectiveGoRoot(project);
		GoArch goArch = getEffectiveGoArch(project);
		GoOs goOs = getEffectiveGoOs(project);
		GoPath goPath = getEffectiveGoPath(project);
		return new GoEnvironment(goRoot, goArch, goOs, goPath);
	}
	
}
@@ -1074,16 +1074,50 @@
 ronment 
+implements GoEnvironmentConstants 
 %7B%0D%0A%09%0D%0A%09p
@@ -1182,39 +1182,51 @@
 %0D%0A%09%09
-String prefValue = getPrefValue
+return new GoRoot(getEffectiveValue_NonNull
 (GoE
@@ -1272,112 +1272,14 @@
 t, G
-oEnvironmentConstants.GOROOT);%0D%0A%09%09if(prefValue == null) %0D%0A%09%09%09return null;%0D%0A%09%09return new GoRoot(prefValue
+OROOT)
 );%0D%0A
@@ -1353,39 +1353,51 @@
 %0D%0A%09%09
-String prefValue = getPrefValue
+return new GoArch(getEffectiveValue_NonNull
 (GoE
@@ -1443,112 +1443,14 @@
 t, G
-oEnvironmentConstants.GOARCH);%0D%0A%09%09if(prefValue == null) %0D%0A%09%09%09return null;%0D%0A%09%09return new GoArch(prefValue
+OARCH)
 );%0D%0A
@@ -1520,39 +1520,49 @@
 %0D%0A%09%09
-String prefValue = getPrefValue
+return new GoOs(getEffectiveValue_NonNull
 (GoE
@@ -1606,108 +1606,12 @@
 t, G
-oEnvironmentConstants.GOOS);%0D%0A%09%09if(prefValue == null) %0D%0A%09%09%09return null;%0D%0A%09%09return new GoOs(prefValue
+OOS)
 );%0D%0A
@@ -1685,30 +1685,26 @@
 %0D%0A%09%09
-String goPathString = 
+return new GoPath(
 getE
@@ -1736,89 +1736,8 @@
 ect)
-;%0D%0A%09%09if(goPathString == null) %0D%0A%09%09%09return null;%0D%0A%09%09return new GoPath(goPathString
 );%0D%0A
@@ -1836,25 +1836,38 @@
 ef = get
-PrefValue
+EffectiveValue_NonNull
 (GoEnvir
@@ -1908,31 +1908,8 @@
 ct, 
-GoEnvironmentConstants.
 GOPA
@@ -2138,17 +2138,30 @@
  get
-PrefValue
+EffectiveValue_NonNull
 (Str
@@ -2204,16 +2204,21 @@
 roject, 
+%0D%0A%09%09%09
 String e
@@ -2335,22 +2335,33 @@
 ) %7B%0D%0A%09%09%09
-return
+String envValue =
  System.
@@ -2391,43 +2391,142 @@
 %0D%0A%09%09
-%7D%0D%0A%09%09return prefValue;%0D%0A%09%7D%0D%0A%09%0D%0A%09/**
+%09return envValue == null ? %22%22 : envValue;%0D%0A%09%09%7D%0D%0A%09%09return prefValue;%0D%0A%09%7D%0D%0A%09%0D%0A%09/**%0D%0A%09 * @return %7B@link GoEnvironment%7D for given project.
 %0D%0A%09 

Fix possible NPEs.