/*
 * The MIT License (MIT)
 * 
 * Copyright (c) <2014> <Ioannis Moutsatsos, Bruno P. Kinoshita>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.biouno.unochoice.model;

import hudson.Extension;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jenkinsci.plugins.scriptler.config.Script;
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
import org.kohsuke.stapler.DataBoundConstructor;

public class ScriptlerScript extends AbstractScript {

	/*
	 * Serial UID.
	 */
	private static final long serialVersionUID = -4833367326600112145L;

	private final String scriptlerScriptId;
	
	private final Map<String, String> parameters;

	@DataBoundConstructor
	public ScriptlerScript(String scriptlerScriptId, List<ScriptlerScriptParameter> parameters) {
		super();
		this.scriptlerScriptId = scriptlerScriptId;
		this.parameters = new HashMap<String, String>();
		if (parameters != null) {
			for (ScriptlerScriptParameter parameter : parameters) {
				this.parameters.put(parameter.getName(), parameter.getValue());
			}
		}
	}
	
	/**
	 * @return the scriptlerScriptId
	 */
	public String getScriptlerScriptId() {
		return scriptlerScriptId;
	}

	/**
	 * @return the parameters
	 */
	public Map<String, String> getParameters() {
		return parameters;
	}

	public Object eval() {
		return eval(null);
	}

	public Object eval(Map<String, String> parameters) {
		if (parameters != null && parameters.size() > 0)
			getParameters().putAll(parameters);
		return this.toGroovyScript().eval(this.getParameters());
	}
	
	// --- utility methods for conversion
	
	public GroovyScript toGroovyScript() {
		final Script scriptler   = ScriptHelper.getScript(getScriptlerScriptId(), true);
		return new GroovyScript(scriptler.script, null);
	}
	
	// --- descriptor
	
	@Extension
	public static class DescriptorImpl extends ScriptDescriptor {
		/*
		 * (non-Javadoc)
		 * 
		 * @see hudson.model.Descriptor#getDisplayName()
		 */
		@Override
		public String getDisplayName() {
			return "Scriptler Script"; 
		}
		
	}
	
}

@@ -1234,16 +1234,36 @@
 tension;
+%0Aimport hudson.Util;
 %0A%0Aimport
@@ -2453,16 +2453,161 @@
 ters) %7B%0A
+%09%09Map%3CString, String%3E evaledParameters = new HashMap%3CString, String%3E();%0A%09%09// if we have any parameter that came from UI, let's eval and use them%0A
 %09%09if (pa
@@ -2652,47 +2652,275 @@
 %3E 0)
+ %7B
 %0A%09%09%09
-getParameters().putAll(parameters);
+for (String key : this.getParameters().keySet()) %7B%0A%09%09%09%09String value = this.getParameters().get(key);%0A%09%09%09%09value = Util.replaceMacro((String) value, parameters);%0A%09%09%09%09evaledParameters.put(key, value);%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09evaledParameters = this.getParameters();%0A%09%09%7D
 %0A%09%09r
@@ -2952,24 +2952,22 @@
 ().eval(
-this.get
+evaled
 Paramete
@@ -2968,18 +2968,16 @@
 rameters
-()
 );%0A%09%7D%0A%09%0A

Fix issue #16 Uno-Choice Scriptler should accept a referenced parameter as a script parameter