package org.richfaces.realworld.util;

import java.util.HashSet;
import java.util.Set;

import javax.faces.context.FacesContext;

import org.ajax4jsf.component.UIRepeat;
import org.jboss.seam.annotations.Name;
import org.richfaces.component.html.HtmlInputText;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.MetaTag;

@Name(value="imageHelper")
public class ImageHelper {

	HtmlInputText metaRef;
    private UIRepeat repeater;
    private Set<Integer> keys = null;
    
    /**
     * @return the keys
     */
    public Set getKeys() {
        return keys;
    }

    /**
     * @param keys the keys to set
     */
    public void setKeys(Set keys) {
        this.keys = keys;
    }

    public void setRepeater(UIRepeat repeater) {
        this.repeater = repeater;
    }

    public UIRepeat getRepeater() {
        return repeater;
    }

    public HtmlInputText getMetaRef() {
        return metaRef;
    }

    public void setMetaRef(HtmlInputText metaRef) {
        this.metaRef = metaRef;
    }
    
    public String change(Image image){
        
        HashSet keys = new HashSet<Integer>();
        int rowKey = getRepeater().getRowIndex();
        keys.add(rowKey);
        setKeys(keys);
        metaRef.processValidators(FacesContext.getCurrentInstance());
        metaRef.processUpdates(FacesContext.getCurrentInstance());
        MetaTag metaTag = new MetaTag();
		metaTag.setTag(metaRef.getValue().toString());
		metaTag.setParent(image);
        image.getTags().add(metaTag);
        return null;
    }
    
    public void addTag(Image image){
		MetaTag metaTag = new MetaTag();
		metaTag.setTag("");
		metaTag.setParent(image);
		image.getTags().add(metaTag);
		//image.getMeta().add("");
	}
	
	public void removeTag(MetaTag tag, Image image){
		image.getTags().remove(tag);
		//image.getMeta().remove(tag);
	}
}
