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.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.GroupStartOpen;
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.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 RecordUpdatesViaUpdateCaches extends VLayout{
	private ListGrid grid;
	
	
	public RecordUpdatesViaUpdateCaches() {
		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);
		
		sortField.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(20);
		final ClientDataSource ds = new ClientDataSource(recordList);
		
		
		grid.setDataSource(ds);
		grid.setFields(fields.toArray(new ListGridField[fields.size()]));
		grid.setShowAllRecords(false);
		grid.setSelectionType(SelectionStyle.MULTIPLE);
		grid.setGroupByMaxRecords(20000);
		grid.setGroupStartOpen(GroupStartOpen.NONE);
		grid.setShowAsynchGroupingPrompt(false);
		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.setWidth100();
		
		Button button = new Button("Add record");
		button.setWidth(150);
		button.setHeight(40);
		button.setWrap(true);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ds.addData(GridData.createRecordList(1).duplicate());
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Add 5 records (Block)");
		button.setWidth(150);
		button.setHeight(40);
		button.setWrap(true);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ds.addData(GridData.createRecordList(5).duplicate());
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Remove selected records (Block)");
		button.setWidth(150);
		button.setHeight(40);
		button.setWrap(true);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ds.removeData(grid.getSelectedRecords());
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update content field");
		button.setWidth(150);
		button.setHeight(40);
		button.setWrap(true);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Record [] selection = grid.getSelectedRecords();
                Record [] records = new Record[selection.length];
                for (int i =0; i < selection.length; i++) {
                	Record record = ds.copyRecord(selection[i]);

                	// Modification:
                    record.setAttribute("contentField", record.getAttributeAsString("contentField") + " - update");
                    records[i] = record;
				}
				
				ds.updateData(records);
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update sort field");
		button.setWidth(150);
		button.setHeight(40);
		button.setWrap(true);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Record [] selection = grid.getSelectedRecords();
                Record [] records = new Record[selection.length];
                for (int i =0; i < selection.length; i++) {
                	Record record = ds.copyRecord(selection[i]);
                    
                    // Modification:
                    record.setAttribute("sortField", GridData.getSortValue());
                    records[i] = record;
				}
				
				ds.updateData(records);
			}
		});
		
		buttonBar.addMember(button);
		
		button = new Button("Update status field (Caches)");
		button.setWidth(150);
		button.setHeight(40);
		button.setWrap(true);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
                Record [] selection = grid.getSelectedRecords();
                Record [] records = new Record[selection.length];
                for (int i =0; i < selection.length; i++) {
                	Record record = ds.copyRecord(selection[i]);
                    // Modification:
                    record.setAttribute("statusField", GridData.getStatusValue());
                    records[i] = record;
				}
				
				ds.updateData(records);
			}
		});
		
		buttonBar.addMember(button);
		
		addMember(buttonBar);
	}
	
}
