/*
 * generated by Xtext 2.12.0
 */
package org.etsi.mts.tdl.formatting2

import com.google.inject.Inject
import org.eclipse.xtext.formatting2.AbstractFormatter2
import org.eclipse.xtext.formatting2.IFormattableDocument
import org.etsi.mts.tdl.Comment
import org.etsi.mts.tdl.ElementImport
import org.etsi.mts.tdl.PackageableElement
import org.etsi.mts.tdl.services.TPLan2GrammarAccess
import org.etsi.mts.tdl.structuredobjectives.PICS
import org.etsi.mts.tdl.TestConfiguration
import org.etsi.mts.tdl.ComponentInstance
import org.etsi.mts.tdl.Connection
import org.etsi.mts.tdl.Element
import org.etsi.mts.tdl.Annotation
import org.etsi.mts.tdl.tdlPackage
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder
import org.etsi.mts.tdl.structuredobjectives.StructuredTestObjective
import org.eclipse.xtext.xbase.formatting2.XbaseFormatter
import static org.etsi.mts.tdl.structuredobjectives.StructuredObjectivesPackage.Literals.*
import static org.etsi.mts.tdl.tdlPackage.Literals.*
import org.etsi.mts.tdl.structuredobjectives.ExpectedBehaviour
import org.etsi.mts.tdl.structuredobjectives.EventSequence
import org.etsi.mts.tdl.structuredobjectives.EventOccurrence
import org.etsi.mts.tdl.structuredobjectives.Value
import org.etsi.mts.tdl.structuredobjectives.EventOccurrenceSpecification

class TPLan2Formatter extends AbstractFormatter2 {
	
	@Inject extension TPLan2GrammarAccess

	def dispatch void format(org.etsi.mts.tdl.Package _package, extension IFormattableDocument document) {
		// TODO: format HiddenRegions around keywords, attributes, cross references, etc. 

	    interior(
       		_package.regionFor.keyword('Domain').prepend[newLine],
        	_package.regionFor.keyword('}').prepend[newLine],
        	[indent]
    	)

	    interior(
       		_package.regionFor.keyword('{').append[newLine],
        	_package.regionFor.keyword('}').prepend[newLine],
        	[indent]
    	)


		_package.allRegionsFor.keyword('entities').prepend[newLine]
		_package.allRegionsFor.keyword('events').prepend[newLine]
		_package.allRegionsFor.keyword(';').append[newLine]
		_package.allRegionsFor.keyword(':').append[newLine]
		_package.allRegionsFor.keyword('}').prepend[indent]

		for (ElementImport elementImport : _package.getImport()) {
			elementImport.format;
		}
		for (PackageableElement packageableElement : _package.getPackagedElement()) {
			packageableElement.prepend[newLine]
			packageableElement.format
		}
		for (org.etsi.mts.tdl.Package __package : _package.getNestedPackage()) {
			__package.format
		}
		for (Comment comment : _package.getComment()) {
			comment.format
		}
		for (org.etsi.mts.tdl.Annotation _annotation : _package.getAnnotation()) {
			_annotation.format
		}
	}
	
	def dispatch void format(PackageableElement p, extension IFormattableDocument document) {
	    p.allRegionsFor.keyword(';').append[newLine]
	    interior(
       		p.regionFor.keyword('{').append[newLine],
        	p.regionFor.keyword('}').prepend[newLine],
        	[indent]
    	)
    	p.append[newLine]
	}

	def dispatch void format(StructuredTestObjective e, extension IFormattableDocument document) {
	    interior(
       		e.regionFor.keyword('{').append[newLine],
        	e.regionFor.keyword('}').prepend[newLine],
        	[indent]
    	)
    	e.regionFor.feature(ELEMENT__NAME).append[newLine]
    	e.regionFor.feature(TEST_OBJECTIVE__DESCRIPTION).append[newLine]
//    	e.regionFor.feature(TEST_OBJECTIVE__OBJECTIVE_URI).surround[newLine]

		e.expectedBehaviour.format

    	e.surround[indent]
    	e.append[newLine]
    	
	}

	def dispatch void format(ExpectedBehaviour e, extension IFormattableDocument document) {
	    e.prepend[newLine]
	    e.interior[indent]

   		e.allRegionsFor.keyword('ensure that').prepend[newLine]
   		e.allRegionsFor.keyword('{').append[newLine]
    	e.allRegionsFor.keyword('}').surround[newLine]

    	e.whenClause.format
    	e.thenClause.format
	}

	def dispatch void format(EventSequence e, extension IFormattableDocument document) {
		for (o : e.events) {
			o.format
		}
	}
	
	def dispatch void format(EventOccurrenceSpecification e, extension IFormattableDocument document) {
	    e.surround[newLine]
	    e.surround[indent]
	    e.eventArgument.format
	}

	def dispatch void format(Value e, extension IFormattableDocument document) {
	    e.surround[newLine]
	    e.surround[indent]
	    interior(
       		e.regionFor.keyword('containing').append[newLine],
        	e.regionFor.keyword(';').prepend[newLine],
        	[indent]
    	)
    	e.allRegionsFor.keyword(',').append[newLine]
	    
	}

	def dispatch void format(TestConfiguration e, extension IFormattableDocument document) {
	    interior(
       		e.regionFor.keyword('{').append[newLine],
        	e.regionFor.keyword('}').prepend[newLine],
        	[indent]
    	)
		for (i : e.componentInstance) {
			i.format	
		}
		for (c : e.connection) {
			c.format
		}
	}

	def dispatch void format(ComponentInstance e, extension IFormattableDocument document) {
		e.regionFor.keyword(';').append[newLine]
	}

	def dispatch void format(Connection e, extension IFormattableDocument document) {
		e.regionFor.keyword(';').append[newLine]
		for (g : e.endPoint) {
			g.regionFor.keyword('.').prepend[noSpace].append[noSpace]
		}
	}
	
	def dispatch void format(Element e, extension IFormattableDocument document) {
		e.prepend[newLine]
	    
	    interior(
	       	e.regionFor.keyword('{').append[newLine],
        	e.regionFor.keyword('}').prepend[newLine],
        	[indent]
    	)

		for (c : e.getComment()) {
			c.format;
		}
		for (a : e.getAnnotation()) {
			a.format
		}

	}
	

	def dispatch void format(PICS pICS, extension IFormattableDocument document) {
		// TODO: format HiddenRegions around keywords, attributes, cross references, etc. 
		for (Comment comment : pICS.getComment()) {
			comment.format;
		}
	}
	
	def dispatch void format(Annotation e, extension IFormattableDocument document) {
		if (e.value.isNullOrEmpty) {
			e.prepend[newLine]
		    e.regionFor.keyword(';').append[newLine]
		} else {
			e.prepend[newLine]
		    e.regionFor.keyword(';').append[newLine]
		}
	}

	def dispatch void format(Comment e, extension IFormattableDocument document) {
		e.prepend[newLine]
	}
	
	// TODO: implement for PICSReference, Entity, Event, EventSpecificationTemplate, EventOccurrenceSpecification, StructuredTestObjective, InitialConditions, ExpectedBehaviour, FinalConditions, EventSequence, RepeatedEventSequence, EventTemplateOccurrence, EntityBinding, TimeConstraint, EntityReference, EventReference, LiteralValue, Content, DataReference, DataInstanceUse, ParameterBinding, ContentReference, LiteralValueReference, StructuredDataType, StructuredDataInstance, MemberAssignment, TestConfiguration, Connection, ComponentType
}
