/*
 * generated by Xtext 2.9.0
 */
package org.xtext.example.mydsl.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext



/**
 * Generates code from your model files on save.
 * 
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
 */
class EinDslGenerator extends AbstractGenerator {



	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {


		//fsa.generateFile("/DEFAULT_ARTIFACT",resource.generateQuestionModel)


	}

/*	def generateQuestionModel(Resource resource){

		 var root = new RootNode
		 root.addChildren(resource.getSelectNode)

		 var gson = new Gson();

		 var json = gson.toJson(root);

		 '''
          «json»
         '''

	}


	def Node getSelectNode(Resource resource)

	{
		var measureNames =resource.allContents.filter(typeof(MeasureList)).map[MeasureList].toIterable
		var aggregation =resource.allContents.filter(typeof(Aggregation)).map[agg].toIterable
		var group  =resource.allContents.filter(typeof(GroupBy)).map[group].toIterable.reduce[a,b|a]


		var select = new SelectNode
		var nn = new NameNode("test")
		var fn =new FunctionNode(AggregationType.SUM);


        for(meas:measureNames)
        {
			//change this
			 nn = new NameNode(meas.name)
        }

		for(agg:aggregation)
		{
			var aggregationType = agg.getAggregationFromString



			fn = new FunctionNode(aggregationType);
		}

		fn.addChildren(nn);
		select.addChildren(fn);

		if(!group.isNullOrEmpty)
			for(Node node :group.getNonAggregatedNodes)
			{
				select.addChildren(node)

			}


		return select;

	}

	def AggregationType getAggregationFromString(String operation) {

		val aggMap= newHashMap("average"  -> "avg","total"->"sum","count"->"count")

		var opp=aggMap.get(operation)

		if (!opp.isNullOrEmpty) {
			return AggregationType.valueOf(opp.toUpperCase());
		}
		return null;
	}


	def List<Node> getNonAggregatedNodes(EList<EObject> dimensions)
	{

		var list =newArrayList

		for(dim:dimensions)
		{
			if(dim instanceof Dimension) {
				var nn = new NameNode(dim.dimension.get(0))
				list.add(nn)
			}
			if(dim instanceof TimeGroup)
			{
				var nn = new NameNode(dim.timeGroup)
				list.add(nn)
			}
		}
		return list

	}*/
}
