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

import com.google.inject.Singleton;
import com.mars.helios.heliosweb.client.widgets.cube.HeliosCubeGrid;
import com.smartgwt.client.data.Hilite;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.cube.CellRecord;
import com.smartgwt.client.widgets.cube.CubeGrid;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellSelectionChangedEvent;
import com.smartgwt.client.widgets.grid.events.CellSelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.EditorExitEvent;
import com.smartgwt.client.widgets.grid.events.EditorExitHandler;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

/**
 *
 * @author Philippe SCHEFFER <philippe.scheffer@cgi.com>
 */
@Singleton
public class CubeGridDisplay {

    private static Hilite[] hilites = new Hilite[]{
        new Hilite() {
            {
                setTextColor("#FF0000");
                setCssText("color:#FF0000");
                setId("myHilite");
            }
        }};
    private CubeGrid cubeGrid;
    Menu menu = new Menu();

    public CubeGridDisplay() {

        cubeGrid = new CubeGrid();
        //in order to enable charting, the Drawing module must be present  
        if (SC.hasDrawing()) {
            cubeGrid.setEnableCharting(true);
        }
        cubeGrid.setData(ProductRevenueData.getData());
        cubeGrid.setHilites(hilites);
        cubeGrid.setWidth100();
        cubeGrid.setHeight100();
        cubeGrid.setHideEmptyFacetValues(true);


        //ubeGrid.setShowCellContextMenus(true);

        cubeGrid.setCanEdit(Boolean.TRUE);
        cubeGrid.addEditorExitHandler(new EditorExitHandler() {
            @Override
            public void onEditorExit(EditorExitEvent event) {
                SC.logWarn("out of  " + event.getNewValue());
                ListGrid listGrid = event.getGrid();
                SC.logWarn("listGrid != null " + (listGrid != null));
                Record selectRecord = event.getRecord();
                SC.logWarn("selectRecord != null " + (selectRecord != null));
                ListGridRecord listGridSelect = cubeGrid.getCellRecord(event.getRowNum(),
                        event.getColNum());
                SC.logWarn("listGridSelect != null " + (listGridSelect != null));
                cubeGrid.hiliteCell(event.getRowNum(), event.getColNum(), "myHilite");
                //  cubeGrid.refreshCell(event.getRowNum(), event.getColNum());
            }
        });
        cubeGrid.setColumnFacets("quarter", "month", "metric");
        cubeGrid.setRowFacets("region", "product");
        final MenuItem dataFill = new MenuItem("show months");
        dataFill.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(MenuItemClickEvent event) {
                CellRecord[] records = cubeGrid.getSelectedCells();
                String months = "";
                for (CellRecord cell : records) {
                    months += cell.getAttribute("month");
                }
                SC.say("selected months : " + months);
            }
        });
        cubeGrid.hiliteFacetValue("month", "February", "myHilite");
        menu.setItems(dataFill);
        cubeGrid.setContextMenu(menu);
    }

    public Canvas buildForm() {
        return cubeGrid;
    }
}
