package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

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

	public BuiltInDS() {
		
	}
	
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        KeyIdentifier debugKey = new KeyIdentifier();
        debugKey.setCtrlKey(true);
        debugKey.setKeyName("Q");

        Page.registerKey(debugKey, new KeyCallback() {
            public void execute(String keyName) {
                SC.showConsole();
            }
        });

        /*
         * Try to force the dynamic load a datasource which may
         * not have been loaded through the .html file 	
         */
        DataSource ds = DataSource.get("animals_2");
        if (ds == null) {
        	loadDataSources("animals_2");
        }
        else {
        	System.err.println("Skipping dyamic load as already loaded");
        }
        
        Canvas c = createView();
        c.draw();
    }
    
    public Canvas createView() {
        VLayout vLayout = new VLayout();
        vLayout.setWidth("100%");
        vLayout.setHeight("90%");
        vLayout.setMembersMargin(10);
    	
        Canvas c1 = createAnimalGrid("animals");
        Canvas c2 = createAnimalGrid("animals_2");
    
        vLayout.addMember(c1);
        vLayout.addMember(c2);
        return vLayout;
    }
    
    public Canvas createAnimalGrid(final String dsId) {
        VLayout vLayout = new VLayout();
        vLayout.setWidth("70%");
        vLayout.setHeight("80%");
        vLayout.setMembersMargin(10);

        final ListGrid grid = new ListGrid();
        grid.setHeight("100%");
        grid.setAlternateRecordStyles(true);
        
        ListGridField regNameField = new ListGridField("commonName", "Regular Name", 200);
        ListGridField sciNameField = new ListGridField("scientificName", "Official Name", 200);
        ListGridField lifespanField = new ListGridField("lifeSpan", "Life Expectancy", 50);
        
        grid.setFields((new ListGridField[] {
        		regNameField, sciNameField, lifespanField
        }));
        
        DataSource ds = DataSource.get(dsId);
        if (ds != null) {
        	grid.setDataSource(ds);
        	grid.fetchData();
        }
        else {
        	System.err.println("Unable to set grid datasource with id : " + dsId +
        			" as the DataSource was null");
        }

        IButton refreshButton = new IButton("Refresh");
        refreshButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
            	DataSource ds = grid.getDataSource();
            	if (ds != null) {
            		grid.invalidateCache();
            		grid.fetchData();
            	}
            	else{
            		ds = DataSource.get(dsId);
            		if (ds != null) {
                		grid.invalidateCache();
                		grid.setDataSource(ds);
                		grid.fetchData();          			
            		}
            		else {
            			System.err.println("Datasource is still null while refreshing");
            		}
            	}
            }
        });

        vLayout.addMember(grid);
        vLayout.addMember(refreshButton);
        
        return vLayout;
    }

    public ListGridField[] getFields() {
    	ListGridField regNameField = new ListGridField("commonName", "Regular Name", 200);
    	ListGridField sciNameField = new ListGridField("scientificName", "Official Name", 200);
    	ListGridField lifespanField = new ListGridField("lifeSpan", "Life Expectancy", 50);
    
    	
    	return new ListGridField[] {
    		regNameField, sciNameField, lifespanField
    	};
    }

    /**
     * Here we attempt to load datasources which otherwise
     * would have been loaded through the .html file
     */
    public void loadDataSources(final String dsName) {
    	DataSource.load(dsName,
    			new com.smartgwt.client.core.Function() {
    		@Override
    		public void execute() {
    			DataSource ds = DataSource.get(dsName); /* try to force it in */
    			System.err.println("Loaded new DATASOURCES");
    			
    		}
    	},
    	true);
    }

}
