package org.helioviewer.jhv.plugins.swek.sources.comesep;

import java.util.Iterator;
import java.util.Locale;

import org.helioviewer.jhv.data.datatype.event.JHVEvent;
import org.helioviewer.jhv.data.datatype.event.JHVEventType;
import org.helioviewer.jhv.data.datatype.event.SWEKParser;
import org.json.JSONException;
import org.json.JSONObject;

public class ComesepParser implements SWEKParser {

    @Override
    public JHVEvent parseEventJSON(JSONObject json, JHVEventType type, int id, long start, long end, boolean full) throws JSONException {
        JHVEvent currentEvent = new JHVEvent(type, id, start, end);

        currentEvent.initParams();
        parseResult(json, currentEvent);
        currentEvent.finishParams();

        return currentEvent;
    }

    private void parseResult(JSONObject result, JHVEvent currentEvent) throws JSONException {
        Iterator<?> keys = result.keys();
        while (keys.hasNext()) {
            parseParameter(result, keys.next(), currentEvent);
        }
    }

    private void parseParameter(JSONObject result, Object key, JHVEvent currentEvent) throws JSONException {
        if (key instanceof String) {
            String keyString = ((String) key).intern();
            String value;
            if (!result.isNull(keyString)) {
                value = result.optString(keyString);
            } else {
                return;
            }

            String lowerkey = keyString.toLowerCase(Locale.ENGLISH);
            if (!(lowerkey.equals("atearliest") || lowerkey.equals("atlatest") ||
                  lowerkey.equals("begin_time_value") || lowerkey.equals("end_time_value") ||
                  lowerkey.startsWith("liftoff"))) {
                value = value.trim();
                if (value.length() != 0)
                    currentEvent.addParameter(keyString, value, true);
            }
        }
    }

}

@@ -105,16 +105,64 @@
 ocale;%0A%0A
+import org.helioviewer.jhv.base.time.TimeUtils;%0A
 import o
@@ -1824,16 +1824,348 @@
 () != 0)
+ %7B%0A                    if (lowerkey.equals(%22atstrongest%22)) %7B%0A                        try %7B%0A                            String t = TimeUtils.apiDateFormat.format(Long.parseLong(value) * 1000L);%0A                            value = t;%0A                        %7D catch (Exception ignore) %7B%0A                        %7D%0A                    %7D
 %0A       
@@ -2203,25 +2203,24 @@
 rameter(
-keyString
+lowerkey
 , value,
@@ -2227,16 +2227,34 @@
  true);%0A
+                %7D%0A
         

Translate COMESEP atstrongest