package org.madore.damlengine;

import org.w3c.dom.*;

public final class TodoEmailAtOrDotElement extends TodoDefaultElement {

    public enum Type {
	AT("commercial-at", "commercial_at.png", "at sign", "arobase"),
	DOT("full-stop", "full_stop.png", "dot", "point");
	final String charName;
	final String imgName;
	final String enName;
	final String frName;
	Type(String charName, String imgName,
	     String enName, String frName) {
	    this.charName = charName;
	    this.imgName = imgName;
	    this.enName = enName;
	    this.frName = frName;
	}
    }

    public static class Factory extends TodoElement.Factory {
	final Type t;
	public Factory(Type t) {
	    super();
	    this.t = t;
	}
	@Override
	public TodoEmailAtOrDotElement newItem(Element node,
					       Context ctx,
					       TodoItem caller) {
	    return new TodoEmailAtOrDotElement(t, node, ctx, caller);
	}
    }

    final Type t;

    public TodoEmailAtOrDotElement(Type t,
				   Element node,
				   Context ctx,
				   TodoItem caller) {
	super(node, ctx, caller);
	this.t = t;
    }

    @Override
    public void handleNodeOnly() {
	if ( ! ( caller instanceof TodoEmailDespammedElement ) )
	    throw new IllegalArgumentException(node.getLocalName()+" node can only be child of email-despammed node");
	Element span0 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "span");
	String expLang = LangHelper.getLangNorec(node);
	String lang = LangHelper.getLangRec(node);
	if ( lang == null || ! ( lang.equals("en") || lang.equals("fr") ) ) {
	    lang = "en";
	    expLang = "en";
	}
	if ( expLang != null )
	    LangHelper.setLangNorec(span0, expLang);
	span0.setAttributeNS(null, "class", "replace-"+t.charName);
	node.getParentNode().replaceChild(span0, node);
	Element span1 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "span");
	span1.setAttributeNS(null, "class", "subreplace-"+t.charName);
	span0.appendChild(span1);
	Element img = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "img");
	img.setAttributeNS(null, "src",
			   (ctx.gc.uriToTop==null?"":ctx.gc.uriToTop)+"images/"+t.imgName);
	img.setAttributeNS(null, "alt", "["+(lang.equals("fr")?t.frName:t.enName)+"]");
	img.setAttributeNS(null, "height", "15");
	img.setAttributeNS(null, "width", "16");
	span1.appendChild(img);
    }

}