// Description: 
// Isomorphic thread: 
// Resolution: 

package com.hp.isomorphic.client.issues;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionUpdatedEvent;
import com.smartgwt.client.widgets.grid.events.SelectionUpdatedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class GridDataSample extends VLayout {
	private ListGrid grid;
	
	public GridDataSample() {
		grid = new ListGrid();
		
		ListGridField gridIdField = new ListGridField("idField", "ID", 40);
		gridIdField.setCanDragResize(false);
		gridIdField.setCanReorder(false);
		gridIdField.setType(ListGridFieldType.INTEGER);
		
		ListGridField gridStatusField = new ListGridField("statusField", "Status", 80);

		ListGridField contentField = new ListGridField("contentField", "Content", 300);

		ListGridField sortField = new ListGridField("sortField", "Sort", 100);

		grid.setFields(gridIdField, gridStatusField, contentField, sortField);
		
		grid.setShowAllRecords(false);
		grid.setSelectionType(SelectionStyle.MULTIPLE);
		grid.setSortField("sortField");
		grid.setGroupByMaxRecords(20000);
        
		AdvancedCriteria criteria = new AdvancedCriteria("contentField", OperatorId.NOT_CONTAINS, " - update");
		grid.setCriteria(criteria);

		grid.addSelectionUpdatedHandler(new SelectionUpdatedHandler() {
			@Override
			public void onSelectionUpdated(SelectionUpdatedEvent event) {
				GWT.log("Grid selection changed: " + grid.getSelectedRecords().length);
			}
		});
		
		grid.setData(GridData.createRecords(20));
		
		addMember(grid);
		
		HLayout buttonBar = new HLayout();
		buttonBar.setAutoHeight();
		buttonBar.setAutoWidth();
		
		Button button = new Button("Add record");
		button.setWidth("150px");
		button.setHeight("40px");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ListGridRecord record = GridData.createRecord();
				grid.addData(record);
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Remove selected records");
		button.setWidth("150px");
		button.setHeight("40px");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				grid.removeSelectedData();
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update content field");
		button.setWidth("150px");
		button.setHeight("40px");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (ListGridRecord record: grid.getSelectedRecords()) {
					String recordId = record.getAttributeAsString("idField");
					Record updatedRecord = findRecord(grid, recordId);
					updatedRecord.setAttribute("contentField", record.getAttributeAsString("contentField") + " - update");
					updateRecord(grid, updatedRecord);
				}
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update sort field");
		button.setWidth("150px");
		button.setHeight("40px");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (ListGridRecord record: grid.getSelectedRecords()) {
					String recordId = record.getAttributeAsString("idField");
					Record updatedRecord = findRecord(grid, recordId);
					updatedRecord.setAttribute("sortField", GridData.getSortValue());
					updateRecord(grid, updatedRecord);
				}
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update status field");
		button.setWidth("150px");
		button.setHeight("40px");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (ListGridRecord record: grid.getSelectedRecords()) {
					String recordId = record.getAttributeAsString("idField");
					Record updatedRecord = findRecord(grid, recordId);
					updatedRecord.setAttribute("statusField", GridData.getStatusValue());
					updateRecord(grid, updatedRecord);
				}
			}
		});
		
		buttonBar.addMember(button);
		
		addMember(buttonBar);
	}
	
	private void updateRecord(ListGrid grid, Record record) {
		int index = grid.getRecordIndex(record);
		grid.refreshRow(index);
		grid.resort();
		grid.setCriteria(grid.getCriteria());
		grid.groupBy(grid.getGroupByFields());
	}

	private static Record findRecord(ListGrid grid, String recordId) {
		return grid.getRecordList().find("idField", recordId);
	}
}
