package gr.demokritos.iit.ydsapi.model;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.Objects;

public class DashboardConfig {
    private final String userId;
    private final String title;
    private final String cookiesObject;

    public DashboardConfig(String user_id, String title, String cookiesObject) {
        this.userId = user_id;
        this.title = title;
        this.cookiesObject = cookiesObject;
    }

    public String getUserId() {
        return userId;
    }

    public String getTitle() {
        return title;
    }

    public String getCookiesObject() {
        return cookiesObject;
    }

    @Override
    public String toString() {
        return "DashboardConfig{" + title + ":" + cookiesObject + "}";
    }

    public String toJSON() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder
                .registerTypeAdapter(DashboardConfig.class, new DashboardConfigSerializer())
                .disableHtmlEscaping()
                .setPrettyPrinting();
        Gson gson = gsonBuilder.create();

        return gson.toJson(this);
    }

    public JsonElement toJSONElement() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder
                .registerTypeAdapter(DashboardConfig.class, new DashboardConfigSerializer())
                .disableHtmlEscaping()
                .setPrettyPrinting();
        Gson gson = gsonBuilder.create();

        return gson.toJsonTree(this);
    }

    @Override
    public int hashCode() {
        int hash = 3;

        hash = 17 * hash + Objects.hashCode(this.title);
        hash = 17 * hash + Objects.hashCode(this.cookiesObject);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DashboardConfig other = (DashboardConfig) obj;
        if (!Objects.equals(this.title, other.title)) {
            return false;
        }
        return Objects.equals(this.cookiesObject, other.cookiesObject);
    }

    public static final String FLD_USERID = "userId";
    public static final String FLD_TITLE = "title";
    public static final String FLD_PARAMS = "parameters";

    /**
     * Helper class to serialize as needed in the API
     */
    class DashboardConfigSerializer implements JsonSerializer<DashboardConfig> {
        @Override
        public JsonElement serialize(DashboardConfig dc, Type type, JsonSerializationContext jsc) {
            final JsonObject jsonObject = new JsonObject();

            // Add userId & title
            jsonObject.addProperty(DashboardConfig.FLD_USERID, dc.getUserId());
            jsonObject.addProperty(DashboardConfig.FLD_TITLE, dc.getTitle());

            // Add parameters. Should be a JSON object, so we always parse it
            jsonObject.add(FLD_PARAMS, new JsonParser()
                    .parse(dc.getCookiesObject())
                    .getAsJsonObject());
            return jsonObject;
        }
    }
}

@@ -179,24 +179,60 @@
 ing userId;%0A
+    private final String dashboard;%0A
     private 
@@ -334,16 +334,34 @@
 user_id,
+ String dashboard,
  String 
@@ -422,16 +422,52 @@
 ser_id;%0A
+        this.dashboard = dashboard;%0A
         
@@ -529,32 +529,563 @@
 sObject;%0A    %7D%0A%0A
+    public DashboardConfig(String jsonDashboardConfig) %7B%0A        DashboardConfig dc = new GsonBuilder()%0A                .registerTypeAdapter(DashboardConfig.class, new DashboardConfigDeserializer())%0A                .create()%0A                .fromJson(jsonDashboardConfig, getClass());%0A%0A        this.userId = dc.getUserId();%0A        this.dashboard = dc.getDashboard();%0A        this.title = dc.getTitle();%0A        this.cookiesObject = dc.getCookiesObject();%0A    %7D%0A%0A    public String getDashboard() %7B%0A        return dashboard;%0A    %7D%0A%0A
     public Strin
@@ -2181,16 +2181,76 @@
 sh = 3;%0A
+        hash = 17 * hash + Objects.hashCode(this.dashboard);
 %0A       
@@ -2650,16 +2650,116 @@
 g) obj;%0A
+        if (!Objects.equals(this.dashboard, other.dashboard)) %7B%0A            return false;%0A        %7D%0A
         
@@ -2975,16 +2975,76 @@
 serId%22;%0A
+    public static final String FLD_DASHBOARD = %22dashboard%22;%0A
     publ
@@ -3480,42 +3480,8 @@
 );%0A%0A
-            // Add userId & title%0A
     
@@ -3552,24 +3552,110 @@
 tUserId());%0A
+            jsonObject.addProperty(DashboardConfig.FLD_DASHBOARD, dc.getDashboard());%0A
             
@@ -3992,11 +3992,906 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Helper class to deserialize a Dashboard configuration JSON String%0A     */%0A    class DashboardConfigDeserializer implements JsonDeserializer%3CDashboardConfig%3E %7B%0A        @Override%0A        public DashboardConfig deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException %7B%0A            final JsonObject jsonObject = json.getAsJsonObject();%0A%0A            // Get values from JSON%0A            final String userId = jsonObject.get(DashboardConfig.FLD_USERID).getAsString();%0A            final String dashboard = jsonObject.get(DashboardConfig.FLD_DASHBOARD).getAsString();%0A            final String title = jsonObject.get(DashboardConfig.FLD_TITLE).getAsString();%0A            final String params = jsonObject.get(DashboardConfig.FLD_PARAMS).getAsString();%0A%0A            return new DashboardConfig(userId, dashboard, title, params);%0A        %7D%0A    %7D
 %0A%7D%0A

Add dashboard ID to the Dashboard configuration model