package com.hp.isomorphic.client.issues;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.shared.GWT;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.data.ResultSet;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.Button;
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.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 ModifyResultSetOnGridSample extends VLayout {
	private ListGrid grid;
	
	public ModifyResultSetOnGridSample() {
		grid = new ListGrid();
		
		ListGridField gridIdField = new ListGridField("idField", "ID", 40);
		ListGridField gridStatusField = new ListGridField("statusField", "Status", 80);
		ListGridField contentField = new ListGridField("contentField", "Content", 300);
		ListGridField sortField = new ListGridField("sortField", "Sort", 100);
		
		gridIdField.setCanDragResize(false);
		gridIdField.setCanReorder(false);
		gridIdField.setType(ListGridFieldType.INTEGER);

		List<ListGridField> fields = new ArrayList<>();
		fields.add(gridIdField);
		fields.add(gridStatusField);
		fields.add(contentField);
		fields.add(sortField);
		
		for (int i = 0; i < 30; ++i) {
			fields.add(new ListGridField("column" + i, "Column " + i, 70));
		}
		
		RecordList recordList = GridData.createRecordList(100);
		DataSource ds = new ClientDataSource(recordList);
		
		grid.setDataSource(ds);
		grid.setFields(fields.toArray(new ListGridField[fields.size()]));
		grid.setShowAllRecords(false);
		grid.setSelectionType(SelectionStyle.MULTIPLE);

//		***********************************************************
//		Adding the sort seems to be what really slows things down.
//		***********************************************************
		grid.setSortField("sortField");
		grid.setAutoFetchData(true);
        
		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);
			}
		});
		
		addMember(grid);
		
		HLayout buttonBar = new HLayout();
		buttonBar.setAutoHeight();
		buttonBar.setAutoWidth();
		
		Button button = new Button("Add record");
		button.setWidth(150);
		button.setHeight(40);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ListGridRecord record = GridData.createRecord();
	        	grid.addData(record);
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Add 50 records");
		button.setWidth(150);
		button.setHeight(40);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (int i = 0; i < 50; ++i) {
					ListGridRecord record = GridData.createRecord();
		        	grid.addData(record);
				}
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Remove selected records");
		button.setWidth(150);
		button.setHeight(40);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				grid.removeSelectedData();
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update content field");
		button.setWidth(150);
		button.setHeight(40);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (ListGridRecord record: grid.getSelectedRecords()) {
					String recordId = record.getAttributeAsString("idField");
					ResultSet resultset = grid.getResultSet();
					if(grid.isGrouped()) {
						resultset = grid.getOriginalResultSet();	
					}
					Record rsRecord = resultset.find("idField", recordId);
					rsRecord.setAttribute("contentField", record.getAttributeAsString("contentField") + " - update");
					resultset.filterLocalData();
					grid.markForRedraw();
				}
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update sort field");
		button.setWidth(150);
		button.setHeight(40);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (ListGridRecord record: grid.getSelectedRecords()) {
					String recordId = record.getAttributeAsString("idField");
					ResultSet resultset = grid.getResultSet();
					if(grid.isGrouped()) {
						resultset = grid.getOriginalResultSet();	
					}
					Record rsRecord = resultset.find("idField", recordId);
					rsRecord.setAttribute("sortField", GridData.getSortValue());
					resultset.filterLocalData();
					grid.markForRedraw();
				}
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update status field");
		button.setWidth(150);
		button.setHeight(40);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (ListGridRecord record: grid.getSelectedRecords()) {
					String recordId = record.getAttributeAsString("idField");
					ResultSet resultset = grid.getResultSet();
					if(grid.isGrouped()) {
						resultset = grid.getOriginalResultSet();	
					}
					
					Record rsRecord = resultset.find("idField", recordId);
					rsRecord.setAttribute("statusField",GridData.getStatusValue());
					resultset.filterLocalData();
					grid.markForRedraw();
				}
			}
		});
		
		buttonBar.addMember(button);
		
		addMember(buttonBar);
	}
}
