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)
 */
@SuppressWarnings("unused")
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);
        if (provider != null)
            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();
        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
        {
        StatefulTaskResult()
            {
            _passed = true;
            _message = "task completed successfully";
            }

        StatefulTaskResult(String message)
            {
            _passed = false;
            _message = message;
            }

        public StatefulTaskResult(boolean passed, String message)
            {
            _passed = passed;
            _message = message;
            }

        public boolean passed()
            {
            return _passed;
            }

        public boolean failed()
            {
            return !_passed;
            }

        public String getMessage()
            {
            return _message;
            }

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

        private final String _message;
        private final boolean _passed;
        }
    }
@@ -293,20 +293,83 @@
 on.core.
-task
+plugins.*;%0Aimport org.museautomation.core.task.*;%0A%0Aimport java.util
 .*;%0A%0A/**
@@ -893,16 +893,101 @@
 (task);%0A
+        for (MusePlugin plugin : _plugins)%0A            run_config.addPlugin(plugin);%0A
         
@@ -2194,35 +2194,190 @@
    p
-rivate MuseProject _project
+ublic void addPlugin(MusePlugin plugin)%0A        %7B%0A        _plugins.add(plugin);%0A        %7D%0A%0A    private MuseProject _project;%0A    private List%3CMusePlugin%3E _plugins = new ArrayList%3C%3E()
 ;%0A  

add MusePlugins to StatefulTaskRunner