package testclient.client;

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

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.Overflow;
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.layout.VLayout;

public class TestCubeGridUI 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 buttonTimeX = new IButton("Grid with Time on X");
        buttonTimeX.setWidth(250);
        buttonTimeX.addClickHandler(event -> {
			if (cubegridTrue != null) {
				layoutTrue.removeMember(cubegridTrue);
			}
			cubegridTrue = createCube(true);
			layoutTrue.addMember(cubegridTrue);
		});
    	layoutTrue.addMember(buttonTimeX);

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

        IButton buttonTimeY = new IButton("Grid with Time on Y");
        buttonTimeY.setWidth(250);
        buttonTimeY.addClickHandler(event -> {
			if (cubegridFalse != null) {
				layoutFalse.removeMember(cubegridFalse);
			}
			cubegridFalse = createCube(false);
			layoutFalse.addMember(cubegridFalse);
		});
        layoutFalse.addMember(buttonTimeY);
        
        layout.addMember(layoutTrue);
        layout.addMember(layoutFalse);
        
    	layout.draw();
    }
    
    private CubeGrid createCube(boolean isTimeOnX) {
    	CubeGrid cube = new CubeGrid();

    	cube.setHideEmptyFacetValues(false);
    	cube.setWidth100();
    	cube.setHeight100();
        cube.setAutoFetchData(true);

        cube.setEnableCharting(true);
        cube.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);

        cube.setAutoSizeHeaders(true);
        cube.setAutoSizeHeaderSpans(true);
        cube.setAutoFitFieldWidths(true);

        cube.setOverflow(Overflow.AUTO);
        cube.setShowAllRecords(false);
        cube.setShowCellContextMenus(true);
        cube.setRowHeaderGridMode(true);

    	cube.setValueTitle("TestCube");
    	
    	List<Facet>facetlist = new ArrayList<>();
    	facetlist.add(createFacet("Time", TIME_2012_VALUE, true));
        facetlist.add(createFacet("Ldc", LDC_VALUE, true));
		facetlist.add(createFacet("Measure", MEASURE_VALUE, false));
        for (Facet facet : facetlist) {
            facet.setCollapsed(true);
        }
    	cube.setFacets(facetlist.toArray(new Facet[facetlist.size()]));

    	if (isTimeOnX) {
            cube.setColumnFacets("Time");
			cube.setRowFacets("Ldc", "Measure");
        } else {
            cube.setRowFacets("Time");
			cube.setColumnFacets("Ldc", "Measure");
        }
        cube.setValueProperty("Value");

		cube.setData(CubeValueData.getData());
		return cube;
    }
    
    private final String[] TIME_2012_VALUE = new String[] {"Jan", "Feb"}; 
    private final String[] LDC_VALUE = new String[] {"CP", "IP"};
	private final String[] MEASURE_VALUE = new String[] {"A", "B"};

    private Facet createFacet(String facetname, String[] facetvalue, boolean hasAll) {
		Facet facet = new Facet(facetname, facetname);
		facet.setIsTree(true);
		List<FacetValue> facetvaluelist = new ArrayList<>();

		String parentfacet = null;
		FacetValue facetval = null;
		if (hasAll) {
			parentfacet = "All " + facetname + "s";
			facetval = new FacetValue(parentfacet, parentfacet);
			facetvaluelist.add(facetval);
		}
		for (String fval : facetvalue) {
			if (parentfacet == null) {
				facetval = new FacetValue(fval, fval);
			} else {
				facetval = new FacetValue(fval, fval, parentfacet);
			}
			facetvaluelist.add(facetval);
		}

        facet.setValues(facetvaluelist.toArray(new FacetValue[facetvaluelist.size()]));
        
        return facet;
    }
}
