/**
 *  Copyright (c) 2013-2014 Angelo ZERR.
 *  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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */
package org.eclipse.angularjs.core;

import java.io.IOException;

import org.eclipse.angularjs.internal.core.Trace;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;

import tern.TernProject;
import tern.server.ITernPlugin;
import tern.server.TernDef;
import tern.server.TernPlugin;

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

/**
 * AngularJS Nature.
 * 
 */
public class AngularNature implements IProjectNature {

	public static final String ID = AngularCorePlugin.PLUGIN_ID
			+ ".angularnature"; //$NON-NLS-1$

	private IProject project;

	public void configure() throws CoreException {
		boolean saveRequired = false;
		
		// Add "angular" plugin
		TernProject ternProject = AngularProject.getTernProject(project);
		if (!containsPlugin(ternProject, TernPlugin.angular)) {
			saveRequired = true;
			ternProject.addPlugin(TernPlugin.angular);
		}
		
		// Add "browser" + "ecma5" JSON Type Def
		if (!containsLib(ternProject, TernDef.browser.name())) {
			saveRequired = true;
			ternProject.addLib(TernDef.browser.name());
		}
		if (!containsLib(ternProject, TernDef.ecma5.name())) {
			saveRequired = true;
			ternProject.addLib(TernDef.ecma5.name());
		}
		
		if (saveRequired) {
			try {
				ternProject.save();
			} catch (IOException e) {
				Trace.trace(Trace.SEVERE,
						"Error while configuring angular nature.", e);
			}
		}
	}

	public boolean containsPlugin(TernProject ternProject, ITernPlugin plugin) {
		JsonObject plugins = ternProject.getPlugins(); // No null can be returned here
		return (plugins.get(plugin.getName()) != null);
	}
	
	public boolean containsLib(TernProject ternProject, String libName) {
		JsonArray libs = ternProject.getLibs();
		if (libs != null) {
			for (JsonValue lib : libs) {
				if (lib.isString() && lib.asString().equals(libName))
					return true;
			}
		}
		return false;
	}
	
	public void deconfigure() throws CoreException {
		// Remove the nature-specific information here.
	}

	public IProject getProject() {
		return project;
	}

	public void setProject(IProject value) {
		project = value;
	}

}

@@ -673,40 +673,8 @@
 ct;%0A
-import tern.server.ITernPlugin;%0A
 impo
@@ -733,133 +733,8 @@
 n;%0A%0A
-import com.eclipsesource.json.JsonArray;%0Aimport com.eclipsesource.json.JsonObject;%0Aimport com.eclipsesource.json.JsonValue;%0A%0A
 /**%0A
@@ -1025,18 +1025,16 @@
  false;%0A
-%09%09
 %0A%09%09// Ad
@@ -1057,32 +1057,35 @@
 in%0A%09%09TernProject
+%3C?%3E
  ternProject = A
@@ -1134,36 +1134,30 @@
 f (!
-containsPlugin(ternProject, 
+ternProject.addPlugin(
 Tern
@@ -1203,60 +1203,12 @@
 ue;%0A
-%09%09%09ternProject.addPlugin(TernPlugin.angular);%0A
 %09%09%7D%0A
-%09%09
 %0A%09%09/
@@ -1250,36 +1250,24 @@
  Def%0A%09%09if (!
-containsLib(
 ternProject,
@@ -1257,34 +1257,40 @@
 if (!ternProject
-, 
+.addLib(
 TernDef.browser.
@@ -1330,76 +1330,17 @@
 ;%0A%09%09
-%09ternProject.addLib(TernDef.browser.name());%0A%09%09%7D%0A%09%09if (!containsLib(
+%7D%0A%09%09if (!
 tern
@@ -1346,18 +1346,24 @@
 nProject
-, 
+.addLib(
 TernDef.
@@ -1407,59 +1407,12 @@
 ue;%0A
-%09%09%09ternProject.addLib(TernDef.ecma5.name());%0A
 %09%09%7D%0A
-%09%09
 %0A%09%09i
@@ -1592,495 +1592,8 @@
 %09%7D%0A%0A
-%09public boolean containsPlugin(TernProject ternProject, ITernPlugin plugin) %7B%0A%09%09JsonObject plugins = ternProject.getPlugins(); // No null can be returned here%0A%09%09return (plugins.get(plugin.getName()) != null);%0A%09%7D%0A%09%0A%09public boolean containsLib(TernProject ternProject, String libName) %7B%0A%09%09JsonArray libs = ternProject.getLibs();%0A%09%09if (libs != null) %7B%0A%09%09%09for (JsonValue lib : libs) %7B%0A%09%09%09%09if (lib.isString() && lib.asString().equals(libName))%0A%09%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%09%0A
 %09pub

Use new Tern API to avoid adding same defs+plugins. See https://github.com/angelozerr/angularjs-eclipse/pull/56