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 static String lastPage = null;
    private static String lastTop = null;
    private static int storyboardCount;
    private static int screenshotCount;
    private String screenshotPrefix;

    public StoryboardTable(Table table, String tableId, SlimTestContext context) {
        super(table, tableId, context);
        String fullPath = context.getPageToTest().getFullPath();
        screenshotPrefix = fullPath;
        if (!fullPath.equals(lastPage)) {
            lastTop = null;
            storyboardCount = 0;
            screenshotCount = 0;
            lastPage = fullPath;
        }
    }

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

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

    protected int getStoryboardCounter() {
        return storyboardCount;
    }

    protected String getScreenshotPrefix() {
        return screenshotPrefix;
    }

    protected int getScreenshotCounter() {
        return screenshotCount;
    }

    @Override
    protected List<SlimAssertion> invokeAction(int startingCol, int endingCol, int row, SlimExpectation expectation) {
        updateCounters();
        String screenshotName = getScreenshotName(row);
        List<SlimAssertion> realAssertions = super.invokeAction(startingCol, endingCol, row, expectation);
        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;
    }

    private void updateCounters() {
        String topTable = id.split("\\.")[0];
        if (!topTable.equals(lastTop)) {
            lastTop = topTable;
            storyboardCount++;
            screenshotCount = 0;
        }
        screenshotCount++;
    }

    protected String getScreenshotName(int row) {
        return String.format("%s/%s/%02d-%03d",
                                getTableKeyword(), getScreenshotPrefix(),
                                getStoryboardCounter(), getScreenshotCounter());
    }

    private class ScreenshotExpectation extends RowExpectation {
        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();
        }
    }
}

@@ -1698,90 +1698,8 @@
 ) %7B%0A
-        updateCounters();%0A        String screenshotName = getScreenshotName(row);%0A
     
@@ -1722,21 +1722,17 @@
 ertion%3E 
-realA
+a
 ssertion
@@ -1809,106 +1809,436 @@
     
-Instruction instruction = callFunction(getTableType() + %22Actor%22, getActionName(), screenshotName);
+addScreenshotAssertionForRow(assertions, row);%0A        return assertions;%0A    %7D%0A%0A    protected void addScreenshotAssertionForRow(List%3CSlimAssertion%3E assertions, int row) %7B%0A        int screenshotColumn = addScreenshotColumn(row);%0A        SlimAssertion screenshotAssertion = createAssertionForTakeScreenshot(screenshotColumn, row);%0A        assertions.add(screenshotAssertion);%0A    %7D%0A%0A    private int addScreenshotColumn(int row) %7B
 %0A   
@@ -2281,38 +2281,22 @@
         
-int screenshotColumn =
+return
  table.g
@@ -2324,28 +2324,41 @@
 w) - 1;%0A    
+%7D%0A%0A
     
+protected 
 SlimAssertio
@@ -2355,34 +2355,30 @@
 imAssertion 
-s
 cre
-enshot
+ate
 Assertion = 
@@ -2378,65 +2378,121 @@
 tion
- = makeAssertion(instruction, new ScreenshotExpectation(s
+ForTakeScreenshot(int screenshotColumn, int row) %7B%0A        updateCounters();%0A        String screenshotName = getS
 cree
@@ -2500,21 +2500,17 @@
 shot
-Column, 
+Name(
 row)
-)
 ;%0A  
@@ -2519,27 +2519,90 @@
     
-realAssertions.add(
+Instruction instruction = callFunction(getTableType() + %22Actor%22, getActionName(), 
 scre
@@ -2603,33 +2603,28 @@
 , screenshot
-Assertion
+Name
 );%0A        r
@@ -2633,12 +2633,12 @@
 urn 
-real
+make
 Asse
@@ -2634,33 +2634,95 @@
 rn makeAssertion
-s
+(instruction, new ScreenshotExpectation(screenshotColumn, row))
 ;%0A    %7D%0A%0A    pri
@@ -2720,21 +2720,23 @@
 %0A%0A    pr
-ivate
+otected
  void up
@@ -3245,21 +3245,23 @@
 %0A%0A    pr
-ivate
+otected
  class S
@@ -3501,24 +3501,56 @@
 expected) %7B%0A
+            int row = getRow();%0A
             
@@ -3594,32 +3594,27 @@
 e(getCol(), 
-getRow()
+row
 , actual);%0A 
@@ -3691,24 +3691,19 @@
 tCol(), 
-getRow()
+row
 , actual

Refactor storyboard code