package org.etsi.mts.tdl.ui.hover;

import java.util.Collection;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.ui.editor.hover.html.DefaultEObjectHoverProvider;
import org.etsi.mts.tdl.StructuredDataType;
import org.etsi.mts.tdl.TestDescription;
import org.etsi.mts.tdl.TestObjective;


public class TDLtxHoverProvider extends DefaultEObjectHoverProvider {
	protected String getHoverInfoAsHtml(EObject o) {
		String html = super.getHoverInfoAsHtml(o);
		if (o instanceof TestObjective) {
			//TODO: search within resource set?
			//TODO: basic PoC, elaborate further, e.g. links, other info 
			Collection<Setting> found = EcoreUtil.UsageCrossReferencer.find(o, o.eResource());
			String targets = found.stream().map(e -> ((TestDescription)e.getEObject()).getName()).collect(Collectors.joining("</li><li>"));
			for (Setting f : found) {
				f.getEObject();
			}
			html+="<br/></br>Referenced in: <b><ul><li>"+targets+"</li></ul></b></br</br>";
		}
		if (o instanceof StructuredDataType) {
			String members = ((StructuredDataType) o).allMembers().stream().map(e -> e.getName() + " : "+e.getDataType().getName()).collect(Collectors.joining("</li><li>"));
			html+="<br/></br>Members: <b><ul><li>"+members+"</li></ul></b></br</br>";
		}		
		return html;
	}

	protected String getHoverInfoAsHtml(TestObjective o) {
		String html = super.getHoverInfoAsHtml(o);
		System.out.println(html);
		return html;
	}
 
}