package com.cloudkick.monitoring;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class CheckState {
	public final String status;
	public final String whence;
	public final String serviceState;
	public final Integer stateColor;

	public CheckState(JSONObject state) throws JSONException {
		whence = state.getString("whence");
		serviceState = state.getString("service_state");
		if (serviceState.equals("OK")) {
			status = state.getString("status");
			stateColor = 0xFFA9F5A9;
		}
		else if (serviceState.equals("WARNING")) {
			status = state.getString("status");
			stateColor = 0xFFFAAC58;
		}
		else if (serviceState.equals("ERROR")) {
			status = state.getString("status");
			stateColor = 0xFFF78181;
		}
		else if (serviceState.equals("NO-AGENT")) {
			status = "Agent Not Connected";
			stateColor = 0xFFBDBDBD;
		}
		else {
			Log.e("Check", "Unknown Service State: " + serviceState);
			status = state.getString("status");
			stateColor =  0xFFBDBDBD;
		}
	}
}
@@ -334,94 +334,383 @@
 %7B%0A%09%09
-whence = state.getString(%22whence%22);%0A%09%09serviceState = state.getString(%22service_state%22);
+// Grab the %22whence%22 if available%0A%09%09if (state.has(%22whence%22)) %7B%0A%09%09%09whence = state.getString(%22whence%22);%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09whence = null;%0A%09%09%7D%0A%0A%09%09// Grab the %22service_state%22 if available%0A%09%09if (state.has(%22service_state%22)) %7B%0A%09%09%09serviceState = state.getString(%22service_state%22);%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09serviceState = %22UNKNOWN%22;%0A%09%09%7D%0A%0A%09%09// Depending on the serviceState set the status and color
 %0A%09%09i
@@ -1084,24 +1084,24 @@
 -AGENT%22)) %7B%0A
-
 %09%09%09status = 
@@ -1151,24 +1151,134 @@
 BDBDBD;%0A%09%09%7D%0A
+%09%09else if (serviceState.equals(%22UNKNOWN%22)) %7B%0A%09%09%09status = %22No Data Available%22;%0A%09%09%09stateColor = 0xFFBDBDBD;%0A%09%09%7D%0A
 %09%09else %7B%0A%09%09%09

Handle unknown states