package com.aripd.aricl.hoverpulse;

import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import org.primefaces.component.api.Widget;

@FacesComponent(value = HoverPulse.COMPONENT_TYPE)
@ResourceDependencies({
    @ResourceDependency(library = "primefaces", name = "jquery/jquery.js"),
    @ResourceDependency(library = "primefaces", name = "core.js"),
    @ResourceDependency(library = "hoverpulse", name = "jquery.hoverpulse.js"),
    @ResourceDependency(library = "hoverpulse", name = "hoverpulse.js")})
public class HoverPulse extends UIComponentBase implements Widget {

    public static final String COMPONENT_TYPE = "com.aripd.aricl.HoverPulse";
    public static final String COMPONENT_FAMILY = "com.aripd.aricl";
    public static final String DEFAULT_RENDERER = "com.aripd.aricl.HoverPulseRenderer";

    protected static enum PropertyKeys {

        size,
        speed;
    }

    public HoverPulse() {
        setRendererType(DEFAULT_RENDERER);
    }

    @Override
    public String getFamily() {
        return COMPONENT_FAMILY;
    }

    public Integer getSize() {
        return (Integer) this.getStateHelper().eval(PropertyKeys.size, 40);
    }

    public void setSize(Integer size) {
        this.getStateHelper().put(PropertyKeys.size, size);
    }

    public Integer getSpeed() {
        return (Integer) this.getStateHelper().eval(PropertyKeys.speed, 400);
    }

    public void setSpeed(Integer speed) {
        this.getStateHelper().put(PropertyKeys.speed, speed);
    }

    @Override
    public String resolveWidgetVar() {
        FacesContext context = getFacesContext();
        String userWidgetVar = (String) getAttributes().get("widgetVar");

        if (userWidgetVar != null) {
            return userWidgetVar;
        } else {
            return "widget_"
                    + getClientId(context).replaceAll(
                            "-|" + UINamingContainer.getSeparatorChar(context),
                            "_");
        }
    }

}
