package com.sample.client.test;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.PageKeyHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.HeaderSpan;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;


public class SampleEntryPoint implements EntryPoint{

	private VLayout layout = new VLayout();
	
	private VLayout     vlMainLayout;
	private	ListGrid 	listGrid;
	private static final int HEADER_HEIGHT_SMALL = 46;
	private static final String BORDER_STYLE_GENERAL = "1px solid lightgrey";

	public void onModuleLoad() {

		if (!GWT.isScript()) { 
		      KeyIdentifier debugKey = new KeyIdentifier(); 
		      debugKey.setCtrlKey(true); 
		      debugKey.setKeyName("D"); 
		      Page.registerKey(debugKey, new PageKeyHandler() {
		    
		    @Override
		    public void execute(String keyName) {
		      SC.showConsole();      
		    }   
		      });
		  }			
		init();
	}


	public void init() {
		vlMainLayout = new VLayout();			
		setMainContainer(vlMainLayout, "vlMainLayout" + getComponentId(), null);

		vlMainLayout.addMember(getGrid());

		layout.setWidth100();
		layout.setHeight100();
		layout.setMembersMargin(0);
		layout.addMember(vlMainLayout);

		listGrid.markForRedraw();
		listGrid.setData(SampleData.getNewRecords());
		
		layout.show();
	}

	public static void setMainContainer(VLayout vlMainLayout,  String compoenentID, String containerType ) {
		// default values for all containers
		vlMainLayout.setID(compoenentID);
		vlMainLayout.setWidth100();	
		vlMainLayout.setHeight100();
		vlMainLayout.setMargin(0);	

		vlMainLayout.setOverflow(Overflow.AUTO);		
	}


	private VLayout getGrid() {
		// create this layout so that it can contain the scroll bars instead of the grid
		VLayout vlGridLayout = new VLayout();
		vlGridLayout.setID("vlGridLayout" + getComponentId());
		vlGridLayout.addMember(setupGrid());
		vlGridLayout.setHeight100();
		vlGridLayout.setWidth100();
		return vlGridLayout;
	}

	private ListGrid setupGrid() {

		listGrid = new ListGrid() ;

		setListGrid(listGrid, "listGrid" + getComponentId());		
	
		listGrid.setHeaderHeight(HEADER_HEIGHT_SMALL);
		listGrid.setShowRecordComponents(true);
		listGrid.setShowRecordComponentsByCell(true);

		ListGridField lgf1 = new ListGridField(SampleData.COL_ARTICLE1, ("COL_ARTICLE1"));
		lgf1.setPrompt(("COL_ARTICLE1"));

		ListGridField lgf2 = new ListGridField(SampleData.COL_COUNTRY_CODE1, ("COL_COUNTRY_CODE1"));
		lgf2.setPrompt(("COL_COUNTRY_CODE1"));

		ListGridField lgf3 = new ListGridField(SampleData.COL_DESC150_1,"&nbsp;");
		//lgf3.setShowTitle(false);
		lgf3.setPrompt(("COL_DESC150_1"));

		ListGridField lgf4 = new ListGridField(SampleData.COL_DESC150_2,"&nbsp;");
		//lgf4.setShowTitle(false);
		lgf4.setPrompt(("COL_DESC150_2"));

		ListGridField lgf5 = new ListGridField(SampleData.COL_BACKGROUND2,"&nbsp;");
		//lgf5.setShowTitle(false);
		lgf5.setPrompt(("COL_BACKGROUND2"));

		ListGridField lgf6 = new ListGridField(SampleData.COL_BACKGROUND,"&nbsp;");
		//lgf6.setShowTitle(false);
		lgf6.setPrompt(("COL_BACKGROUND"));

		ListGridField lgf7 = new ListGridField(SampleData.COL_POPULATION1,"&nbsp;");
		//lgf7.setShowTitle(false);
		lgf7.setPrompt(("COL_POPULATION1"));

		ListGridField lgf8 = new ListGridField(SampleData.COL_MORE,"&nbsp;");
		//lgf8.setShowTitle(false);
		lgf8.setPrompt(("COL_MORE"));

		ListGridField lgf9 = new ListGridField(SampleData.COL_POPULATION, ("COL_POPULATION"));
		lgf9.setPrompt(("COL_POPULATION"));



		listGrid.setFields(lgf1, lgf2, 
				lgf3, 
				lgf4, 
				lgf5, 
				lgf6, 
				lgf7, 
				lgf8, lgf9);

		HeaderSpan headerspan =   new HeaderSpan(("DESC150 headerspan"), new String[]{SampleData.COL_DESC150_1,
			SampleData.COL_DESC150_2, 
			SampleData.COL_BACKGROUND2,
			SampleData.COL_BACKGROUND,
			SampleData.COL_POPULATION1,
			SampleData.COL_MORE});

		headerspan.setAlign(Alignment.LEFT);

		listGrid.setHeaderSpans(headerspan);



		return listGrid;
	}


	private String getComponentId() {
		return "test";
	}
	
	public static void setListGrid(ListGrid listGrid, String componentID){

		listGrid.setID(componentID);
		listGrid.setWidth100();  
		listGrid.setHeight100();		
		listGrid.setAlternateRecordStyles(true);
		listGrid.setBorder(BORDER_STYLE_GENERAL);
		listGrid.setShowHeaderContextMenu(false);
		listGrid.setShowEmptyMessage(false);
		listGrid.setEmptyMessage("");		
		listGrid.setBodyOverflow(Overflow.SCROLL);
		listGrid.setLeaveScrollbarGap(true);
		listGrid.setCanSort(false);
		listGrid.setCanResizeFields(true);	
		listGrid.setFastCellUpdates(true);
		// Setting this to false disables keyboard scrolling on all browsers with the exception of MSIE 
		//listGrid.setCanFocus(false);
		listGrid.setShowRollOver(false);
		listGrid.setAutoFitFieldsFillViewport(false);
		listGrid.setSelectionType(SelectionStyle.SINGLE);
		listGrid.setCellPadding(5);
		// this is required so that when a column is sized so small that its contents are clipped, the cell value will be displayed
		// when the user hovers over that cell
		listGrid.setCanHover(null);
		listGrid.setShowClippedValuesOnHover(true);
		listGrid.setAutoSizeHeaderSpans(true);

		// the next 4 options can potentially cause a performance issue

		// if a developer needs to not auto size the grid, they should override these next 2 values
		listGrid.setAutoFitFieldWidths(true);
		listGrid.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);

		// this is added to avoid the lag that we see when scrolling through many records
		listGrid.setShowAllRecords(true);
		listGrid.setShowAllColumns(true);
		//listGrid.setBaseStyle("showGridLines");
		
		//the next 3 values prevents the display of the context menu on header spans 
		listGrid.setShowHeaderSpanTitlesInSortEditor(false);
		listGrid.setCanFreezeFields(false);
		listGrid.setCanPickFields(false);
	}

}