package net.sourceforge.phpdt.internal.launching;

import java.io.File;

import net.sourceforge.phpeclipse.resourcesview.PHPProject;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;

public class InterpreterRunnerConfiguration {
	protected ILaunchConfiguration configuration;

	public InterpreterRunnerConfiguration(ILaunchConfiguration aConfiguration) {
		configuration = aConfiguration;
	}
	
	public String getAbsoluteFileName() {
		IPath path = new Path(getFileName());
		IProject project = getProject().getProject();

		return project.getLocation().toOSString() + "/" + getFileName();
	}
	
	public String getFileName() {
		String fileName = "";

		try {
			fileName = configuration.getAttribute(PHPLaunchConfigurationAttribute.FILE_NAME, "No file specified in configuration");
		} catch(CoreException e) {}
		
		return fileName.replace('\\', '/');
	}
	
	public PHPProject getProject() {
		String projectName = "";
		
		try {
			projectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
		} catch(CoreException e) {
			PHPLaunchingPlugin.log(e);
		}

		IProject project = PHPLaunchingPlugin.getWorkspace().getRoot().getProject(projectName);

		PHPProject phpProject = new PHPProject();
		phpProject.setProject(project);
		return phpProject;
	}
	
	public File getAbsoluteWorkingDirectory() {
		String file = null;
		try {
			file = configuration.getAttribute(PHPLaunchConfigurationAttribute.WORKING_DIRECTORY, "");
		} catch(CoreException e) {
			PHPLaunchingPlugin.log(e);
		}
		return new File(file);
	}
	
	public String getInterpreterArguments() {
		try {
			return configuration.getAttribute(PHPLaunchConfigurationAttribute.INTERPRETER_ARGUMENTS, "");
		} catch(CoreException e) {}
		
		return "";
	}
	
	public String getProgramArguments() {
		try {
			return configuration.getAttribute(PHPLaunchConfigurationAttribute.PROGRAM_ARGUMENTS, "");
		} catch (CoreException e) {}
		
		return "";
	}

	public PHPInterpreter getInterpreter() {
		String selectedInterpreter = null;
		try {
			selectedInterpreter = configuration.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
		} catch(CoreException e) {}
		
		return PHPRuntime.getDefault().getInterpreter(selectedInterpreter);
	}
}