package org.squonk.notebook.execution;

import org.squonk.execution.steps.StepDefinition;
import org.squonk.notebook.api.CellDTO;
import org.squonk.notebook.api.CellType;
import org.squonk.notebook.client.CallbackClient;
import org.squonk.execution.steps.StepExecutor;
import org.squonk.execution.variable.impl.CellCallbackClientVariableLoader;
import org.squonk.execution.variable.VariableManager;
import javax.inject.Inject;

/**
 *
 * @author timbo
 */
public abstract class AbstractStepExecutor implements QndCellExecutor {

    @Inject
    protected CallbackClient callbackClient;
    
    protected final String cellTypeName;
    
    
    public AbstractStepExecutor(String cellTypeName) {
        this.cellTypeName = cellTypeName;
    }

    protected abstract StepDefinition[] getStepDefintions(CellDTO cell);

    @Override
    public boolean handles(CellType cellType) {
        System.out.println("Testing cell type name " + cellType.getName() + " for " + cellTypeName);
        return cellType.getName().equals(cellTypeName);
    }

    @Override
    public void execute(String cellName) {
        VariableManager manger = new VariableManager(new CellCallbackClientVariableLoader(callbackClient, cellName));
        CellDTO cell = callbackClient.retrieveCell(cellName);

        StepDefinition[] steps = getStepDefintions(cell);

        StepExecutor executor = new StepExecutor(manger);
        try {
            executor.execute(steps, null);
        } catch (Exception e) {
            throw new RuntimeException("Failed to execute cell", e);
        }
    }

    protected StepDefinition configureOption(StepDefinition step, CellDTO cell, String option) {
        return step.withOption(option, cell.getOptionMap().get(option).getValue());
    }

}

@@ -164,16 +164,58 @@
 llType;%0A
+import org.squonk.notebook.api.OptionDTO;%0A
 import o
@@ -256,16 +256,16 @@
 Client;%0A
-
 import o
@@ -430,24 +430,25 @@
 bleManager;%0A
+%0A
 import javax
@@ -466,17 +466,115 @@
 ect;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A
- *%0A
  * @
@@ -663,16 +663,111 @@
 utor %7B%0A%0A
+    private static final Logger LOG = Logger.getLogger(AbstractStepExecutor.class.getName());%0A%0A
     @Inj
@@ -815,21 +815,17 @@
 Client;%0A
-    
 %0A
+
     prot
@@ -857,25 +857,17 @@
 peName;%0A
-    %0A    
+%0A
 %0A    pub
@@ -1111,26 +1111,16 @@
     
-System.out.println
+LOG.info
 (%22Te
@@ -1892,70 +1892,428 @@
     
-return step.withOption(option, cell.getOptionMap().get(option)
+OptionDTO dto = cell.getOptionMap().get(option);%0A        if (dto == null) %7B%0A            if (LOG.isLoggable(Level.INFO)) %7B%0A                String values = cell.getOptionMap().keySet().stream().collect(Collectors.joining(%22,%22));%0A                LOG.info(%22Option %22 + option + %22 not found. Possible values are %22 + values);%0A            %7D%0A            return step;%0A        %7D else %7B%0A            return step.withOption(option, dto
 .get
@@ -2318,21 +2318,31 @@
 etValue());%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

options should be optional