package org.xcolab.entity.utils;

import org.json.JSONObject;

import org.xcolab.client.admin.AdminClient;
import org.xcolab.client.admin.pojo.ConfigurationAttribute;
import org.xcolab.util.attributes.AttributeGetter;

import java.util.ArrayList;
import java.util.List;

public abstract class WidgetPreference {

    public abstract AttributeGetter<String> getConfigurationAttribute();

    protected String preferenceId;
    protected JSONObject prefs;
    private final List<String> allPreferenceIds = new ArrayList<>();

    private final static String DEFAULT_ID = "default";
    private final static String PREFERENCES_JSON_OBJECT = "preferences";

    public WidgetPreference(String id) {

        prefs = new JSONObject(getConfigurationAttribute().get());

        if (prefs.has(PREFERENCES_JSON_OBJECT)) {

            JSONObject preferencesArray = prefs.getJSONObject(PREFERENCES_JSON_OBJECT);

            if (id != null) {
                preferenceId = id;
                preferencesArray.keySet().stream().forEach(s -> allPreferenceIds.add(s));
            } else {
                preferenceId = DEFAULT_ID;
                allPreferenceIds.add(DEFAULT_ID);
            }
            if(preferencesArray.has(preferenceId)) {
                prefs = preferencesArray.getJSONObject(preferenceId);
            }else{
                prefs = preferencesArray.getJSONObject(DEFAULT_ID);
                preferenceId = DEFAULT_ID;
            }

        }else{
            allPreferenceIds.add(DEFAULT_ID);
            preferenceId = DEFAULT_ID;
        }
    }

    public WidgetPreference() {
        this(DEFAULT_ID);
    }

    protected void savePreferences(JSONObject prefsToSave, String id) {
        id = (id==null?(DEFAULT_ID):(id));
        JSONObject currentPreferences = new JSONObject(getConfigurationAttribute().get());
        if (!currentPreferences.has(PREFERENCES_JSON_OBJECT)) {
            JSONObject defaultPrefs =currentPreferences ;

            currentPreferences = new JSONObject();
            JSONObject preferences = new JSONObject();
            preferences.put(id,prefsToSave);
            if(!id.equals(DEFAULT_ID)){
                preferences.put(DEFAULT_ID,defaultPrefs);
            }

            currentPreferences.put(PREFERENCES_JSON_OBJECT,preferences);

        }else{

            JSONObject preferences = currentPreferences.getJSONObject(PREFERENCES_JSON_OBJECT);

            preferences.put(id,prefsToSave);
            currentPreferences.put(PREFERENCES_JSON_OBJECT,preferences);
        }
        ConfigurationAttribute configurationAttribute = new ConfigurationAttribute();
        configurationAttribute.setName(getConfigurationAttribute().name());
        configurationAttribute.setStringValue(currentPreferences.toString());
        AdminClient.updateConfigurationAttribute(configurationAttribute);
    }

    public String getPreferenceId() {
        return preferenceId;
    }

    public void setPreferenceId(String preferenceId) {
        this.preferenceId = preferenceId;
    }

    public JSONObject getPrefs() {
        return prefs;
    }

    public void setPrefs(JSONObject prefs) {
        this.prefs = prefs;
    }

    public List<String> getAllPreferenceIds() {
        return allPreferenceIds;
    }


}

@@ -888,33 +888,32 @@
 S_JSON_OBJECT);%0A
-%0A
             if (
@@ -912,43 +912,62 @@
     
-if (id != null) %7B%0A                p
+//preferencesArray.keySet().stream().forEach(s -%3E allP
 refe
@@ -977,13 +977,17 @@
 ceId
- = id
+s.add(s))
 ;%0A  
@@ -988,35 +988,50 @@
 ));%0A            
-   
+for(String prefId:
  preferencesArra
@@ -1044,55 +1044,135 @@
 et()
-.stream().forEach(s -%3E allPreferenceIds.add(s))
+)%7B%0A                allPreferenceIds.add(prefId);%0A            %7D%0A%0A            if (id != null) %7B%0A                preferenceId = id
 ;%0A  

fix for COLAB-2099