package com.acme.vcutil.helper;

import java.io.IOException;

import java.util.Locale;

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

import javax.faces.application.Application;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import oracle.adf.share.ADFContext;


public class CustomViewHandler extends ViewHandler
{
    private final ViewHandler base;
    //private final String LANGUAGE_EL_EXPRESSION = "#(localeBean.language)";

    public CustomViewHandler(ViewHandler base)
    {
        this.base = base;
    }

    @Override
    public Locale calculateLocale(FacesContext facesContext)
    {
        Locale locale = ADFContext.getCurrent().getLocale();
        if (locale == null)
            locale = new Locale("tr");

        return locale;
    }

    public String calculateRenderKitId(FacesContext facesContext)
    {
        return base.calculateRenderKitId(facesContext);
    }

    public UIViewRoot createView(FacesContext facesContext, String string)
    {
        return base.createView(facesContext, string);
    }

    public String getActionURL(FacesContext facesContext, String string)
    {
        return base.getActionURL(facesContext, string);
    }

    public String getResourceURL(FacesContext facesContext, String string)
    {
        return base.getResourceURL(facesContext, string);
    }

    public void renderView(FacesContext facesContext, UIViewRoot uiViewRoot) throws IOException
    {
        base.renderView(facesContext, uiViewRoot);
    }

    public UIViewRoot restoreView(FacesContext facesContext, String string)
    {
        return base.restoreView(facesContext, string);
    }

    public void writeState(FacesContext facesContext) throws IOException
    {
        base.writeState(facesContext);
    }

    public static Object evaluateEL(String el)
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        ValueExpression valueExp = elFactory.createValueExpression(elContext, el, Object.class);

        return valueExp.getValue(elContext);
    }
}
