/**
 * Licensed 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.
 *
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 */
package org.thingml.compilers.utils;

import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;

import org.thingml.compilers.ThingMLCompiler;
import org.thingml.compilers.configuration.CfgBuildCompiler;
import org.thingml.compilers.configuration.CfgMainGenerator;
import org.thingml.compilers.thing.ThingActionCompiler;
import org.thingml.compilers.thing.ThingApiCompiler;
import org.thingml.compilers.thing.ThingImplCompiler;
import org.thingml.xtext.constraints.ThingMLHelpers;
import org.thingml.xtext.thingML.Configuration;
import org.thingml.xtext.thingML.ThingMLModel;

/**
 * Created by ffl on 24.11.14.
 */
public abstract class OpaqueThingMLCompiler extends ThingMLCompiler {

	PrintStream m, e;

	public OpaqueThingMLCompiler(ThingActionCompiler thingActionCompiler, ThingApiCompiler thingApiCompiler, CfgMainGenerator mainCompiler, CfgBuildCompiler cfgBuildCompiler, ThingImplCompiler thingImplCompiler) {
		super(thingActionCompiler, thingApiCompiler, mainCompiler, cfgBuildCompiler, thingImplCompiler);
		final OutputStream stream = getMessageStream();
		if (stream != null) {
			m = new PrintStream(stream);
			e = new PrintStream(stream);
		}
	}

	public void println(String msg) {
		if (m != null)
			m.println(msg);
		else
			System.out.println(msg);
	}

	public void erroln(String msg) {
		if (e != null)
			e.println(msg);
		else
			System.err.println(msg);
	}

	@Override
	public void compile(Configuration cfg, String... options) {
		println("Running " + getName() + " compiler on configuration " + cfg.getName());
		final long start = System.currentTimeMillis();
		//Saving the complete model, e.g. to get all required inputs if there is a problem in the compiler
		ThingMLModel flatModel = flattenModel(ThingMLHelpers.findContainingModel(cfg));
		saveAsThingML(flatModel, new File(ctx.getOutputDirectory(), cfg.getName() + "_merged.thingml").getAbsolutePath());
		saveAsXMI(flatModel, new File(ctx.getOutputDirectory(), cfg.getName() + "_merged.xmi").getAbsolutePath());

		//compile
		do_call_compiler(cfg, options);
		println("Compilation complete. Took " + (System.currentTimeMillis() - start) + " ms.");
	}

	@Override
	public void compileConnector(String connector, Configuration cfg, String... options) {
		println("Running connector compiler " + connector + " on configuration " + cfg.getName());
		super.compileConnector(connector, cfg, options);
	}

	public abstract void do_call_compiler(Configuration cfg, String... options);

}

@@ -2456,24 +2456,103 @@
 odel(cfg));%0A
+%09%09new Thread(new Runnable() %7B%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09try %7B%0A%09%09%09
 %09%09saveAsThin
@@ -2656,16 +2656,19 @@
 ath());%0A
+%09%09%09
 %09%09saveAs
@@ -2768,16 +2768,127 @@
 ath());%0A
+%09%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09%09//We do not really care if something goes wrong here!%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D).start();
 %0A%09%09//com

save merged thingml and xmi files in a separate thread