package fi.nls.oskari.control.statistics.db;

import fi.nls.oskari.util.JSONHelper;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * This is the value object for the layer url and other metadata in the database.
 * MyBatis Type for the SQL table oskari_maplayers
 */
public class RegionSet {

    private long oskariLayerId; // oskari_maplayer.id
    private String oskariLayerName; // oskari_maplayer.name
    private String url; // oskari_maplayer.url
    private String srs; // oskari_maplayer.srs_name
    private String attributes; // oskari_maplayer.attributes

    public long getOskariLayerId() {
        return oskariLayerId;
    }

    public void setOskariLayerId(long oskariLayerId) {
        this.oskariLayerId = oskariLayerId;
    }

    public String getOskariLayerName() {
        return oskariLayerName;
    }

    public void setOskariLayerName(String oskariLayerName) {
        this.oskariLayerName = oskariLayerName;
    }

    public String getAttributes() {
        return attributes;
    }

    public void setAttributes(String attributes) {
        this.attributes = attributes;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getSrs() {
        return srs;
    }

    public void setSrs(String srs) {
        this.srs = srs;
    }

    public JSONObject asJSON() {
        JSONObject tags = new JSONObject();
        try {
            // only regionset id and regionIdTag is used in frontend, and regionIdTag should propably come from datasource...
            //tags.put("nameIdTag", getNameProperty());
            tags.put("regionIdTag", getIdProperty());
            //tags.put("url", getUrl());
            //tags.put("featuresUrl", getFeaturesUrl());
        } catch (JSONException e) {
            throw new RuntimeException("Something went wrong serializing the region set", e);
        }
        return tags;
    }

    public String getNameProperty() {
        return getStatsJSON().optString("nameIdTag");
    }

    public String getIdProperty() {
        return getStatsJSON().optString("regionIdTag");
    }

    public String getFeaturesUrl() {
        return getStatsJSON().optString("featuresUrl");
    }

    private JSONObject getStatsJSON() {
        JSONObject json = JSONHelper.createJSONObject(attributes);
        if(json == null) {
            return new JSONObject();
        }
        JSONObject stats = json.optJSONObject("statistics");

        if(stats == null) {
            return new JSONObject();
        }
        return stats;
    }

}

@@ -579,16 +579,85 @@
 ibutes%0A%0A
+    private JSONObject stats; // Lazily populated by getStatsJSON()%0A%0A
     publ
@@ -2361,32 +2361,65 @@
 etStatsJSON() %7B%0A
+        if (stats == null) %7B%0A    
         JSONObje
@@ -2481,17 +2481,22 @@
     
+    
 if
+ 
 (json 
-=
+!
 = nu
@@ -2515,63 +2515,13 @@
     
-  
-return new JSONObject();%0A        %7D%0A        JSONObject
+     
  sta
@@ -2559,16 +2559,29 @@
 tics%22);%0A
+            %7D
 %0A       
@@ -2581,18 +2581,23 @@
         
+    
 if
+ 
 (stats =
@@ -2618,22 +2618,27 @@
         
-return
+    stats =
  new JSO
@@ -2640,32 +2640,46 @@
 w JSONObject();%0A
+            %7D%0A
         %7D%0A      

Store the stats JSONObject in a lazily populated field