package website.automate.jwebrobot.executor;

import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.automate.jwebrobot.context.GlobalExecutionContext;
import website.automate.jwebrobot.context.ScenarioExecutionContext;
import website.automate.jwebrobot.exceptions.StepsMustBePresentException;
import website.automate.jwebrobot.executor.action.StepExecutor;
import website.automate.jwebrobot.listener.ExecutionEventListeners;
import website.automate.jwebrobot.validator.ContextValidators;
import website.automate.waml.io.model.main.Scenario;
import website.automate.waml.io.model.main.action.Action;

@Service
public class DefaultScenarioExecutor implements ScenarioExecutor {

    private final WebDriverProvider webDriverProvider;
    private final ExecutionEventListeners listener;
    private final ContextValidators validator;
    private final ScenarioPatternFilter scenarioPatternFilter;
    private final StepExecutor stepExecutor;

    @Autowired
    public DefaultScenarioExecutor(
        WebDriverProvider webDriverProvider,
        ExecutionEventListeners listener,
        ContextValidators validator,
        ScenarioPatternFilter scenarioPatternFilter,
        StepExecutor stepExecutor
    ) {
        this.webDriverProvider = webDriverProvider;
        this.listener = listener;
        this.validator = validator;
        this.scenarioPatternFilter = scenarioPatternFilter;
        this.stepExecutor = stepExecutor;
    }

    @Override
    public void execute(GlobalExecutionContext context) {
        listener.beforeExecution(context);
        validator.validate(context);

        try{
            for (Scenario scenario : context.getScenarios()) {
                execute(context, scenario);
            }
        } catch (Exception e){
            listener.errorExecution(context, e);
            throw e;
        }

        listener.afterExecution(context);
    }

    private void execute(GlobalExecutionContext context, Scenario scenario){
        ExecutorOptions options = context.getOptions();

        if (scenarioPatternFilter.isExecutable(options.getScenarioPattern(), scenario.getName())){
            WebDriver driver = webDriverProvider.createInstance(options.getWebDriverType(), options.getWebDriverUrl());

            if(options.isMaximizeWindow() == Boolean.TRUE){
                driver.manage().window().maximize();
            }

            ScenarioExecutionContext scenarioExecutionContext = new ScenarioExecutionContext(context, scenario, driver);
            try {
                runScenario(scenario, scenarioExecutionContext);
            } catch (Exception e){
                listener.errorScenario(scenarioExecutionContext, e);
                throw e;
            }
            finally {
                driver.quit();
            }
        }
    }

    @Override
    public void runScenario(Scenario scenario, ScenarioExecutionContext scenarioExecutionContext) {
        listener.beforeScenario(scenarioExecutionContext);

        scenarioExecutionContext.setScenario(scenario);

        if (scenario.getSteps() == null) {
            throw new StepsMustBePresentException(scenario.getName());
        }

        for (Action action : scenario.getSteps()) {
            stepExecutor.execute(action, scenarioExecutionContext);
        }

        listener.afterScenario(scenarioExecutionContext);
    }
}

@@ -77,16 +77,73 @@
 Driver;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -197,16 +197,16 @@
 owired;%0A
-
 import o
@@ -759,16 +759,63 @@
 ction;%0A%0A
+import static java.text.MessageFormat.format;%0A%0A
 @Service
@@ -883,16 +883,192 @@
 utor %7B%0A%0A
+    private static final String TEMPLATE_SCENARIO_START_OK_LOG_MESSAGE = %22ok: %7B0%7D %3E %7B1%7D%22;%0A%0A    private Logger logger = LoggerFactory.getLogger(DefaultScenarioExecutor.class);%0A%0A
     priv
@@ -2504,16 +2504,118 @@
 me()))%7B%0A
+            logger.info(format(TEMPLATE_SCENARIO_START_OK_LOG_MESSAGE, scenario.getName(), %22Start%22));%0A
         
@@ -3251,24 +3251,24 @@
 ver.quit();%0A
-
             
@@ -3261,32 +3261,132 @@
 ;%0A            %7D%0A
+            logger.info(format(TEMPLATE_SCENARIO_START_OK_LOG_MESSAGE, scenario.getName(), %22End%22));%0A
         %7D%0A    %7D%0A

Revert "Do not log scenario start/end"