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.MapTerm;
import simulator.Event;
import simulator.SystemState;
import simulator.interfaces.INativeReceiver;

public class HtmlElementVisibilityReceiver implements INativeReceiver{
	
	protected HtmlElement element;
	
	private final IWebSocketMessageSender ws;
	
	public HtmlElementVisibilityReceiver(HtmlElement elem, IWebSocketMessageSender ws){
		this.element = elem;
		this.ws = ws;
	}
	
	@Override
	public void onReceiveFromModel(Event event, SystemState nextSystemState) {
		Expression message = event.getMessage();
		if (message instanceof Term) {
			Expression visible = ((Term) message).getChild(0);
			if (visible instanceof Constant) {
				if (((Constant) visible).getSymbol() == DataConstraintModel.true_) {
					element.setStyle("display", "none");
					
					JsonTerm json = new JsonTerm();
					json.addMember("method", new Constant("changeStyle"));
					json.addMember("id", new Constant(element.getId()));
					MapTerm datas = new MapTerm();
					datas.insert("display", new Constant("none"));
					json.addMember("datas", datas);
					
					ws.send(json.toString());
					
					
				} else {
					element.resetStyle("display");
					
					JsonTerm json = new JsonTerm();
					json.addMember("method", new Constant("changeStyle"));
					json.addMember("id", new Constant(element.getId()));
					MapTerm datas = new MapTerm();
					datas.insert("display", new Constant("null"));
					json.addMember("datas", datas);
					
					ws.send(json.toString());
				}
			}
		}
	}

}
