package simulator.interfaces.html;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import models.algebra.Constant;
import models.dataConstraintModel.JsonTerm;
import models.dataConstraintModel.MapTerm;

public class HtmlElement {

	private final String type;
	private final String id;
	private String text;
	private Map<String, String> styles;
	private Set<String> classes;
	private IRestEventListener restEventListener;
	
	HtmlElement(String type, String id, String text){
		this.type = type;
		this.id = id.replace("\"", "");
		this.text = text;
		styles = new HashMap<>();
		classes = new HashSet<>();
	}
	
	public void setStyle(String property, String value) {
		styles.put(property, value);
	}
	
	public void resetStyle(String property) {
		styles.remove(property);
	}
	
	public void addClass(String className) {
		classes.add(className);
	}
	
	public void removeClass(String className) {
		classes.remove(className);
	}
	
	public MapTerm toMap() {
		MapTerm res = new MapTerm();
		res.insert("\"type\"", new Constant("\"" + type + "\""));
		res.insert("\"id\"", new Constant("\"" + id + "\""));
		res.insert("\"text\"", new Constant(text));
		MapTerm stylesMap = new MapTerm();
		for(String prop : styles.keySet()) {
			stylesMap.insert(prop, new Constant(styles.get(prop)));
		}
		res.insert("\"styles\"", stylesMap);
		String classTmp = "\"" + String.join(" ", classes) + "\"";
		res.insert("\"class\"", new Constant(classTmp));
		return res;
	}
	
	
	public void onRestEvent(String method, JsonTerm message) {
		restEventListener.onEvent(method, message);
	}
	
	
	public String getType() {
		return type;
	}

	public String getId() {
		return id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	public void setRestEventListener(IRestEventListener event) {
		this.restEventListener = event;
	}

}
