/*
 * AutoTestBean.java		Date created: 13.10.2008
 * Last modified by: $Author$
 * $Revision$	$Date$
 */

package org.ajax4jsf.autotest.bean;

import java.util.Date;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.FacesEvent;
import javax.faces.event.ValueChangeEvent;

import org.ajax4jsf.javascript.ScriptUtils;
import org.richfaces.event.DropEvent;
import org.richfaces.event.NodeExpandedEvent;
import org.richfaces.event.NodeSelectedEvent;

/**
 * Bean for auto test
 * 
 * @author Andrey Markavtsov
 * 
 */
public class AutoTestBean {

    public static final String INPUT_TEXT = "Text";

    public static final String STATUS_ID = "_auto_status";

    public static final String UPDATE_MODEL_STATUS = "UpdateModel";

    public static final String ACTION_LISTENER_STATUS = "ActionListener";

    public static final String NESTED_ACTION_LISTENER_STATUS = "NestedListener";

    public static final String VALUE_CHANGE_LISTENER_STATUS = "ValueChangeListener";

    public static final String NODE_SELECTED_LISTENER_STATUS = "NodeSelectedListener";

    public static final String NODE_EXPANDED_LISTENER_STATUS = "NodeExpandedListener";

    public static final String VALIDATOR_ID = "autoTestValidator";

    public static final String VALIDATOR_DEFAULT_ID = "autoTestDefaultValidator";

    public static final String ONCOMPLETE = "window._ajaxOncomplete = true;";

    // private String input = INPUT_TEXT;

    private String reRender = STATUS_ID;

    private Boolean limitToList = false;

    private Boolean ajaxSingle = false;

    private Boolean immediate = false;

    private Boolean bypassUpdate = false;

    private Boolean rendered = true;

    private String status = null;

    private String oncomplete = ONCOMPLETE;

    private String validatorId = VALIDATOR_DEFAULT_ID;

    public void actionListener(ActionEvent event) {
        setStatus(getStatus() + ACTION_LISTENER_STATUS);
    }
    
    public void actionListener(DropEvent event) {
        setStatus(getStatus() + ACTION_LISTENER_STATUS);
    }
 
 
    public void valueChangeListener(ValueChangeEvent event) {
        setStatus(getStatus() + VALUE_CHANGE_LISTENER_STATUS);
    }

    public void processSelection(NodeSelectedEvent event) {
        setStatus(getStatus() + NODE_SELECTED_LISTENER_STATUS);
    }

    public void processExpansion(NodeExpandedEvent nodeExpandedEvent) {
        setStatus(getStatus() + NODE_EXPANDED_LISTENER_STATUS);
    }

    public String load() {
        status = null;
        return null;
    }

    public String reset() {
        status = null;
        return null;
    }

    // public String testRendered() {
    // reset();
    // rendered = false;
    // return null;
    // }
    //	
    // public String testReRender() {
    // reset();
    // reRender = TIME_ID + "," + STATUS_ID ;
    // return null;
    // }
    //	
    // public String testListeners() {
    // reset();
    // return null;
    // }
    //	
    // public String testFalidation() {
    // reset();
    // return null;
    // }
    //	
    // public String testAjaxSingle() {
    // reset();
    // ajaxSingle = true;
    // return null;
    // }
    //	
    // public String testImmdediate() {
    // reset();
    // immediate = true;
    // return null;
    // }
    //	
    // public String testBypassUpdate() {
    // reset();
    // bypassUpdate = true;
    // return null;
    // }
    //	
    // public String testLimitToList1() {
    // reset();
    // limitToList = true;
    // return null;
    // }
    //	
    // public String testLimitToList2() {
    // reset();
    // limitToList = true;
    // reRender = TIME_ID + "," + STATUS_ID;
    // return null;
    // }
    //	
    // public String testValidator() {
    // reset();
    // validatorId = VALIDATOR_ID;
    // return null;
    // }
    //	
    // public String testValidatorAndAjaxSingle() {
    // return null;
    // }

    public Object getRequestParamsMap() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (context != null) {
            ExternalContext ext = context.getExternalContext();
            if (ext != null) {
                return ScriptUtils.toScript(context.getExternalContext().getRequestParameterMap());
            }
        }
        return "";
    }

    public String getText() {
        return String.valueOf(new Date().getTime());
    }

    /**
     * @return the input
     */
    public String getInput() {
        return INPUT_TEXT;
    }

    /**
     * @param input
     *                the input to set
     */
    public void setInput(String input) {
        if (input != null && input.equals(INPUT_TEXT)) {
            setStatus(getStatus() + UPDATE_MODEL_STATUS);
        }
        // this.input = input;
    }

    /**
     * @return the reRender
     */
    public String getReRender() {
        return reRender;
    }

    /**
     * @param reRender
     *                the reRender to set
     */
    public void setReRender(String reRender) {
        this.reRender = reRender;
    }

    /**
     * @return the limitToList
     */
    public Boolean getLimitToList() {
        return limitToList;
    }

    /**
     * @param limitToList
     *                the limitToList to set
     */
    public void setLimitToList(Boolean limitToList) {
        this.limitToList = limitToList;
    }

    /**
     * @return the ajaxSingle
     */
    public Boolean getAjaxSingle() {
        return ajaxSingle;
    }

    /**
     * @param ajaxSingle
     *                the ajaxSingle to set
     */
    public void setAjaxSingle(Boolean ajaxSingle) {
        this.ajaxSingle = ajaxSingle;
    }

    /**
     * @return the immediate
     */
    public Boolean getImmediate() {
        return immediate;
    }

    /**
     * @param immediate
     *                the immediate to set
     */
    public void setImmediate(Boolean immediate) {
        this.immediate = immediate;
    }

    /**
     * @return the bypassUpdate
     */
    public Boolean getBypassUpdate() {
        return bypassUpdate;
    }

    /**
     * @param bypassUpdate
     *                the bypassUpdate to set
     */
    public void setBypassUpdate(Boolean bypassUpdate) {
        this.bypassUpdate = bypassUpdate;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        if (status == null) {
            status = "";
        }
        return status;
    }

    /**
     * @param status
     *                the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the rendered
     */
    public Boolean getRendered() {
        return rendered;
    }

    /**
     * @param rendered the rendered to set
     */
    public void setRendered(Boolean rendered) {
        this.rendered = rendered;
    }

    /**
     * @return the oncomplete
     */
    public String getOncomplete() {
        return oncomplete;
    }

    /**
     * @param oncomplete the oncomplete to set
     */
    public void setOncomplete(String oncomplete) {
        this.oncomplete = oncomplete;
    }

    /**
     * @return the validatorId
     */
    public String getValidatorId() {
        return validatorId;
    }

    /**
     * @param validatorId the validatorId to set
     */
    public void setValidatorId(String validatorId) {
        this.validatorId = validatorId;
    }

}
