/*
 * Licensed to Hewlett-Packard Development Company, L.P. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
*/
package com.hp.score.lang.tests.operation;

import com.hp.score.lang.api.Slang;
import com.hp.score.lang.entities.CompilationArtifact;
import com.hp.score.lang.entities.ScoreLangConstants;
import com.hp.score.lang.runtime.env.RunEnvironment;
import com.hp.score.lang.runtime.env.RunEnvironment;
import org.eclipse.score.api.TriggeringProperties;
import org.eclipse.score.events.EventBus;
import org.eclipse.score.events.EventConstants;
import org.eclipse.score.events.ScoreEvent;
import org.eclipse.score.events.ScoreEventListener;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;

import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_ACTION_END;
import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_ACTION_ERROR;
import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_ACTION_START;
import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_EXECUTION_FINISHED;
import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_INPUT_END;
import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_INPUT_START;
import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_OUTPUT_END;
import static com.hp.score.lang.entities.ScoreLangConstants.EVENT_OUTPUT_START;
import static com.hp.score.lang.entities.ScoreLangConstants.SLANG_EXECUTION_EXCEPTION;

/*
 * Created by orius123 on 12/11/14.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring/systemTestContext.xml")
public class SystemsTestsParent {

    @Autowired
    protected EventBus eventBus;

    @Autowired
    protected Slang slang;

    private LinkedBlockingQueue<ScoreEvent> queue = new LinkedBlockingQueue<>();

    protected ScoreEvent trigger(CompilationArtifact compilationArtifact, Map<String, Serializable> userInputs) throws InterruptedException {
        Map<String, Serializable> executionContext = createExecutionContext(userInputs);
        TriggeringProperties triggeringProperties = TriggeringProperties
                .create(compilationArtifact.getExecutionPlan())
                .setDependencies(compilationArtifact.getDependencies())
                .setContext(executionContext);

        registerHandlers();
        slang.run(compilationArtifact, userInputs);
        ScoreEvent event;
        do {
            event = queue.take();
            Assert.assertNotSame("Error event has been thrown during execution", SLANG_EXECUTION_EXCEPTION, event.getEventType());
            System.out.println("Event received: " + event.getEventType() + " Data is: " + event.getData());
        } while(!EventConstants.SCORE_FINISHED_EVENT.equals(event.getEventType()));
        return event;
    }


    private static Map<String, Serializable> createExecutionContext(Map<String, Serializable> userInputs) {
        Map<String, Serializable> executionContext = new HashMap<>();
        executionContext.put(ScoreLangConstants.RUN_ENV, new RunEnvironment());
        executionContext.put(ScoreLangConstants.USER_INPUTS_KEY, (Serializable) userInputs);
        return executionContext;
    }

    private void registerHandlers() {
        Set<String> handlerTypes = new HashSet<>();
        handlerTypes.add(EventConstants.SCORE_FINISHED_EVENT);
        handlerTypes.add(EventConstants.SCORE_ERROR_EVENT);
        handlerTypes.add(EventConstants.SCORE_FAILURE_EVENT);
        handlerTypes.add(EVENT_ACTION_START);
        handlerTypes.add(EVENT_ACTION_END);
        handlerTypes.add(EVENT_ACTION_ERROR);
        handlerTypes.add(EVENT_INPUT_START);
        handlerTypes.add(EVENT_INPUT_END);
        handlerTypes.add(EVENT_OUTPUT_START);
        handlerTypes.add(EVENT_OUTPUT_END);
        handlerTypes.add(SLANG_EXECUTION_EXCEPTION);
        handlerTypes.add(EVENT_EXECUTION_FINISHED);
        eventBus.subscribe(new ScoreEventListener() {

            @Override
            public void onEvent(ScoreEvent event) {
                try {
                    queue.put(event);
                } catch(InterruptedException ignore) {}
            }
        }, handlerTypes);
    }
}

@@ -944,261 +944,8 @@
 ct;%0A
-import com.hp.score.lang.entities.ScoreLangConstants;%0Aimport com.hp.score.lang.runtime.env.RunEnvironment;%0Aimport com.hp.score.lang.runtime.env.RunEnvironment;%0Aimport org.eclipse.score.api.TriggeringProperties;%0Aimport org.eclipse.score.events.EventBus;%0A
 impo
@@ -1373,34 +1373,8 @@
 le;%0A
-import java.util.HashMap;%0A
 impo
@@ -1553,671 +1553,9 @@
 nts.
-EVENT_ACTION_END;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.EVENT_ACTION_ERROR;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.EVENT_ACTION_START;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.EVENT_EXECUTION_FINISHED;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.EVENT_INPUT_END;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.EVENT_INPUT_START;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.EVENT_OUTPUT_END;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.EVENT_OUTPUT_START;%0Aimport static com.hp.score.lang.entities.ScoreLangConstants.SLANG_EXECUTION_EXCEPTION
+*
 ;%0A%0A/
@@ -1761,57 +1761,8 @@
  %7B%0A%0A
-    @Autowired%0A    protected EventBus eventBus;%0A%0A
     
@@ -2028,354 +2028,8 @@
 n %7B%0A
-        Map%3CString, Serializable%3E executionContext = createExecutionContext(userInputs);%0A        TriggeringProperties triggeringProperties = TriggeringProperties%0A                .create(compilationArtifact.getExecutionPlan())%0A                .setDependencies(compilationArtifact.getDependencies())%0A                .setContext(executionContext);%0A%0A
     
@@ -2431,16 +2431,17 @@
  %7D while
+ 
 (!EventC
@@ -2534,400 +2534,8 @@
  %7D%0A%0A
-%0A    private static Map%3CString, Serializable%3E createExecutionContext(Map%3CString, Serializable%3E userInputs) %7B%0A        Map%3CString, Serializable%3E executionContext = new HashMap%3C%3E();%0A        executionContext.put(ScoreLangConstants.RUN_ENV, new RunEnvironment());%0A        executionContext.put(ScoreLangConstants.USER_INPUTS_KEY, (Serializable) userInputs);%0A        return executionContext;%0A    %7D%0A%0A
     
@@ -3236,16 +3236,13 @@
     
-eventBus
+slang
 .sub
@@ -3247,16 +3247,24 @@
 ubscribe
+OnEvents
 (new Sco
@@ -3441,16 +3441,17 @@
  %7D catch
+ 
 (Interru

make tests use new slang apis