package com.focuss.basicenv.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.UserSummary;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point for test SmartGWT GWT module.
 */
public class Application implements EntryPoint {
    /**
     * This is called when the browser loads Application.html.
     */
    @SuppressWarnings("deprecation")
	public void onModuleLoad() {
        GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
            public void onUncaughtException(Throwable t) {
                System.err.println("--- UNCAUGHT EXCEPTION ---");
                t.printStackTrace();
            }
        });
        
        KeyIdentifier debugKey = new KeyIdentifier();
        debugKey.setCtrlKey(true);
        debugKey.setKeyName("D");

        Page.registerKey(debugKey, new KeyCallback() {
			
			@Override
			public void execute(final String keyName) {
				SC.showConsole();
			}
		} );
        
        buffersExample();
//        
//        HLayout hLayout = new HLayout();
//        hLayout.setMargin(10);
//
//        // *************** Add widgets to hLayout here. ****************
//        Label label = new Label("Hello world!");
//        hLayout.addMember(label);
//
//        hLayout.draw();
    }
    
    private void buffersExample() {
    	VLayout exampleLayout = new VLayout();



    	exampleLayout.setHeight100();
    	exampleLayout.setWidth100();

    	DataSource buffersDS = DataSource.get("bufferTable");
    	ListGrid gridExample = new ListGrid(buffersDS);

    	gridExample.setAutoFetchData(true);
    	gridExample.setDataPageSize(50);
    	gridExample.setID("ListGridBuffers");
    	gridExample.setHeight("60%");
    	gridExample.setWidth("80%");
    	gridExample.setCanAddSummaryFields(true);
    	gridExample.setCanGroupBy(true);
    	gridExample.setAutoFitFieldWidths(true);
    	gridExample.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);

    	Map<String,String> summaryVars = new HashMap<String, String>();
    	summaryVars.put("A", "SKU");
    	summaryVars.put("B", "LOCATION");

    	ListGridField summaryText = new ListGridField("textField", "Texto", 100);
    	summaryText.setCanEdit(false);
    	summaryText.setCanSortClientOnly(true);
    	summaryText.setCanFilter(true);
    	UserSummary textSummary = new UserSummary("#{A}#{B}", summaryVars);
    	summaryText.setUserSummary(textSummary);

    	gridExample.draw();



    	exampleLayout.addMember(gridExample);

    	gridExample = addField(summaryText, gridExample);

    	exampleLayout.draw();
    	
	}
    
    private ListGrid addField(ListGridField field, ListGrid listGrid) {
    	ArrayList<ListGridField> gridFields = new ArrayList<ListGridField>(Arrays.asList(listGrid.getFields()));
    	gridFields.add(field);
    	listGrid.setFields(gridFields.toArray(new ListGridField[gridFields.size()]));
    	listGrid.markForRedraw();
    	return listGrid;
    }
    
   
    
    
}

