package com.hp.isomorphic.client.issues;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class FullGridSampleDataSource extends DataSource {
	private static final int INITIAL_RECORD_COUNT = 40;
	private static int id = 0;
	
	public FullGridSampleDataSource() {
		DataSourceField dsIdField = new DataSourceField("idField", FieldType.TEXT, "ID");
		dsIdField.setPrimaryKey(true);
		
		DataSourceField dsStatusField = new DataSourceField("statusField", FieldType.TEXT, "Status");

		DataSourceField dscontentField = new DataSourceField("contentField", FieldType.TEXT, "Text");
		
		DataSourceField dsSortField = new DataSourceField("sortField", FieldType.INTEGER, "Sort");

		setFields(dsIdField, dsStatusField, dscontentField, dsSortField);
		
		ListGridRecord[] records = new ListGridRecord[INITIAL_RECORD_COUNT];
		
		for (int i = 0; i < records.length; ++i) {
			records[i] = createRecord();
		}
		
		setCacheData(records);
		setClientOnly(true);
	}
	
	public static String getStatusValue() {
		Double r = Math.random() * 4;
		switch (r.intValue()) {
		case 3:
			return "Unknown";
		case 2:
			return "Error";
		case 1:
			return "Warning";
		default:
			return "OK";
		}
	}
	
	public static int getSortValue() {
		Double r = Math.random() * 1000;
		return r.intValue();
	}
	
	public static ListGridRecord createRecord() {
		ListGridRecord record = new ListGridRecord();
		record.setAttribute("idField", id);
		record.setAttribute("statusField", getStatusValue());
		record.setAttribute("contentField", "Content " + id);
		record.setAttribute("sortField", getSortValue());
		++id;
		return record;
	}
}
