/*
 * generated by Xtext 2.25.0
 */
package org.etsi.mts.tdl.validation;

import java.net.URI;
import java.net.URISyntaxException;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.ecore.util.EObjectValidator;
import org.eclipse.epsilon.evl.emf.validation.CompositeEValidator;
import org.eclipse.epsilon.evl.emf.validation.EvlValidator;
import org.eclipse.ocl.pivot.internal.utilities.PivotDiagnostician.BasicDiagnosticWithRemove;
import org.eclipse.xtext.validation.AbstractValidationDiagnostic;
import org.eclipse.xtext.validation.Check;
import org.etsi.mts.tdl.Element;
import org.etsi.mts.tdl.TestConfiguration;
import org.etsi.mts.tdl.tdlPackage;

/**
 * This class contains custom validation rules. 
 *
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
 */
public class TDLtxValidator extends AbstractTDLtxValidator {
	private static boolean initialised;

	static {
//		try {
//			init();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	
	
	public static final String INVALID_NAME = "invalidName";
//
//	@Check
//	public void checkGreetingStartsWithCapital(Greeting greeting) {
//		if (!Character.isUpperCase(greeting.getName().charAt(0))) {
//			warning("Name should start with a capital",
//					TDLtxPackage.Literals.GREETING__NAME,
//					INVALID_NAME);
//		}
//	}
	private static EvlValidator evlValidator;

	
	public TDLtxValidator() {
		super();
		try {
//			init();
		} catch (Exception e) {
//			 TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void init() throws Exception {
		if (initialised) {
			return;
		}
		//TODO: goes a bit in circles
		//TODO: extract into an activator or something? also as command
		//TODO: works in principle, occasionally causes hangs and strange errors though...
		System.out.println("Init EVL validator..");
		// Assuming you have generated the metamodel code
		EPackage ePackage = tdlPackage.eINSTANCE;

		// Pass a model name if your script uses it
		// Pass a valid bundle ID as it used for reporting (if not in a plugin use your project name or similar)
		
		String modelName = "TDL"; //This seems to be important! Otherwise strange hangs occur -> TDL?
		String bundleId = "org.etsi.mts.tdl.constraints";
		URI evlScriptURI = new URI("platform:/plugin/"+bundleId+"/constraints/tdl-generated-ETSI-ES-203-119-1-V1.5.1-fixed.evl");
		//TODO: try out extension point integration
		//TODO: try out fixes -> can they be in a separate module?
		//TODO: add other constraints for TO/TC
		evlValidator = new EvlValidator(
		    evlScriptURI, modelName , ePackage.getNsURI(), bundleId);

		evlValidator.setShowErrorDialog(false);
		//TODO: extract to optional on demand / auto validation
		//TODO: this brings all the epsilon dependencies here as well
		//-> effectively requires constraints to be installed.. shall be plugged in ideally..
		//-> check again extension point integration..
//		registerValidator(ePackage);
		initialised = true;
	}

	private static void registerValidator(EPackage ePackage) {
		EValidator existingValidator = EValidator.Registry.INSTANCE.getEValidator(ePackage);
		if (existingValidator instanceof CompositeEValidator) {
		    ((CompositeEValidator) existingValidator).getDelegates().add(evlValidator);
		} else {
		    if (existingValidator == null) {
		        existingValidator = EObjectValidator.INSTANCE;
		    }
		    CompositeEValidator newValidator = new CompositeEValidator();
		    newValidator.getDelegates().add(existingValidator);
		    newValidator.getDelegates().add(evlValidator);
		    EValidator.Registry.INSTANCE.put(ePackage, newValidator);
		}
	}
	
	
//TODO: try to add individual element-based checks on demand with different priorities to reduce overhead when editing..

//	@Check
//	public void checkConfigurationsStartWithCapital(TestConfiguration c) {
//	if (!Character.isUpperCase(c.getName().charAt(0))) {
//		warning("Name should start with a capital",
//				tdlPackage.Literals.ELEMENT__NAME,
//				INVALID_NAME);
//	}
//}

	@Check
	public void checkConfig(TestConfiguration e) {
//		System.out.println("Checkxs: "+e);
//		BasicDiagnosticWithRemove.validate(e, null);
//		BasicDiagnostic d = new BasicDiagnostic();
//		if (!evlValidator.validate(e, d, null)) {
//			System.out.println("  Warning: ");
//			d.getChildren().forEach(m->System.out.println("    "+m.getMessage()));
//		};
	}
	
	@Check
	public void checkElement(Element e) {
//		System.out.println("Checks: "+e);
//		BasicDiagnosticWithRemove.validate(e, null);
//		BasicDiagnostic d = new BasicDiagnostic();
//		if (!evlValidator.validate(e, d, null)) {
//			System.out.println("  Warning: ");
//		};
//		d.getChildren().forEach(m->System.out.println("    "+m.getMessage()));
//        BasicDiagnostic diagnostics = createDefaultDiagnostic(e);

//		if (!Character.isUpperCase() {
//			warning("Name should start with a capital",
//					tdlPackage.Literals.ELEMENT__NAME,
//					INVALID_NAME);
//		}
}

	
}
