package nl.hsac.fitnesse.fixture.slim;

import nl.hsac.fitnesse.fixture.Environment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Base class for Slim fixtures.
 */
public class SlimFixture {
    private static final Pattern PATTERN = Pattern.compile("<a href=\"(.*?)\">(.*?)</a>(.*)", Pattern.CASE_INSENSITIVE);
    private Environment environment = Environment.getInstance();

    /**
     * @return environment to be used.
     */
    protected Environment getEnvironment() {
        return environment;
    }

    protected String getUrl(String htmlLink) {
        String result = htmlLink;
        Matcher matcher = PATTERN.matcher(htmlLink);
        if (matcher.matches()) {
            result = matcher.group(1) + matcher.group(3);
        }
        return result;
    }

    /**
     * Removes result of wiki formatting (for e.g. email addresses) if needed.
     * @param rawValue value as received from Fitnesse.
     * @return rawValue if it was just text, cleaned version if it was not.
     */
    protected String cleanupValue(String rawValue) {
        String result = null;
        Matcher matcher = PATTERN.matcher(rawValue);
        if (matcher.matches()) {
            result = matcher.group(2);
        } else {
            result = rawValue;
        }
        return result;
    }

    public boolean waitSeconds(int i) {
        return waitMilliSeconds(i * 1000);
    }

    public boolean waitMilliSeconds(int i) {
        boolean result;
        try {
            Thread.sleep(i);
            result = true;
        } catch (InterruptedException e) {
            result = false;
        }
        return result;
    }
}

@@ -795,24 +795,608 @@
 ult;%0A    %7D%0A%0A
+    /**%0A     * Stores a (global) value so it can be accessed by other fixtures/pages.%0A     * @param symbolName name to store value under.%0A     * @param value value to store.%0A     */%0A    public void setGlobalValue(String symbolName, String value) %7B%0A        getEnvironment().setSymbol(symbolName, value);%0A    %7D%0A%0A    /**%0A     * Retrieves a (global) value, which was previously stored using #setGlobalValue().%0A     * @param symbolName name value was stored under.%0A     */%0A    public String getGlobalValue(String symbolName) %7B%0A        return getEnvironment().getSymbol(symbolName);%0A    %7D%0A%0A
     /**%0A    

Add support to use environment's Symbols as global variables by Slim fixtures