package com.hp.isomorphic.client.issues;

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

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ClientDataSource extends DataSource {
	private RecordList data = null;
	
	public ClientDataSource(RecordList records) {
		setID("SampleDataSource");
		
		DataSourceField dsIdField = new DataSourceField("idField", FieldType.TEXT, "ID");
		DataSourceField dsStatusField = new DataSourceField("statusField", FieldType.TEXT, "Status");
		DataSourceField dsContentField = new DataSourceField("contentField", FieldType.TEXT, "Text");
		DataSourceField dsSortField = new DataSourceField("sortField", FieldType.INTEGER, "Sort");

		dsIdField.setPrimaryKey(true);
		
		List<DataSourceField> fields = new ArrayList<>();
		fields.add(dsIdField);
		fields.add(dsStatusField);
		fields.add(dsContentField);
		fields.add(dsSortField);
		
		for (int i = 0; i < 30; ++i) {
			fields.add(new DataSourceField("column" + i, FieldType.TEXT));
		}
		setFields(fields.toArray(new DataSourceField[fields.size()]));
		
		setClientOnly(true);
		setDataProtocol(DSProtocol.CLIENTCUSTOM);
		data = records;
	}
	
	public void addData(Record[] records) {
		data.addList(records);
		DSResponse response = new DSResponse(getID(), DSOperationType.ADD, records);
		updateCaches(response);
	}

	public void removeData(Record[] records) {
		data.removeList(records);
		DSResponse response = new DSResponse(getID(), DSOperationType.REMOVE, records);
		updateCaches(response);
	}
	
	public void updateData(Record[] records) {
		for (Record record : records) {
			int index = data.indexOf(record);
			data.set(index, record);
		}
		
		DSResponse response = new DSResponse(getID(), DSOperationType.UPDATE, records);
		updateCaches(response);
	}
	
	@Override
	protected Object transformRequest(DSRequest request) {
		DSResponse response = new DSResponse(); 
		response.setOperationType(request.getOperationType());
		ListGridRecord record;
		Object key;
		
		switch (request.getOperationType()) {
		case FETCH:
			Record[] array = data.toArray();
			response.setData(array);
			response.setStartRow(0);
			response.setEndRow(data.getLength() - 1);
			response.setTotalRows(data.getLength());
			response.setStatus(0);
			break;
			
		case ADD:
			record = new ListGridRecord(request.getData());
			data.add(record);
			
			response.setData(record);
			response.setStartRow(data.getLength() - 1);
			response.setEndRow(data.getLength());
			response.setTotalRows(data.getLength());
			response.setStatus(0);

			break;
			
		case UPDATE:
			record = new ListGridRecord(request.getData());
			key = record.getAttributeAsObject("idField");
			Record originalRecord = data.find("idField", key);
			int index = data.indexOf(originalRecord);
			data.set(index, record);

			response.setData(record);
			response.setStatus(0);
			break;
			
		case REMOVE:
			record = new ListGridRecord(request.getData());
			data.remove(record);

			response.setData(record);
			response.setStatus(0);
			break;
			
		default:
			response.setStatus(-1);
			break;
		}
		
		processResponse(request.getRequestId(), response);

		return request.getData();
	}
}
