package org.museautomation.core.task.state;

import org.museautomation.builtins.plugins.input.*;
import org.museautomation.builtins.plugins.state.*;
import org.museautomation.core.*;
import org.museautomation.core.context.*;
import org.museautomation.core.execution.*;
import org.museautomation.core.task.*;

/**
 * Runs tasks in the context of a StateContainer and InputProviders
 *
 * @author Christopher L Merrill (see LICENSE.txt for license details)
 */
public class StatefulTaskRunner
    {
    public StatefulTaskRunner(MuseProject project)
        {
        _project = project;
        }

    public StatefulTaskResult runTask(MuseTask task, InputProvider provider)
        {
        // prep run configuration and state plugins
        TaskConfiguration run_config = new BasicTaskConfiguration(task);
        run_config.addPlugin(new InputProviderPlugin(provider));
        run_config.addPlugin(new InjectInputsPlugin(new InjectInputsPluginConfiguration()));
        run_config.addPlugin(new InjectStatePlugin(new InjectStatePluginConfiguration()));
        run_config.addPlugin(new ExtractStatePlugin(new ExtractStatePluginConfiguration()));
        run_config.addPlugin(new StateContainerPlugin(_states));
        
        BlockingThreadedTaskRunner runner = new BlockingThreadedTaskRunner(new ProjectExecutionContext(_project), run_config);
        runner.runTask();

        TaskResult task_result = TaskResult.find(runner.getExecutionContext());
        if (task_result == null)
            return new StatefulTaskResult("failed - unable to find the result!");
        if (task_result.isPass())
            // TODO verify the state was extracted
            return new StatefulTaskResult("passed");
        else
            return new StatefulTaskResult("failed");

        }

    public StateContainer getStateContainer()
        {
        return _states;
        }

    public void setStateContainer(StateContainer container)
        {
        _states = container;
        }

    private MuseProject _project;
    private StateContainer _states = new BasicStateContainer();

    public static class StatefulTaskResult
        {
        public StatefulTaskResult(String message)
            {
            _message = message;
            }

        @Override
        public String toString()
            {
            return _message;
            }

        private String _message;
        }
    }
@@ -452,16 +452,44 @@
 ls)%0A */%0A
+@SuppressWarnings(%22unused%22)%0A
 public c
@@ -830,16 +830,50 @@
 (task);%0A
+        if (provider != null)%0A    
         
@@ -1760,16 +1760,8 @@
 ult(
-%22passed%22
 );%0A 
@@ -1825,17 +1825,16 @@
 iled%22);%0A
-%0A
         
@@ -2181,32 +2181,305 @@
 esult%0A        %7B%0A
+        StatefulTaskResult()%0A            %7B%0A            _passed = true;%0A            _message = %22task completed successfully%22;%0A            %7D%0A%0A        StatefulTaskResult(String message)%0A            %7B%0A            _passed = false;%0A            _message = message;%0A            %7D%0A%0A
         public S
@@ -2488,32 +2488,48 @@
 tefulTaskResult(
+boolean passed, 
 String message)%0A
@@ -2551,34 +2551,336 @@
            _
-message = 
+passed = passed;%0A            _message = message;%0A            %7D%0A%0A        public boolean passed()%0A            %7B%0A            return _passed;%0A            %7D%0A%0A        public boolean failed()%0A            %7B%0A            return !_passed;%0A            %7D%0A%0A        public String getMessage()%0A            %7B%0A            return _
 message;%0A   
@@ -3016,16 +3016,22 @@
 private 
+final 
 String _
@@ -3031,31 +3031,70 @@
 tring _message;%0A
+        private final boolean _passed;%0A
         %7D%0A    %7D

ENHANCE - Add pass/fail to StatefulTaskResult