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.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ClientDataSource extends DataSource {
	private ListGridRecord[] data;
	
	public ClientDataSource(ListGridRecord[] 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");
		DataSourceField dsDateField = new DataSourceField("dateField", FieldType.DATETIME, "Date");

		dsIdField.setPrimaryKey(true);
		
		List<DataSourceField> fields = new ArrayList<>();
		fields.add(dsIdField);
		fields.add(dsStatusField);
		fields.add(dsContentField);
		fields.add(dsSortField);
		fields.add(dsDateField);
		
		for (int i = 0; i < 30; ++i) {
			DataSourceField dataSourceField = new DataSourceField("column" + i, FieldType.TEXT);
			fields.add(dataSourceField);
			if (i > 10) {
				dataSourceField.setHidden(true);
			}
		}
		setFields(fields.toArray(new DataSourceField[fields.size()]));
		
		setDataProtocol(DSProtocol.CLIENTCUSTOM);
		data = records;
	}
	
	public ClientDataSource(List<ListGridField> fields, ListGridRecord[] records) {
		setID("SampleDataSource");
		
		List<DataSourceField> dsFields = new ArrayList<>();
		for (ListGridField lgField: fields) {
			FieldType type;
			switch (lgField.getType()) {
			case DATETIME:
				type = FieldType.DATETIME;
				break;
				
			case INTEGER:
				type = FieldType.INTEGER;
				break;
				
			default:
				type = FieldType.TEXT;
				break;
			}
			DataSourceField dsField = new DataSourceField(lgField.getName(), type, lgField.getTitle());
			if (lgField.getName().equals("joblet_id")) {
				dsField.setPrimaryKey(true);
			}
			
			dsFields.add(dsField);
		}

		setFields(dsFields.toArray(new DataSourceField[dsFields.size()]));
		
		setDataProtocol(DSProtocol.CLIENTCUSTOM);
		data = records;
	}

	public void addData(Record[] records) {
		DSResponse response = new DSResponse(getID(), DSOperationType.ADD, records);
		updateCaches(response);
	}

	public void removeData(Record[] records) {
		DSResponse response = new DSResponse(getID(), DSOperationType.REMOVE, records);
		updateCaches(response);
	}
	
	public void updateData(Record[] records) {
		DSResponse response = new DSResponse(getID(), DSOperationType.UPDATE, records);
		updateCaches(response);
	}
	
	@Override
	protected Object transformRequest(DSRequest request) {
		DSResponse response = new DSResponse(); 
		response.setOperationType(request.getOperationType());
		Record record;
		
		switch (request.getOperationType()) {
		case FETCH:
			response.setData(data);
			response.setStartRow(0);
			response.setEndRow(data.length - 1);
			response.setTotalRows(data.length);
			response.setStatus(0);
			data = null;
			break;
			
		case ADD:
			record = new ListGridRecord(request.getData());
			response.setData(record);
			response.setStatus(0);

			break;
			
		case UPDATE:
			record = new ListGridRecord(request.getData());
			response.setData(record);
			response.setStatus(0);
			break;
			
		case REMOVE:
			record = new ListGridRecord(request.getData());
			response.setData(record);
			response.setStatus(0);
			break;
			
		default:
			response.setStatus(-1);
			break;
		}
		
		processResponse(request.getRequestId(), response);

		return request.getData();
	}
}
