package com.mars.helios.heliosweb.client;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.inject.client.Ginjector;
import com.google.gwt.user.client.Timer;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.Hilite;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.cube.CellRecord;
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.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;
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;

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

    private final Ginjector injector = GWT.create(Ginjector.class);

    public class ProductRevenue extends ListGridRecord {

        public ProductRevenue(String quarter, String month, String region, String product,
                              String metric, int value, int percentNational) {
            setAttribute("quarter", quarter);
            setAttribute("month", month);
            setAttribute("region", region);
            setAttribute("product", product);
            setAttribute("metric", metric);
            setAttribute("_value", value);
            setAttribute("percentNational", percentNational);
        }
    }


    private static final Hilite[] HILITES = new Hilite[]{
            new Hilite() {
                {
                    // You can set a criteria to apply the style conditionally.
                    //
                    // It doesn't work with explicit hilite :
                    // cubeGrid.hiliteFacetValue("month", "February", "myHilite");
                    // the hilite ignore the condition
                    setCriteria(new AdvancedCriteria("_value", OperatorId.LESS_OR_EQUAL, 5000));

                    setTextColor("#FF0000");
                    setCssText("color:#FF0000");
                    setId("myHilite");
                }
            },
            new Hilite() {
                {
                    // You can set a criteria to apply the style conditionally.
                    //
                    // It doesn't work with explicit hilite :
                    // cubeGrid.hiliteFacetValue("month", "February", "myHilite");
                    // the hilite ignore the condition
                    setCriteria(new AdvancedCriteria("_value", OperatorId.GREATER_THAN, 100000));

                    setTextColor("#0000FF");
                    setCssText("color:#0000FF");
                    setId("myHilite2");
                }
            }
    };

    /**
     * This is the entry point method.
     */
    @Override
    public void onModuleLoad() {
        // 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);
    }

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

        /*
         * Cube setup
         */

        String[] firstNames = {"Alfred", "Bob", "Charles", "David", "Emilien",
                "Fabrice", "Gerard", "Henri", "Ignace", "Julien", "Kevin", "Lucien",
                "Maurice", "Nicolas", "Olivier", "Patrick", "Quentin", "Robert", "Stéphane",
                "Thomas", "Ulysse", "Vincent", "William", "Xavier", "Yves", "Zoe"};
        String[] lastNames = {"Arbogast", "Becht", "Cuvier", "Durand", "Einstein",
                "Frot", "Goncalves", "Herbillaut", "Ibrahimovitch", "Jugnot", "Klein",
                "Lienhardt", "Michu", "Nolam", "Obama", "Pastewski", "Quirin", "Ribero",
                "Stendhal", "Tallerand", "Urban", "Vinci", "Wagner", "Xenakis", "Yserand",
                "Zand"};


        Facet facet1 = new Facet("FirstName");
        createFacetValues(facet1, firstNames);

        Facet facet2 = new Facet("LastName");
        createFacetValues(facet2, lastNames);
        Facet facet3 = new Facet("Period");
        createPeriodValues(facet3, 300);


        Menu menu = new Menu();
        final CubeGrid cubeGrid = new CubeGrid();
        //in order to enable charting, the Drawing module must be present
        if (SC.hasDrawing()) {
            cubeGrid.setEnableCharting(true);
        }
        ProductRevenue[] data = new ProductRevenue[]{
                new ProductRevenue("Q1, 2002", "January", "Western U.S.", "Pens", "Revenue", 10000, 25),
                new ProductRevenue("Q1, 2002", "January", "Western U.S.", "Chairs", "Revenue", 50000, 45),
                new ProductRevenue("Q1, 2002", "January", "Western U.S.", "Monitors", "Revenue", 120000, 49),
                new ProductRevenue("Q1, 2002", "January", "Western U.S.", "Pens", "Profit", 2000, 25),
                new ProductRevenue("Q1, 2002", "January", "Western U.S.", "Chairs", "Profit", 5000, 45),
                new ProductRevenue("Q1, 2002", "January", "Western U.S.", "Monitors", "Profit", 44000, 59),
                new ProductRevenue("Q1, 2002", "January", "Midwest U.S.", "Pens", "Revenue", 8000, 20),
                new ProductRevenue("Q1, 2002", "January", "Midwest U.S.", "Chairs", "Revenue", 22000, 20),
                new ProductRevenue("Q1, 2002", "January", "Midwest U.S.", "Monitors", "Revenue", 20000, 8),
                new ProductRevenue("Q1, 2002", "January", "Midwest U.S.", "Pens", "Profit", 2000, 25),
                new ProductRevenue("Q1, 2002", "January", "Midwest U.S.", "Chairs", "Profit", 2000, 18),
                new ProductRevenue("Q1, 2002", "January", "Midwest U.S.", "Monitors", "Profit", 5000, 7),
                new ProductRevenue("Q1, 2002", "January", "Eastern U.S.", "Pens", "Revenue", 22000, 55),
                new ProductRevenue("Q1, 2002", "January", "Eastern U.S.", "Chairs", "Revenue", 40000, 36),
                new ProductRevenue("Q1, 2002", "January", "Eastern U.S.", "Monitors", "Revenue", 105000, 43),
                new ProductRevenue("Q1, 2002", "January", "Eastern U.S.", "Pens", "Profit", 4000, 50),
                new ProductRevenue("Q1, 2002", "January", "Eastern U.S.", "Chairs", "Profit", 4000, 36),
                new ProductRevenue("Q1, 2002", "January", "Eastern U.S.", "Monitors", "Profit", 25000, 34),
                new ProductRevenue("Q1, 2002", "February", "Western U.S.", "Pens", "Revenue", 12000, 23),
                new ProductRevenue("Q1, 2002", "February", "Western U.S.", "Chairs", "Revenue", 42000, 47),
                new ProductRevenue("Q1, 2002", "February", "Western U.S.", "Monitors", "Revenue", 160000, 40),
                new ProductRevenue("Q1, 2002", "February", "Western U.S.", "Pens", "Profit", 4000, 23),
                new ProductRevenue("Q1, 2002", "February", "Western U.S.", "Chairs", "Profit", 4000, 47),
                new ProductRevenue("Q1, 2002", "February", "Western U.S.", "Monitors", "Profit", 68000, 40),
                new ProductRevenue("Q1, 2002", "February", "Midwest U.S.", "Pens", "Revenue", 10000, 19),
                new ProductRevenue("Q1, 2002", "February", "Midwest U.S.", "Chairs", "Revenue", 12000, 13),
                new ProductRevenue("Q1, 2002", "February", "Midwest U.S.", "Monitors", "Revenue", 75000, 19),
                new ProductRevenue("Q1, 2002", "February", "Midwest U.S.", "Pens", "Profit", 3000, 20),
                new ProductRevenue("Q1, 2002", "February", "Midwest U.S.", "Chairs", "Profit", 1000, 11),
                new ProductRevenue("Q1, 2002", "February", "Midwest U.S.", "Monitors", "Profit", 32000, 17),
                new ProductRevenue("Q1, 2002", "February", "Eastern U.S.", "Pens", "Revenue", 31000, 58),
                new ProductRevenue("Q1, 2002", "February", "Eastern U.S.", "Chairs", "Revenue", 35000, 39),
                new ProductRevenue("Q1, 2002", "February", "Eastern U.S.", "Monitors", "Revenue", 164000, 41),
                new ProductRevenue("Q1, 2002", "February", "Eastern U.S.", "Pens", "Profit", 8000, 53),
                new ProductRevenue("Q1, 2002", "February", "Eastern U.S.", "Chairs", "Profit", 4000, 44),
                new ProductRevenue("Q1, 2002", "February", "Eastern U.S.", "Monitors", "Profit", 88000, 47),
                new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Pens", "Revenue", 18000, 26),
                new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Chairs", "Revenue", 25000, 54),
                new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Monitors", "Revenue", 220000, 40),
                new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Pens", "Profit", 9000, 29),
                new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Chairs", "Profit", 2000, 40),
                new ProductRevenue("Q1, 2002", "March", "Western U.S.", "Monitors", "Profit", 112000, 38),
                new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Pens", "Revenue", 7000, 10),
                new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Chairs", "Revenue", 6000, 13),
                new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Monitors", "Revenue", 135000, 25),
                new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Pens", "Profit", 2000, 6),
                new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Chairs", "Profit", 1000, 20),
                new ProductRevenue("Q1, 2002", "March", "Midwest U.S.", "Monitors", "Profit", 66000, 23),
                new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Pens", "Revenue", 44000, 64),
                new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Chairs", "Revenue", 15000, 33),
                new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Monitors", "Revenue", 190000, 35),
                new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Pens", "Profit", 20000, 65),
                new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Chairs", "Profit", 2000, 40),
                new ProductRevenue("Q1, 2002", "March", "Eastern U.S.", "Monitors", "Profit", 115000, 39)
        };
        cubeGrid.setData(data);
        cubeGrid.setHilites(HILITES);
        cubeGrid.setWidth100();
        cubeGrid.setHeight100();
        cubeGrid.setHideEmptyFacetValues(true);
        // cubeGrid.setShowCellContextMenus(true);

        cubeGrid.setCanEdit(true);

        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");
                }
            }
        });
        cubeGrid.hiliteFacetValue("month", "February", "myHilite");
        menu.setItems(dataFill);
        cubeGrid.setContextMenu(menu);

        VLayout layout = new VLayout();
        layout.setWidth100();
        layout.setHeight100();
        //layout.addMember(toolStrip);
        layout.addMember(cubeGrid);

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

    private void createFacetValues(Facet facet, String[] facetValues) {
        Set<FacetValue> values = new HashSet<FacetValue>();

        for (String value : facetValues) {
            values.add(new FacetValue(value, value));
        }

        facet.setValues(values.toArray(new FacetValue[0]));
    }

    private void createPeriodValues(Facet facet, int periods) {
        Set<FacetValue> values = new HashSet<FacetValue>();
        for (int i = 1; i <= periods; i++) {
            String period = "" + i;

            while (period.length() < 3) {
                period = "0" + period;
            }

            period = "P" + period;

            values.add(new FacetValue(period, period));
        }

        facet.setValues(values.toArray(new FacetValue[0]));
    }
}