package com.powerrunner.ui.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.cube.CubeGrid;
import com.smartgwt.client.widgets.cube.Facet;
import com.smartgwt.client.widgets.cube.FacetValue;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class PowerRunnerUI implements EntryPoint {
	private CubeGrid cubegridTrue;
	private CubeGrid cubegridFalse;
	
    @Override  
    public void onModuleLoad() {
    	final VLayout layout = new VLayout();
    	layout.setWidth100();
    	layout.setHeight100();
    	
    	final VLayout layoutTrue = new VLayout();
    	layoutTrue.setWidth100();
    	layoutTrue.setHeight100();
    	
    	IButton buttonHideEmptyTrue = new IButton("Grid with HideEmptyFacetValues = true");
    	buttonHideEmptyTrue.setWidth(250);
    	buttonHideEmptyTrue.addClickHandler(new ClickHandler() {	
			@Override
			public void onClick(ClickEvent event) {
				if (cubegridTrue != null) {
					layoutTrue.removeMember(cubegridTrue);
				}
				cubegridTrue = createCube(true);
				layoutTrue.addMember(cubegridTrue);				
			}
		});
    	layoutTrue.addMember(buttonHideEmptyTrue);

    	final VLayout layoutFalse = new VLayout();
    	layoutFalse.setWidth100();
    	layoutFalse.setHeight100();

        IButton buttonHideEmptyFalse = new IButton("Grid with HideEmptyFacetValues = false");
        buttonHideEmptyFalse.setWidth(250);
        buttonHideEmptyFalse.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (cubegridFalse != null) {
					layoutFalse.removeMember(cubegridFalse);
				}
				cubegridFalse = createCube(false);
				layoutFalse.addMember(cubegridFalse);
			}
		});
        layoutFalse.addMember(buttonHideEmptyFalse);
        
        layout.addMember(layoutTrue);
        layout.addMember(layoutFalse);
        
    	layout.draw();
    }
    
    private CubeGrid createCube(boolean isHideEmptyFacetValues) {
    	CubeGrid cube = new CubeGrid();

    	cube.setHideEmptyFacetValues(isHideEmptyFacetValues);
    	cube.setWidth100();
    	cube.setHeight100();
		 
    	cube.setValueTitle("TestCube");
    	
    	List<Facet>facetlist = new ArrayList<Facet>();
    	facetlist.add(createFacet("Time", TIME_2012_VALUE));
    	facetlist.add(createFacet("Delivery Point", DP_VALUE));
    	cube.setFacets(facetlist.toArray(new Facet[facetlist.size()]));

    	cube.setColumnFacets("Time");
    	cube.setRowFacets("Delivery Point");
    	cube.setValueProperty("Value");

		cube.setData(CubeValueData.getData());
		return cube;
    }
    
    private final String[] TIME_2012_VALUE = new String[] {"Jan", "Feb"}; 
    private final String[] DP_VALUE = new String[] {"A", "B", "C"}; 
       
    private Facet createFacet(String facetname, String[] facetvalue) {
		Facet facet = new Facet(facetname, facetname);
		facet.setIsTree(true);
		List<FacetValue> facetvaluelist = new ArrayList<FacetValue>();
		
		String parentfacet = "All " + facetname + "s";
		FacetValue facetval = new FacetValue(parentfacet, parentfacet);
		facetvaluelist.add(facetval);
		for (String fval : facetvalue) {
			facetval = new FacetValue(fval, fval, parentfacet);
			facetvaluelist.add(facetval);
		}
        facet.setValues(facetvaluelist.toArray(new FacetValue[facetvaluelist.size()]));
        
        return facet;
    }
}
