/**
 *  Copyright (c) 2015-2017 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 ts.eclipse.ide.angular2.internal.cli.terminal;

import java.io.File;
import java.util.List;

import ts.eclipse.ide.angular2.cli.NgCommand;
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreter;
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreterFactory;

/**
 * Angular command interpeter factory.
 *
 */
public class NgCommandInterpreterFactory implements ICommandInterpreterFactory {

	@Override
	public ICommandInterpreter create(List<String> parameters, String workingDir) {
		NgCommand command = getCommand(parameters);
		if (command == null) {
			return null;
		}
		File projectDir = null;
		switch (command) {
		case NEW:
			projectDir = getNgNewProjectDir(parameters, workingDir);
			if (projectDir == null) {
				return null;
			}
			return new NgProjectCommandInterpreter(projectDir, workingDir);
		case INIT:
			projectDir = new File(workingDir);
			return new NgProjectCommandInterpreter(projectDir, workingDir);
		case GENERATE:
			String blueprint = getBluePrint(parameters);
			if (blueprint == null) {
				return null;
			}
			return new NgGenerateCommandInterpreter(blueprint, workingDir);
		case SERVE:
			return new NgServeCommandInterpreter(workingDir);
		case BUILD:
			return new NgBuildCommandInterpreter(parameters, workingDir);
		default:
			return null;
		}
	}

	private String getBluePrint(List<String> parameters) {
		if (parameters.size() < 2) {
			return null;
		}
		return parameters.get(1);
	}

	private NgCommand getCommand(List<String> parameters) {
		if (parameters.size() < 1) {
			return null;
		}
		return NgCommand.getCommand(parameters.get(0));
	}

	private File getNgNewProjectDir(List<String> parameters, String workingDir) {
		if (parameters.size() < 2) {
			return null;
		}
		String projectName = parameters.get(1);
		return new File(workingDir, projectName);
	}
}

@@ -691,16 +691,100 @@
 actory;%0D
+%0Aimport ts.eclipse.ide.terminal.interpreter.ICommandInterpreterParametersExtractor;%0D
 %0A%0D%0A/**%0D%0A
@@ -910,16 +910,56 @@
 rFactory
+, ICommandInterpreterParametersExtractor
  %7B%0D%0A%0D%0A%09@
@@ -2429,12 +2429,669 @@
 e);%0D%0A%09%7D%0D
+%0A%0D%0A%09@Override%0D%0A%09public String extractParameters(String cmdWithParameters) %7B%0D%0A%09%09int index = cmdWithParameters.indexOf(%22ng%22);%0D%0A%09%09if (index == -1) %7B%0D%0A%09%09%09return null;%0D%0A%09%09%7D%0D%0A%09%09if (index == 0) %7B%0D%0A%09%09%09return cmdWithParameters.substring(index + %22ng%22.length(), cmdWithParameters.length());%0D%0A%09%09%7D%0D%0A%09%09String start = cmdWithParameters.substring(index, cmdWithParameters.length());%0D%0A%09%09if (start.startsWith(%22ng.cmd %22)) %7B%0D%0A%09%09%09return cmdWithParameters.substring(index + %22ng.cmd %22.length(), cmdWithParameters.length());%0D%0A%09%09%7D%0D%0A%09%09if (start.startsWith(%22ng %22)) %7B%0D%0A%09%09%09return cmdWithParameters.substring(index + %22ng %22.length(), cmdWithParameters.length());%0D%0A%09%09%7D%0D%0A%09%09return null;%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Support ng.cmd for terminal interpreter.