/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

package demo.editing.faces;

import java.beans.Beans;
import java.io.IOException;
import java.io.Writer;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import ilog.views.faces.component.IlvViewHelper;
import ilog.views.faces.dhtml.renderkit.IlvDHTMLDecoder;
import ilog.views.faces.dhtml.renderkit.IlvDHTMLRenderer;
import ilog.views.faces.dhtml.renderkit.IlvDHTMLResponseWriter;
import ilog.views.faces.dhtml.renderkit.IlvDHTMLUtil;
import ilog.views.faces.dhtml.renderkit.IlvDependencyManager;
import ilog.views.faces.dhtml.renderkit.IlvScriptDescriptor;
import ilog.views.faces.dhtml.renderkit.IlvScriptManager;
import ilog.views.util.IlvLocaleUtil;

/**
 * The editable property sheet renderer. This renderer renders an HTML place
 * holder, the import the needed JavaScript file and the declaration of the
 * JavaScript instance that will dynamically generate the property sheet on the
 * client when an object is selected by the selection manager.
 */
public class PropertySheetRenderer extends IlvDHTMLRenderer {

  /**
   * Returns the type of this renderer.
   * 
   * @return The type of this renderer.
   */
  public static String getRendererType() {
    return "ilog.faces.PropertySheet";
  }

  /**
   * Script files that must be included in the response before the rendering
   * JavaScript code can be generated by this renderer.
   */
  public final static IlvScriptDescriptor scriptDescriptor = new IlvScriptDescriptor("ViewHelper",
      IlvDHTMLRenderer.scriptDescriptor) {
    Override
    public void addScriptsToLoad(IlvScriptManager scriptManager, Writer writer) {
      scriptManager.addLocaleScript(IlvLocaleUtil.getCurrentLocale(), "/data/GanttPropertySheet-messages.js");
      scriptManager.addScript("/data/GanttPropertySheet.js");
    }
  };

  /**
   * Script files that must be included in the response before the rendering
   * JavaScript code can be generated by this renderer.
   */
  Override
  public IlvScriptDescriptor getScriptDescriptor() {
    return scriptDescriptor;
  }

  /**
   * Implementation of the "Apply Values from Request" phase of the JSF life
   * cycle. Decodes from the request the update data for this component, and
   * updates the component according to this data.
   */
  Override
  public void decode(FacesContext context, UIComponent component) {
    super.decode(context, component);

    IlvDHTMLDecoder decoder = new IlvDHTMLDecoder(getParameterMap(context, component), component);
    decoder.decodeDependencyProperty(VIEW_ID);

  }

  /**
   * {@inheritDoc}
   */
  Override
  protected void resolveDependencies(UIComponent component, IlvDependencyManager dependencyManager) {

    IlvViewHelper vh = (IlvViewHelper) component;
    resolveDependency(component, VIEW, vh.getViewId(), dependencyManager);
    resolveVBDependency(component, VIEW, VIEW_ID, dependencyManager);

  }

  /**
   * {@inheritDoc}
   */
  Override
  protected void emitJSSetProperties(FacesContext ctx, UIComponent component) throws IOException {

    super.emitJSSetProperties(ctx, component);
    IlvDHTMLResponseWriter writer = new IlvDHTMLResponseWriter(ctx, component);
    writer.writeJSDependencyProperty(VIEW);
    writer.writeJSStringProperty("component");

  }

  /**
   * {@inheritDoc}
   */
  Override
  protected void emitJSCreation(FacesContext facesContext, UIComponent component) throws IOException {
    IlvDHTMLResponseWriter writer = new IlvDHTMLResponseWriter(facesContext, component);
    String id = IlvDHTMLUtil.getJSRef(component);
    String tableId = id + "_table";
    String[] params = { tableId };
    writer.writeJSProxyCreation("PropertySheet", params);
  }

  /**
   * {@inheritDoc}
   */
  Override
  public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    super.encodeEnd(context, component);
    IlvDHTMLResponseWriter writer = new IlvDHTMLResponseWriter(context, component);
    super.encodeEnd(context, component);

    if (!Beans.isDesignTime()) {
      emitScriptsLoad(writer.getSupport());
      String id = IlvDHTMLUtil.getJSRef(component);
      String tableId = id + "_table";
      ResponseWriter out = context.getResponseWriter();
      out.startElement("span", component);
      out.writeAttribute("id", tableId, null);
      out.endElement("span");

      writer.startScriptSection();
      emitJSCreation(context, component);
      emitJSSetProperties(context, component);
      emitOnLoadHandler(context, component);
      emitResolvedDependencies(writer.getSupport(), component);
      writer.endScriptSection();
    }
  }
}