package com.hp.isomorphic.client.issues;

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

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class GridData extends DataSource {
	private static Integer id = 0;
	
	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.toString());
		record.setAttribute("statusField", getStatusValue());
		record.setAttribute("contentField", "Content " + id);
		record.setAttribute("sortField", getSortValue());
		record.setAttribute("dateField", new Date());
		
		for (int i = 0; i < 100; ++i) {
			record.setAttribute("column" + i, "Data " + i);
		}

		++id;
		return record;
	}
	
	public static ListGridRecord[] createRecordList(int count) {
		List<ListGridRecord> records = new ArrayList<>();
		for (int i = 0; i < count; ++i) {
			records.add(createRecord());
		}
		return records.toArray(new ListGridRecord[0]);
	}
	
	@SuppressWarnings("unchecked")
	public static List<ListGridRecord> createRecordList(List<Object> jobs) {
		List<ListGridRecord> records = new ArrayList<>();
		for (Object jobObject: jobs) {
			JsonObject job = (JsonObject)jobObject;
			List<Object> jobAttributes = (List<Object>)job.getValue();
			List<Object> jobProperties = null;
			for (Object jobAttribute: jobAttributes) {
				if (((JsonObject)jobAttribute).getName().equals("properties")) {
					jobProperties = (List<Object>)((JsonObject)jobAttribute).getValue();
					break;
				}
			}

			ListGridRecord record = createRecord(jobProperties);
			records.add(record);
		}

		return records;
	}

	private static ListGridRecord createRecord(List<Object> jobProperties) {
		ListGridRecord record = new ListGridRecord();
		
		for (Object jobProperty: jobProperties) {
			JsonObject jp = (JsonObject)jobProperty;
			String name = jp.getName();
			if (name.equals("joblet_queueOrderTime")) {
				Object value = jp.getValue();
				if (value == null) {
					record.setAttribute(name, (Long)null);
				}
				else {
					record.setAttribute(name, new Long(Long.parseLong(value.toString())));
				}
			}
			else if (name.equals("joblet_priority") || name.equals("joblet_id")) {
				Object value = jp.getValue();
				if (value == null) {
					record.setAttribute(name, (Integer)null);
				}
				else {
					record.setAttribute(name, Integer.parseInt(value.toString()));
				}
			}
			else if (name.equals("joblet_nextDropDeadDate") || name.equals("joblet_startDropDeadDate") || name.equals("joblet_endDropDeadDate")) {
				Object value = jp.getValue();
				if (value == null) {
					record.setAttribute(name, (Date)null);
				}
				else {
					record.setAttribute(name, DateUtils.parse(value.toString()));
				}
			}
			else {
				Object value = jp.getValue();
				if (value == null) {
					record.setAttribute(name, (String)null);
				}
				else {
					record.setAttribute(name, value.toString());
				}
			}
		}
		
		return record;
	}
}
