package nl.hsac.fitnesse.slim;

import fitnesse.slim.instructions.Instruction;
import fitnesse.testsystems.slim.SlimTestContext;
import fitnesse.testsystems.slim.Table;
import fitnesse.testsystems.slim.results.SlimTestResult;
import fitnesse.testsystems.slim.tables.ScriptTable;
import fitnesse.testsystems.slim.tables.SlimAssertion;
import fitnesse.testsystems.slim.tables.SlimExpectation;

import java.util.List;


/**
 * Script table which takes a 'screenshot' on each action.
 */
public class StoryboardTable extends ScriptTable {
    private String screenshotPrefix;

    public StoryboardTable(Table table, String tableId, SlimTestContext context) {
        super(table, tableId, context);
        screenshotPrefix = context.getPageToTest().getFullPath();
    }

    @Override
    protected String getTableKeyword() {
        return "storyboard";
    }

    protected String getActionName() {
        return "takeScreenshot";
    }

    protected String getScreenshotPrefix() {
        return screenshotPrefix;
    }

    @Override
    protected List<SlimAssertion> invokeAction(int startingCol, int endingCol, int row, SlimExpectation expectation) {
        List<SlimAssertion> realAssertions = super.invokeAction(startingCol, endingCol, row, expectation);
        String screenshotName = getScreenshotName(row);
        Instruction instruction = callFunction(getTableType() + "Actor", getActionName(), screenshotName);
        table.addColumnToRow(row, "");
        int screenshotColumn = table.getColumnCountInRow(row) - 1;
        SlimAssertion screenshotAssertion = makeAssertion(instruction, new ScreenshotExpectation(screenshotColumn, row));
        realAssertions.add(screenshotAssertion);
        return realAssertions;
    }

    protected String getScreenshotName(int row) {
        return String.format("%s/%s/%s.%s", getTableKeyword(), getScreenshotPrefix(), id, row);
    }

    private class ScreenshotExpectation extends RowExpectation {
        public ScreenshotExpectation(int col, int row) {
            super(col, row);
        }

        @Override
        protected SlimTestResult createEvaluationMessage(String actual, String expected) {
            try {
                table.substitute(getCol(), getRow(), actual);
            } catch (Throwable t) {
                table.substitute(getCol(), getRow(), actual + ": " + t.getMessage());
            }
            return SlimTestResult.plain();
        }
    }
}

@@ -528,16 +528,181 @@
 Table %7B%0A
+    private static String lastPage = null;%0A    private static String lastTop = null;%0A    private static int storyboardCount;%0A    private static int screenshotCount;%0A
     priv
@@ -862,32 +862,31 @@
         
-screenshotPrefix
+String fullPath
  = conte
@@ -915,24 +915,240 @@
 FullPath();%0A
+        screenshotPrefix = fullPath;%0A        if (!fullPath.equals(lastPage)) %7B%0A            lastTop = null;%0A            storyboardCount = 0;%0A            screenshotCount = 0;%0A            lastPage = fullPath;%0A        %7D%0A
     %7D%0A%0A    @
@@ -1304,32 +1304,114 @@
 enshot%22;%0A    %7D%0A%0A
+    protected int getStoryboardCounter() %7B%0A        return storyboardCount;%0A    %7D%0A%0A
     protected St
@@ -1471,32 +1471,114 @@
 tPrefix;%0A    %7D%0A%0A
+    protected int getScreenshotCounter() %7B%0A        return screenshotCount;%0A    %7D%0A%0A
     @Override%0A  
@@ -1690,24 +1690,106 @@
 ectation) %7B%0A
+        updateCounters();%0A        String screenshotName = getScreenshotName(row);%0A
         List
@@ -1887,64 +1887,8 @@
 n);%0A
-        String screenshotName = getScreenshotName(row);%0A
     
@@ -2315,145 +2315,515 @@
   pr
-otected String getScreenshotName(int row) %7B%0A        return String.format(%22%25s/%25s/%25s.%25s%22, getTableKeyword(), getScreenshotPrefix(), id, row
+ivate void updateCounters() %7B%0A        String topTable = id.split(%22%5C%5C.%22)%5B0%5D;%0A        if (!topTable.equals(lastTop)) %7B%0A            lastTop = topTable;%0A            storyboardCount++;%0A            screenshotCount = 0;%0A        %7D%0A        screenshotCount++;%0A    %7D%0A%0A    protected String getScreenshotName(int row) %7B%0A        return String.format(%22%25s/%25s/%2502d-%2503d%22,%0A                                getTableKeyword(), getScreenshotPrefix(),%0A                                getStoryboardCounter(), getScreenshotCounter()
 );%0A 
@@ -2901,23 +2901,16 @@
         
-public 
 Screensh

Update storyboard to number its images based on internal counters, so that it starts number at 1 for the storyboard table instance and then a sequential number for each row. This makes the image's filenames ordered alphabetically the same as the order they were taken in.