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

package com.hp.isomorphic.client.issues;

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.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.ResultSet;
import com.smartgwt.client.types.DSOperationType;
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.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class FullGridSample extends VLayout {
	private ListGrid grid;
	
	public FullGridSample() {
		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);
		
		final DataSource ds = new FullGridSampleDataSource();

		grid.setShowAllRecords(true);
		grid.setAutoFetchData(true);
		grid.setDataSource(ds);
		grid.setSelectionType(SelectionStyle.MULTIPLE);
		grid.setSortField("sortField");
		grid.setGroupByMaxRecords(2500);
		
		AdvancedCriteria criteria = new AdvancedCriteria("contentField", OperatorId.NOT_CONTAINS, "update");
		grid.setCriteria(criteria);

		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 = FullGridSampleDataSource.createRecord();
				updateCache(grid, record, DSOperationType.ADD);
			}
		});
		
		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()) {
					record.setAttribute("contentField", record.getAttributeAsString("contentField") + " - update");
					updateCache(grid, record, DSOperationType.UPDATE);
				}
			}
		});
		
		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()) {
					record.setAttribute("sortField", FullGridSampleDataSource.getSortValue());
					updateCache(grid, record, DSOperationType.UPDATE);
				}
			}
		});
		
		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()) {
					record.setAttribute("statusField", FullGridSampleDataSource.getStatusValue());
					updateCache(grid, record, DSOperationType.UPDATE);
				}
			}
		});
		
		buttonBar.addMember(button);
		
		addMember(buttonBar);
	}

	private static void updateCache(ListGrid grid, Record record, DSOperationType opType) {
		DSRequest request = new DSRequest();
		request.setOperationType(opType);

		DSResponse response = new DSResponse();
		ListGridRecord lgRecord = new ListGridRecord();
		for (String attrName: record.getAttributes()) {
			lgRecord.setAttribute(attrName, record.getAttributeAsObject(attrName));
		}
			
		response.setData(new ListGridRecord[]{ lgRecord });
		response.setOperationType(opType);
		response.setStatus(0);

		if (opType == DSOperationType.ADD) {
			ResultSet originalResultSet = grid.getOriginalResultSet();
			int rowCount = originalResultSet.getLength();
			request.setStartRow(rowCount);
			request.setEndRow(rowCount + 1);
			response.setStartRow(rowCount);
			response.setEndRow(rowCount + 1);
			response.setTotalRows(rowCount + 1);
		}

		DataSource dataSource = grid.getDataSource();
		dataSource.updateCaches(response, request);
	}
}
