package com.acme.util.ui.bean;


import com.acme.structure.exception.main.UnknownExpressionException;
import com.acme.util.ui.UIHelper;
import com.acme.util.ui.WindowManager;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


public class ToolbarImplementationBean extends ToolbarMBean
{
    ToolbarMBean templateBean = null;

    public ToolbarImplementationBean()
    {
        super();
    }

    public void loadButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.loadButtonPressed(actionEvent);
    }

    public void searchButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.searchButtonPressed(actionEvent);
    }

    public void clearButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.clearButtonPressed(actionEvent);
    }

    public void addButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.addButtonPressed(actionEvent);
    }

    public void removeButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.removeButtonPressed(actionEvent);
    }

    public void saveButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.saveButtonPressed(actionEvent);
    }

    public void previousButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.previousButtonPressed(actionEvent);
    }

    public void nextButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.nextButtonPressed(actionEvent);
    }

    public void copyButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.copyButtonPressed(actionEvent);
    }

    public void pasteButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.pasteButtonPressed(actionEvent);
    }

    public void rollbackButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.rollbackButtonPressed(actionEvent);
    }

    public void refreshButtonPressed(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        bean.refreshButtonPressed(actionEvent);
    }

    public void recordHistoryPressed(ActionEvent actionEvent)
    {
        //        try
        //        {
        //            RecordHistoryWrapper rhw = RecordHistoryWrapper.getInstance((BaseEntity)getRecordHistoryEntity());
        //            WRecordHistory recordHistory = (WRecordHistory)WindowManager.getInstance().openWindowByKey("com.celebi.ikarus.main.component.window.WRecordHistory");
        //            recordHistory.initializeWithObject(rhw);
        //        }
        //        catch (Exception exc)
        //        {
        //         UIHelper.showInformationMessages(WindowManager.getInstance().getHandler().handleException(new FeatureNotSupportedException()));
        //        }
    }

    public Object getRecordHistoryEntity(ActionEvent actionEvent)
    {
        ToolbarMBean bean = getBean();
        return bean.getRecordHistoryEntity(actionEvent);
    }

    //Access the template attribute to resolve the application
    //managed bean reference

    private ToolbarMBean getBean()
    {
        if (templateBean == null)
        {
            FacesContext fctx = FacesContext.getCurrentInstance();
            ELContext elctx = fctx.getELContext();
            ExpressionFactory exprFactory = fctx.getApplication().getExpressionFactory();
            ValueExpression ve = exprFactory.createValueExpression(elctx, "#{attrs.toolbarCommandsBean}", Object.class);
            Object valueObject = ve.getValue(elctx);
            if (valueObject != null)
            {
                templateBean = (ToolbarMBean)valueObject;
            } else
            {
                UIHelper.showInformationMessages(WindowManager.getInstance().getHandler().handleException(new UnknownExpressionException("#{attrs.toolbarCommandsBean}")));
            }
        }
        return templateBean;
    }
}
