package org.etsi.mts.tdl;

import java.util.List;

import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.resource.DerivedStateAwareResource;
import org.eclipse.xtext.resource.IDerivedStateComputer;

//TODO: move upstream
class TDLtxDerivedStateComputer implements IDerivedStateComputer {
	
	@Override
	public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
		List<EnumDataType> enums = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), EnumDataType.class);
		enums.forEach(e-> {
			e.getValue().forEach(v -> v.setDataType(e));
		});
//		//TODO: also for gate references (instead of in scope?)
//		List<GateReference> grs = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), GateReference.class);
//		grs.forEach(e-> e.setName(e.getName()==null ? 
//				null //e.getComponent().getName()+"_"+e.getGate().getName() 
//				: e.getName()));
//		grs.forEach(e-> System.out.println(e.getName()));
	}

	@Override
	public void discardDerivedState(DerivedStateAwareResource resource) {
		List<EnumDataType> enums = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), EnumDataType.class);
		enums.forEach(e-> {
			e.getValue().forEach(v -> v.setDataType(null));
		});
//		//TODO: also for gate references (instead of in scope?)
//		List<GateReference> grs = EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), GateReference.class);
//		grs.forEach(e-> e.setName(null));
	}
}