package simulator.interfaces.html;

import models.algebra.Constant;
import models.algebra.Expression;
import models.algebra.Term;
import models.dataConstraintModel.DataConstraintModel;
import models.dataConstraintModel.JsonTerm;
import models.dataConstraintModel.ResourcePath;
import models.dataFlowModel.DataTransferChannel;
import simulator.Resource;
import simulator.Simulator;
import simulator.interfaces.NativeSender;

public class HtmlElementTextSender extends NativeSender implements IRestEventListener{

	public HtmlElementTextSender(Simulator simulator, DataTransferChannel channel, ResourcePath resourcePath,
			Resource resource) {
		super(simulator, channel, resourcePath, resource);
	}

	@Override
	public void onEvent(String method, JsonTerm message) {
		if(method.equals("onTextChanged")) {
			String newText = message.get("newText").toString();
			System.out.println(newText);
			Constant text = new Constant(newText, DataConstraintModel.typeString);
			Expression sendMessage = channel.getOutputChannelMembers().iterator().next().getStateTransition().getMessageExpression();
			Term termMessage = (Term)sendMessage.clone();
			termMessage.setChild(0, text);
			sendToModel(termMessage);
		}
	}

}
