package com.mars.helios.heliosweb.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.smartgwt.client.types.Alignment;
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.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

/**
 * Entry point classes define
 * <code>onModuleLoad()</code>.
 */
public class HeliosWeb implements EntryPoint {

    private final HeliosGinjector injector = GWT.create(HeliosGinjector.class);
    private VLayout layout =   new VLayout();

    /**
     * This is the entry point method.
     */
    @Override
    public void onModuleLoad() {
        GWT.setUncaughtExceptionHandler(new HeliosUncaughtExceptionHandler());
        // see http://code.google.com/p/google-web-toolkit/issues/detail?id=1617
        Timer timer = new Timer() {
            @Override
            public void run() {
                actualLoading();
            }
        };

        timer.schedule(1);
    }


    private Facet[] buildFacets() {
        //mixedPack dimensions
        Facet mixedPack = new Facet("fromNodeId", "Mixed Packs");
        mixedPack.setValues(buildDummyFacet());
        mixedPack.setWidth(200);

        //component dimension
        Facet compoment = new Facet("toNodeId", "Components");
        compoment.setIsTree(true);
        compoment.setCollapsed(false);
        compoment.setCanCollapse(false);
        compoment.setCanMinimize(false);
        compoment.setAlign(Alignment.LEFT);
        compoment.setWidth(200);
        compoment.setValues(buildDummyFacet());

        //details dimension
        Facet details = new Facet("data", "Details");
        details.setValues(buildDummyFacet());

        //my 3dimensions facets
        Facet[] facets = {
                mixedPack,
                compoment,
                details
        };
        return facets;
    }

    private FacetValue[] buildDummyFacet() {
        FacetValue[] mixedPackValues = new FacetValue[3];
        for (int j = 0; j < mixedPackValues.length; j++) {
            mixedPackValues[j] = new FacetValue("dummyId" + j, "dummy desc");
        }
        return mixedPackValues;
    }

    public void actualLoading() {
        SC.logInfo("Starting sandbox setup");


        final CubeGrid cubeGrid = new CubeGrid();
        cubeGrid.setDataSource(new MyDS());

        cubeGrid.setWidth100();
        cubeGrid.setHeight100();
        cubeGrid.setHideEmptyFacetValues(false);
        cubeGrid.setShowCellContextMenus(true);


        cubeGrid.setWidth100();
        cubeGrid.setHeight100();
        cubeGrid.setHideEmptyFacetValues(false);
        cubeGrid.setShowEmptyMessage(true);
        cubeGrid.setShowCellContextMenus(false);
        cubeGrid.setCanEdit(true);
        cubeGrid.setAutoFetchData(true);
        cubeGrid.setAutoSaveEdits(false);
        cubeGrid.setFacets(buildFacets());
        cubeGrid.setValueProperty("_value");
        cubeGrid.setCellIdProperty("cellId");
        cubeGrid.setColumnFacets("fromNodeId","data");
        cubeGrid.setRowFacets("toNodeId");


        cubeGrid.setAutoFetchData(true);
        cubeGrid.fetchData();

        cubeGrid.setCanEdit(true);
        cubeGrid.setAutoSaveEdits(false);

        /*
         * Setup toolbar
         */
        ToolStripButton redrawBtn = new ToolStripButton("Redraw !");
        redrawBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                layout.redraw();
            }
        });

        ToolStrip toolStrip = new ToolStrip();
        toolStrip.addButton(redrawBtn);


        layout.setWidth100();
        layout.setHeight100();
        layout.addMember(toolStrip);
        layout.addMember(cubeGrid);

        layout.draw();
        SC.logInfo("Setup done !");
    }
}
