/*
 *  The MIT License (MIT)
 *
 *  View the full license at:
 *  https://github.com/hendrixjoseph/FamilyTree/blob/master/LICENSE.md
 *
 *  Copyright (c) 2015 Joseph Hendrix
 *
 *  Hosted on GitHub at https://github.com/hendrixjoseph/FamilyTree
 *
 */

package edu.wright.hendrix11.c3;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import javax.faces.render.Renderer;

import java.io.IOException;

/**
 * @author Joe Hendrix
 */
@FacesRenderer(rendererType = ChartComponent.DEFAULT_RENDERER, componentFamily = ChartComponent.COMPONENT_FAMILY)
public class ChartRenderer extends Renderer
{
    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException
    {
        ChartComponent chart = (ChartComponent) component;
        ChartModel model = chart.getChartModel();

        ResponseWriter writer = context.getResponseWriter();

        writer.startElement("div", null);
        writer.writeAttribute("id", chart.getId(), "id");

        if ( chart.getStyle() != null )
        {
            writer.writeAttribute("style", chart.getStyle(), "style");
        }

        writer.endElement("div");

        writer.startElement("script", null);
        writer.writeAttribute("type", "text/javascript", null);

        writer.write("var chart = c3.generate({");
        writer.write("bindto:'#");
        writer.write(chart.getId());
        writer.write("',");
        writer.write("data:{");
        writer.write("columns:[['mydata1',");

        StringBuilder sb = new StringBuilder(model.getData().values().size() * 3);

        for(Number data : model.getData().values())
        {
            sb.append(data).append(",");
        }

        sb.setLength(sb.length() - 1);

        writer.write(sb.toString());
        writer.write("]]");

//        writer.write("['mydata1',30,200,100,400,150,250],");
//        writer.write("['data2',50,20,10,40,15,25]");
//        writer.write("]");

        if(chart.getType() != null)
        {
            writer.write(",type:'");
            writer.write(chart.getType());
            writer.write("'");
        }

        writer.write("}});");

        writer.endElement("script");
    }
}

@@ -280,16 +280,62 @@
 11.c3;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0A
 import j
@@ -1607,191 +1607,63 @@
     
-writer.write(%22columns:%5B%5B'mydata1',%22);%0A%0A        StringBuilder sb = new StringBuilder(model.getData().values().size() * 3);%0A%0A        for(Number data : model.getData().values())%0A
+%0A        if(model.hasArrayData())%0A        %7B%0A   
         
 %7B%0A  
@@ -1650,33 +1650,33 @@
    %7B%0A           
-%7B
+ 
 %0A            sb.
@@ -1672,40 +1672,22 @@
     
+%7D%0A
     
-sb.append(data).append(%22,%22);
+    else
 %0A   
@@ -1691,27 +1691,30 @@
         
-%7D%0A%0A
+%7B%0A    
         
 sb.setLe
@@ -1709,40 +1709,47 @@
     
-sb.setLength(sb.length() - 1);%0A%0A
+writer.write(%22columns:%5B%5B'data',%22);%0A    
     
@@ -1769,24 +1769,60 @@
 ite(
-sb.to
 String
-());%0A
+Utils.join(model.getData().values(),',');%0A    
     
@@ -1844,16 +1844,26 @@
 e(%22%5D%5D%22);
+%0A        %7D
 %0A%0A//    
@@ -2162,32 +2162,139 @@
 (%22'%22);%0A        %7D
+%0A        %0A        if(chart.getGridX() != null %7C%7C chart.getGridY() != null)%0A        %7B%0A            %0A        %7D
 %0A%0A        writer

Update ChartRenderer.java