package org.fedorahosted.flies.rest.dto;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.fedorahosted.flies.LocaleId;


@XmlType(name="textFlowType", namespace=Namespaces.FLIES, propOrder={"content", "extensions"})
@XmlRootElement(name="text-flow", namespace=Namespaces.FLIES)
@XmlSeeAlso({
	TextFlowTargets.class
})
public class TextFlow extends AbstractBaseResource implements Resource{

	private String id;
	private LocaleId lang;
	private Integer version = 1;
	
	private String content;
	private List<Object> extensions;

	public TextFlow() {
	}
	
	public TextFlow(String id) {
		this.id = id;
	}
	
	public TextFlow(String id, LocaleId lang) {
		this(id);
		this.lang = lang;
	}

	@XmlAttribute(name="id", required=true)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@XmlAttribute(name="version", required=true)
	public Integer getVersion() {
		return version;
	}
	
	public void setVersion(Integer version) {
		this.version = version;
	}

	@XmlAnyElement(lax=true)
	public List<Object> getExtensions() {
		if(extensions == null)
			extensions = new ArrayList<Object>();
		return extensions;
	}

	@Override
	public <T> T getExtension(Class<T> clz){
		if(extensions == null)
			return null;
		for(Object o : extensions){
			if(clz.isInstance(o))
				return clz.cast(o);
		}
		return null;
	}

	@Override
	public <T> T getOrAddExtension(Class<T> clz) {
		T ext = getExtension(clz);
		if(ext == null){
			try {
				ext = clz.newInstance();
			} catch (Throwable e) {
				throw new RuntimeException("unable to create instance", e);
			}
		}
		return ext;
	}
	
	@XmlJavaTypeAdapter(type=LocaleId.class, value=LocaleIdAdapter.class)
	@XmlAttribute(name="lang", namespace=Namespaces.XML, required=false)
	public LocaleId getLang() {
		return lang;
	}
	
	public void setLang(LocaleId lang) {
		this.lang = lang;
	}

	@XmlElement(name="content",namespace=Namespaces.FLIES, required=true)
	public String getContent() {
		if(content == null)
			return "";
		return content;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public TextFlowTarget getTarget(LocaleId localeId){
		for(Object obj : getExtensions()){
			if(obj instanceof TextFlowTargets){
				TextFlowTargets tft = (TextFlowTargets) obj;
				for(TextFlowTarget t : tft.getTargets()){
					if(localeId.equals(t.getLang()))
						return t;
				}
			}
		}
		return null;
	}
	
	public void addTarget(TextFlowTarget target) {
		TextFlowTargets targets = null;
		for(Object obj : getExtensions()){
			if(obj instanceof TextFlowTargets){
				targets = (TextFlowTargets) obj;
				break;
			}
		}
		if(targets == null){
			targets = new TextFlowTargets();
			getExtensions().add(targets);
		}
		targets.getTargets().add(target);

	}
	
}

@@ -2546,32 +2546,216 @@
 ontent;%0D%0A%09%7D%0D%0A%09%0D%0A
+%09public boolean hasTargets() %7B%0D%0A%09%09return getExtension(TextFlowTargets.class) != null;%0D%0A%09%7D%0D%0A%09public TextFlowTargets getTargets()%7B%0D%0A%09%09return getExtension(TextFlowTargets.class);%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public TextFlow

add convenience methods for accessing TextFlowTargets