package com.npu.hinc.client.common.charts;

import java.util.LinkedList;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.ChartType;
import com.smartgwt.client.widgets.chart.FacetChart;
import com.smartgwt.client.widgets.chart.MetricSettings;
import com.smartgwt.client.widgets.cube.Facet;
import com.smartgwt.client.widgets.cube.FacetValue;

public class ChartTest extends FacetChart{

	
	protected Integer numExtraAxis = 0;
	
	public ChartTest (){
		createChart();
	}
	
	public void createChart(){
		Record[] records = getRecords(); //change here to check another set of values, eg. getRecord(); getRecords2(); or getRecords3();
		
		numExtraAxis = records.length -1;
	
		this.setWidth100();  
		this.setHeight100();  
		this.setChartType(ChartType.COLUMN);  
		this.setStacked(true);  
		this.setBorder("1px solid black");  
		this.setShowTitle(false);  
		this.setShowChartRect(true);  
		this.setShowTitle(false);
		this.setData(records);
			
		this.setStacked(records[0].getAttributeAsBoolean("stacked")); 
		Facet metricFacet = new Facet("metric", "metric");  
		metricFacet.setInlinedValues(true); 
		metricFacet.setId("metric");
		
		FacetValue[] facets = getFacetValues(records);
		metricFacet.setValues(facets);
		metricFacet.setShowParentsLast(false); 
		
		MetricSettings [] axisSettings = new MetricSettings[numExtraAxis];
		final String [] axisMetrics = new String[numExtraAxis];
		final LinkedList<MetricSettings> metricSettings = new LinkedList<MetricSettings>();
		for (int i = 1; i <= numExtraAxis; i++) {
			MetricSettings metricSetting = new MetricSettings(); 
			metricSetting.setChartType(ChartType.COLUMN);
			metricSetting.setValueTitle("("+i+")");
			metricSetting.setStacked(false); 
			metricSetting.setShowDataPoints(true);
			metricSetting.setShowAxis(true); //if i change to false will draw ok for my getRecords() set of data but not for a set with more data
			metricSetting.setShowValueOnHover(true);
			metricSetting.setShowValueAxisLabel(true);
			
			String gradation = records[i].getAttributeAsString("gradation_string");
			metricSetting.setMatchGradations(gradation);  
		
			axisSettings[i-1] = metricSetting;
			axisMetrics[i-1] = "value" + (i);
			metricSettings.add(metricSetting);
		}
		
		this.setExtraAxisSettings(axisSettings);  
		this.setExtraAxisMetrics(axisMetrics);
		
		Facet period = new Facet("periodString",  "Period");
		Facet title = new Facet("title");
		this.setFacets(period, title, metricFacet);  
		
		this.setTooltip(null);
		this.setShowLegend(true);
		this.setBarMargin(7);
		this.markForRedraw();
		this.show();
	}
	

	public Record[] getRecords(){ 
		
		int i = 8; //min = 1 ; max = 8 //change the number of variables
		
		int j = 1;
		
		Record[] recss = new Record[i];
		
		
		Record rec0 = new Record();
		rec0.setAttribute("periodString", "dez / 2014");
		rec0.setAttribute("value0", -563557d);
		rec0.setAttribute("color", "#0000ff");
		rec0.setAttribute("title", "I1");
		rec0.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec0;
		
		Record rec1 = new Record();
		rec1.setAttribute("periodString", "dez / 2014");
		rec1.setAttribute("value1", 5583d);
		rec1.setAttribute("color", "#FFFF00");
		rec1.setAttribute("title", "I2");
		rec1.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec1;
		
		Record rec2 = new Record();
		rec2.setAttribute("periodString", "dez / 2014");
		rec2.setAttribute("value2", -252585d);
		rec2.setAttribute("color", "#755B00");
		rec2.setAttribute("title", "I3");
		rec2.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec2;
		
		Record rec3 = new Record();
		rec3.setAttribute("periodString", "dez / 2014");
		rec3.setAttribute("value3", -315602d);
		rec3.setAttribute("color", "#FF00FF");
		rec3.setAttribute("title", "I4");
		rec3.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec3;
		
		Record rec4 = new Record();
		rec4.setAttribute("periodString", "dez / 2014");
		rec4.setAttribute("value4", -126d);
		rec4.setAttribute("color", "#0000ff");
		rec4.setAttribute("title", "I5");
		rec4.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec4;
		
		Record rec5 = new Record();
		rec5.setAttribute("periodString", "dez / 2014");
		rec5.setAttribute("value5", -827d);
		rec5.setAttribute("color", "#339966");
		rec5.setAttribute("title", "I6");
		rec5.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec5;
		
		Record rec6 = new Record();
		rec6.setAttribute("periodString", "dez / 2014");
		rec6.setAttribute("value6", 0d);
		rec6.setAttribute("color", "#000000");
		rec6.setAttribute("title", "I7");
		rec6.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec6;
		
		Record rec7 = new Record();
		rec7.setAttribute("periodString", "dez / 2014");
		rec7.setAttribute("value7", 0d);
		rec7.setAttribute("color", "#00CCFF");
		rec7.setAttribute("title", "I8");
		rec7.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec7;
		
		
		
		return recss;
		
	}
	
public Record[] getRecords2(){
		
		int i = 8; //min = 1 ; max = 9
		
		int j = 1;
		
		Record[] recss = new Record[i];
		
		
		Record rec0 = new Record();
		rec0.setAttribute("periodString", "dez / 2014");
		rec0.setAttribute("value0", 9d);
		rec0.setAttribute("title", "I1");
		rec0.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec0;
		
		Record rec1 = new Record();
		rec1.setAttribute("periodString", "dez / 2014");
		rec1.setAttribute("value1", 2d);
		rec1.setAttribute("title", "I2");
		rec1.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec1;
		
		Record rec2 = new Record();
		rec2.setAttribute("periodString", "dez / 2014");
		rec2.setAttribute("value2", 3d);
		rec2.setAttribute("title", "I3");
		rec2.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec2;
		
		Record rec3 = new Record();
		rec3.setAttribute("periodString", "dez / 2014");
		rec3.setAttribute("value3", 4d);
		rec3.setAttribute("title", "I4");
		rec3.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec3;
		
		Record rec4 = new Record();
		rec4.setAttribute("periodString", "dez / 2014");
		rec4.setAttribute("value4", 5d);
		rec4.setAttribute("title", "I5");
		rec4.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec4;
		
		Record rec5 = new Record();
		rec5.setAttribute("periodString", "dez / 2014");
		rec5.setAttribute("value5", 6d);
		rec5.setAttribute("title", "I6");
		rec5.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec5;
		
		Record rec6 = new Record();
		rec6.setAttribute("periodString", "dez / 2014");
		rec6.setAttribute("value6", 7d);
		rec6.setAttribute("title", "I7");
		rec6.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec6;
		
		Record rec7 = new Record();
		rec7.setAttribute("periodString", "dez / 2014");
		rec7.setAttribute("value7", 8d);
		rec7.setAttribute("title", "I8");
		rec7.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec7;
		
		Record rec8 = new Record();
		rec8.setAttribute("periodString", "dez / 2014");
		rec8.setAttribute("value8", 9d);
		rec8.setAttribute("title", "I9");
		rec8.setAttribute("gradation_string", "value0");
		recss[i-j++] = rec8;
		
		return recss;
		
	}
	
public Record[] getRecords3(){
		
		
		Record[] recss = new Record[8];
		
		
		Record rec0 = new Record();
		rec0.setAttribute("periodString", "dez / 2014");
		rec0.setAttribute("value0", -563557d);
		rec0.setAttribute("title", "I1");
		rec0.setAttribute("gradation_string", "value0");
		recss[7] = rec0;
		
		Record rec1 = new Record();
		rec1.setAttribute("periodString", "dez / 2014");
		rec1.setAttribute("value1", 5583d);
		rec1.setAttribute("title", "I2");
		rec1.setAttribute("gradation_string", "value0");
		recss[6] = rec1;
		
		Record rec2 = new Record();
		rec2.setAttribute("periodString", "dez / 2014");
		rec2.setAttribute("value2", -252585d);
		rec2.setAttribute("title", "I3");
		rec2.setAttribute("gradation_string", "value0");
		recss[5] = rec2;
		
		Record rec3 = new Record();
		rec3.setAttribute("periodString", "dez / 2014");
		rec3.setAttribute("value3", -315602d);
		rec3.setAttribute("title", "I4");
		rec3.setAttribute("gradation_string", "value0");
		recss[4] = rec3;
		
		Record rec4 = new Record();
		rec4.setAttribute("periodString", "nov / 2014");
		rec4.setAttribute("value0", -126d);
		rec4.setAttribute("title", "I1");
		rec4.setAttribute("gradation_string", "value0");
		recss[3] = rec4;
		
		Record rec5 = new Record();
		rec5.setAttribute("periodString", "nov / 2014");
		rec5.setAttribute("value1", -827d);
		rec5.setAttribute("title", "I2");
		rec5.setAttribute("gradation_string", "value0");
		recss[2] = rec5;
		
		Record rec6 = new Record();
		rec6.setAttribute("periodString", "nov / 2014");
		rec6.setAttribute("value2", 7d);
		rec6.setAttribute("title", "I3");
		rec6.setAttribute("gradation_string", "value0");
		recss[1] = rec6;
		
		Record rec7 = new Record();
		rec7.setAttribute("periodString", "nov / 2014");
		rec7.setAttribute("value3", 8d);
		rec7.setAttribute("title", "I4");
		rec7.setAttribute("gradation_string", "value0");
		recss[0] = rec7;
		
		return recss;
		
	}

	private FacetValue[] getFacetValues(Record[] recs){
		FacetValue[] facetsvalue = new FacetValue[numExtraAxis+1];
		for(int i =0; i <= numExtraAxis; i++){
			FacetValue facetVal = new FacetValue("value" + i, " ("+ i +")");
			facetsvalue[i] = facetVal;
		}
		return facetsvalue;
	}
	
}
