/*
 * 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 thinstock.faces.renderer;

import java.io.IOException;
import java.io.Writer;

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

import ilog.views.chart.faces.dhtml.component.IlvChartDHTMLView;
import ilog.views.chart.faces.dhtml.renderkit.IlvChartRenderer;
import ilog.views.faces.dhtml.renderkit.ComponentCreationListener;
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.IlvFacesManager;
import ilog.views.faces.dhtml.renderkit.IlvScriptDescriptor;
import ilog.views.faces.dhtml.renderkit.IlvScriptManager;

/**
 *  
 */
public class MinorChartViewRenderer extends IlvChartRenderer {
  public static IlvScriptDescriptor scriptDescriptor = new IlvScriptDescriptor("minorChart",
      IlvChartRenderer.scriptDescriptor) {

    Override
    public void addScriptsToLoad(IlvScriptManager scriptManager, Writer writer) {
      scriptManager.addScript("/data/MinorChartViewProxy.js");
    }
  };

  Override
  public IlvScriptDescriptor getScriptDescriptor() {
    return scriptDescriptor;
  }

  public static String getRendererType() {
    return MinorChartViewRenderer.class.getName();
  }

  Override
  protected void resolveDependencies(UIComponent component, IlvDependencyManager depManager) {
    super.resolveDependencies(component, depManager);
    String view = (String) component.getAttributes().get(VIEW_ID);
    new ComponentCreationListener(component, view, VIEW) {

      Override
      public void onComponentCreated(Writer writer, UIComponent depComponent) throws IOException {
        String depJSRef = IlvDHTMLUtil.getJSRef(depComponent);
        String jsRef = IlvDHTMLUtil.getJSRef(getComponent());
        writer.write(depJSRef + ".chartUpdater.addTarget(" + jsRef + ");");
      }
    };
    resolveDependency(component, VIEW, view, depManager);
  }

  Override
  protected void emitJSCreation(FacesContext facesContext, UIComponent component) throws IOException {

    IlvDHTMLResponseWriter writer = new IlvDHTMLResponseWriter(facesContext, component);
    IlvChartDHTMLView cv = (IlvChartDHTMLView) component;
    String[] params = { getReferenceNodeRef(facesContext, component), component.getAttributes().get(WIDTH).toString(),
        component.getAttributes().get(HEIGHT).toString(), (cv.getClientState() == null ? "" : cv.getClientState()) };

    writer.writeJSProxyCreation("MinorChartViewProxy", params);
  }

  Override
  protected void emitJSSetProperties(FacesContext ctx, UIComponent component) throws IOException {
    super.emitJSSetProperties(ctx, component);
    UIComponent view = (UIComponent) component.getAttributes().get(VIEW);
    String jsRef = IlvDHTMLUtil.getJSRef(component);
    IlvFacesManager mgr = IlvDHTMLUtil.findManager(ctx);
    IlvDependencyManager dMgr = mgr.getDependencyManager();
    if (view != null && IlvDHTMLUtil.isDependencyRendered(component, dMgr)) {
      String depJSRef = IlvDHTMLUtil.getJSRef(view);
      ctx.getResponseWriter().write(depJSRef + ".chartUpdater.addTarget(" + jsRef + ");\n");
    }
  }

  Override
  protected void emitOnLoadHandler(FacesContext ctx, UIComponent component) throws IOException {
    // Avoid the init of the minor chart views.

    IlvDHTMLResponseWriter writer = new IlvDHTMLResponseWriter(ctx, component);
    writer.writeJSBodyEventHandler("load", "IlvSizedViewProxy.onload");
  }

}