/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mars.helios.heliosweb.client.screen.databalance;

import com.mars.helios.heliosweb.shared.domain.DataBalanceDto;
import com.mars.helios.heliosweb.shared.domain.HeliosObject;
import com.smartgwt.client.util.SC;
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.form.DynamicForm;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 *
 * @author helios
 */
public class CubeGridBugs extends VLayout {

    VLayout mainLayout;
    boolean modeEmpty = false;
    int nbLineItem = 5;
    int nbItem = 5;
    CubeGrid cubeGrid;

    public CubeGridBugs() {

        mainLayout = new VLayout();
        mainLayout.setWidth100();
        mainLayout.setHeight100();
        mainLayout.setMembersMargin(8);
        mainLayout.setLayoutMargin(10);
        createAdvancedCube(getData());
        mainLayout.setMembers(cubeGrid);
    }

    /**
     *
     * @param data
     */
    private void createAdvancedCube(BugDto[] data) {


        if (SC.hasAnalytics()) {
            cubeGrid = new CubeGrid();

            cubeGrid.setWidth100();
            cubeGrid.setHeight100();
            cubeGrid.setHideEmptyFacetValues(true);
            cubeGrid.setShowCellContextMenus(false);
            //cubeGrid.setCanEdit(true);
            // cubeGrid.setValueProperty("value");            

            //cubeGrid.setCellIdProperty("cellID");
            // we can edit data
            cubeGrid.setCanEdit(true);
            cubeGrid.setShowEmptyMessage(true);
            cubeGrid.setShowAllColumns(true);
            cubeGrid.setShowAllRecords(true);
            cubeGrid.setHideEmptyFacetValues(false);


            //          cubeGrid.setShow
            //     cubeGrid.setFacets(DataBalanceFacets.getDataBalanceFacets());
        /*
             * final NumberFormat numberFormat = NumberFormat.getFormat("######0.0#######");
             *
             * cubeGrid.setCellFormatter(new CellFormatter() {
             *
             * public String format(Object value, ListGridRecord record, int rowNum, int colNum) { if (value == null) {
             * return null; } try { return numberFormat.format(((Number) value).longValue()); } catch (Exception e) {
             * return value.toString(); } } });
             */

            //     cubeGrid.setContextMenu(getSelectMenu(DataBalanceEditForm.TREATMENT_SCOPE.SELECT));



            String[] columnsFacetId = new String[]{
                "lineItem"
            };


            cubeGrid.setColumnFacets(columnsFacetId);


            String[] rowColumnFacetId = new String[]{
                "item"
            };


            //  cubeGrid.addColumnFacet("period");
            cubeGrid.setRowFacets(rowColumnFacetId);
            SC.logWarn("before set data");
           cubeGrid.setFacets(createFacet());
            cubeGrid.setData(data);
            SC.logWarn("xxx");
            cubeGrid.draw();




        }
    }

    Facet[] createFacet() {
        Facet[] facets = new Facet[2];

        Facet lineItemFacet = new Facet("lineItem", "Line Item");

        FacetValue[] facetLiValues = new FacetValue[nbLineItem];
        for (int i = 0; i < nbLineItem; i++) {
            facetLiValues[i] = new FacetValue("lineItem" + i, "lineItem" + i);

        }

        lineItemFacet.setValues(facetLiValues);

        Facet itemFacet = new Facet("item", "Line Item");

        FacetValue[] facetItemValues = new FacetValue[nbItem];
        for (int i = 0; i < nbItem; i++) {
            facetItemValues[i] = new FacetValue("item" + i, "item" + i);

        }

        itemFacet.setValues(facetItemValues);

        facets[0] = lineItemFacet;
        facets[1] = itemFacet;

        return facets;

    }

    public CubeGrid getCubeGrid() {
        return cubeGrid;
    }

    BugDto[] getData() {
        BugDto[] bugDto = new BugDto[1];
        bugDto[0] = new BugDto("item1", "item1", "lineItem1", "lineItem1", "", "", "", "", "", "", "", "", "0");
        return bugDto;
    }
}
