package nl.sense_os.commonsense.client.viz.data;

import nl.sense_os.commonsense.client.viz.data.timeseries.BackEndDataPoint;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;

public class GetSensorDataResponseJso extends JavaScriptObject {

    protected GetSensorDataResponseJso() {
        // empty protected constructor
    }

    /**
     * Creates a GetSensorDataResponseJso from the JSON response from CommonSense. Also converts any
     * "embedded" JSON-disguised-as-String objects, so that e.g.
     * <code>{"foo":"{\"bar\":\"baz\"}"}</code> will get completely converted.
     * 
     * @param source
     *            Raw response from CommonSense.
     * @return JavaScriptObject representing the response.
     */
    public final static native GetSensorDataResponseJso create(String source) /*-{
        function stripslashes(str) {
            return (str + '').replace(/\\(.?)/g, function(s, n1) {
                switch (n1) {
                case '\\':
                    return '\\';
                case '0':
                    return '\u0000';
                case '':
                    return '';
                default:
                    return n1;
                }
            });
        }
        var stripped = stripslashes(source);
        var jsonFixed = stripped.replace(/:\"{/g, ':{').replace(/}\"/g, '}');
        return eval('(' + jsonFixed + ')');
    }-*/;

    public final native JsArray<BackEndDataPoint> getData() /*-{
        return this.data;
    }-*/;

    public final native int getTotal() /*-{
        return this.total;
    }-*/;
}

@@ -1493,181 +1493,664 @@
     
-public final native JsArray%3CBackEndDataPoint%3E getData() /*-%7B%0D%0A        return this.data;%0D%0A    %7D-*/;%0D%0A%0D%0A    public final native int getTotal() /*-%7B%0D%0A        return this.total;
+/**%0D%0A     * @return The 'data' property of the response, containing an array with sensor data. If 'data'%0D%0A     *         is undefined, an empty array is returned.%0D%0A     */%0D%0A    public final native JsArray%3CBackEndDataPoint%3E getData() /*-%7B%0D%0A        if (undefined != this.data) %7B%0D%0A            return this.data;%0D%0A        %7D else %7B%0D%0A            return %5B%5D;%0D%0A        %7D%0D%0A    %7D-*/;%0D%0A%0D%0A    /**%0D%0A     * @return The 'total' property of the response. If 'total' is undefined, -1 is returned.%0D%0A     */%0D%0A    public final native int getTotal() /*-%7B%0D%0A        if (undefined != this.total) %7B%0D%0A            return this.total;%0D%0A        %7D else %7B%0D%0A            return -1;%0D%0A        %7D
 %0D%0A  

fix crash when handling missing properties in JSNI.