/**
 * 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.javascript.node;

import org.thingml.compilers.builder.Section;
import org.thingml.compilers.javascript.JSContext;
import org.thingml.compilers.javascript.JSSourceBuilder;
import org.thingml.compilers.javascript.JSSourceBuilder.JSClass;
import org.thingml.compilers.javascript.JSThingImplCompiler;
import org.thingml.xtext.thingML.Thing;

public class NodeJSThingImplCompiler extends JSThingImplCompiler {

	@Override
	protected String getThingPath(Thing thing, JSContext jctx) {
		return jctx.firstToUpper(thing.getName()) + ".js";
	}

	@Override
	protected Section createMainSection(Thing thing, JSSourceBuilder builder, JSContext jctx) {
		builder.append("'use strict';").append("");

		Section imports = builder.section("imports").lines();
		if (jctx.getContextAnnotation("hasEnum") != null && jctx.getContextAnnotation("hasEnum").equals("true")) {
			imports.append("const Enum = require('./enums');");
		}		
		imports.append("const Event = require('./events');");
		imports.append("const StateJS = require('@steelbreeze/state');");
		imports.append("const EventEmitter = require('events').EventEmitter;");
		builder.append("");

		Section main = builder.section("main").lines();

		builder.append("module.exports = " + jctx.firstToUpper(thing.getName()) + ";");

		return main;
	}

	@Override
	protected JSClass newThingClass(Thing thing, Section parent, JSContext jctx) {
		return JSSourceBuilder.es5Class(parent, jctx.firstToUpper(thing.getName()));
	}

}

@@ -737,16 +737,62 @@
 ode;%0D%0A%0D%0A
+import org.eclipse.emf.ecore.util.EcoreUtil;%0D%0A
 import o
@@ -830,16 +830,16 @@
 ction;%0D%0A
-
 import o
@@ -1108,16 +1108,109 @@
 .Thing;%0D
+%0Aimport org.thingml.xtext.thingML.ThingMLPackage;%0D%0A%0D%0Aimport com.google.common.collect.Lists;%0D
 %0A%0D%0Apubli
@@ -1627,111 +1627,172 @@
 %0D%0A%09%09
-if (jctx.getContextAnnotation(%22hasEnum%22) != null && jctx.getContextAnnotation(%22hasEnum%22).equals(%22true%22)
+%0D%0A%09%09final Object literal = EcoreUtil.getObjectByType(Lists.newArrayList(thing.eAllContents()), ThingMLPackage.eINSTANCE.getEnumLiteralRef());%0D%0A%09%09if (literal != null
 ) %7B%0D

make eslint happier regarding unused enums