/*
 * generated by Xtext 2.22.0
 */
package org.etsi.mts.tdl.ui.contentassist;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor;
import org.etsi.mts.tdl.Member;
import org.etsi.mts.tdl.MemberAssignment;
import org.etsi.mts.tdl.StructuredDataType;
import org.etsi.mts.tdl.DataInstanceUse;
import org.etsi.mts.tdl.DataType;
import org.etsi.mts.tdl.StructuredDataInstance;

/**
 * See https://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#content-assist
 * on how to customize the content assistant.
 */
public class TDLan2ProposalProvider extends AbstractTDLan2ProposalProvider {
	@Override
	public void complete_MemberAssignment(EObject model, RuleCall ruleCall, ContentAssistContext context,
			ICompletionProposalAcceptor acceptor) {
		if (model instanceof StructuredDataInstance) {
			for (Member m : ((StructuredDataType)
					((StructuredDataInstance)model).getDataType()).allMembers()) {
				acceptor.accept(createCompletionProposal(m.getName(), context));
			}
		} else {
			super.complete_MemberAssignment(model, ruleCall, context, acceptor);
		}

	}
	@Override
	public void completeStructuredDataInstance_MemberAssignment(EObject model, Assignment assignment,
			ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		if (model instanceof StructuredDataInstance) {
			for (Member m : ((StructuredDataType)
					((StructuredDataInstance)model).getDataType()).allMembers()) {
				acceptor.accept(createCompletionProposal(m.getName(), context));
			}
		} else {
			super.completeStructuredDataInstance_MemberAssignment(model, assignment, context, acceptor);
		}

	}
	
	@Override
	public void completeParameterBinding_Parameter(EObject model, Assignment assignment, ContentAssistContext context,
			ICompletionProposalAcceptor acceptor) {
		DataType dataType = null;
		if (model instanceof DataInstanceUse) {
			//TODO: double check if scoping is not incomplete as well
			DataInstanceUse dataInstanceUse = (DataInstanceUse) model;
			if (dataInstanceUse.getDataInstance()!=null && dataInstanceUse.getDataInstance() instanceof StructuredDataInstance) {
				dataType = ((StructuredDataInstance)dataInstanceUse.getDataInstance()).getDataType();
			} else if (dataInstanceUse.getDataInstance()!=null) {
				dataType = (StructuredDataType) dataInstanceUse.getDataType();
			} else {
				if (model.eContainer() instanceof MemberAssignment) {
					dataType = (StructuredDataType) ((MemberAssignment) model.eContainer()).getMember().getDataType();
				} else {
					
				}
			}
		}
		//TODO: handle when parts are missing
		if (dataType!=null && dataType instanceof StructuredDataType) {
			for (Member m : ((StructuredDataType)dataType).allMembers()) {
//				acceptor.accept(createCompletionProposal(m.getName(), new StyledString(m.getName()), null, context));
				acceptor.accept(createCompletionProposal(m.getName(), context));
			}
		} else {
			super.completeParameterBinding_Parameter(model, assignment, context, acceptor);
		}
	}
	
	@Override
	public void completeMemberAssignment_Member(EObject model, Assignment assignment, ContentAssistContext context,
			ICompletionProposalAcceptor acceptor) {
		if (model instanceof StructuredDataInstance) {
			for (Member m : ((StructuredDataType)
					((StructuredDataInstance)model).getDataType()).allMembers()) {
				acceptor.accept(createCompletionProposal(m.getName(), context));
			}
		} else {
			super.completeMemberAssignment_Member(model, assignment, context, acceptor);
		}
	}
	
	@Override
	public void complete_Assignment(EObject model, RuleCall ruleCall, ContentAssistContext context,
			ICompletionProposalAcceptor acceptor) {
		super.complete_Assignment(model, ruleCall, context, acceptor);
	}
}
