package com.webileapps.excalibur;

import android.content.SharedPreferences;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;

public class ExcaliburConfig {
    private String userId;
    private String clientId;
    private String fcmToken;
    private boolean isStaging = false;
    private ArrayList<ExcaliburConfig.CustomField> fields = new ArrayList();

    public ExcaliburConfig(String clientId) {
        this.clientId = clientId;
    }

    public ExcaliburConfig(String clientId, String userId) {
        this.clientId = clientId;
        this.userId = userId;
    }

    String getUserId() {
        return this.userId;
    }

    String getClientId() {
        return this.clientId;
    }

    String getFcmToken() {
        return this.fcmToken;
    }

    boolean getStaging() {
        return this.isStaging;
    }

    void setUserId(String userId) {
        this.userId = userId;
    }

    public void setFcmToken(String fcmToken) {
        this.fcmToken = fcmToken;
    }

    public void setStaging(boolean staging) {
        this.isStaging = staging;
    }

    public void putCustomField(String key, String value, ExcaliburConfig.Scope scope) {
        this.fields.add(new ExcaliburConfig.CustomField(key, value, scope));
    }

    public void putCustomField(String key, String value) {
        this.fields.add(new ExcaliburConfig.CustomField(key, value, (ExcaliburConfig.Scope)null));
    }

    void save(SharedPreferences preferences) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("CLIENT_ID", this.clientId);
        editor.putString("USER_ID", this.userId);
        editor.putString("FCM_TOKEN", this.fcmToken);
        editor.putBoolean("IS_STAGING", this.isStaging);
        JSONObject object = new JSONObject();
        Iterator var4 = this.fields.iterator();

        while(var4.hasNext()) {
            ExcaliburConfig.CustomField field = (ExcaliburConfig.CustomField)var4.next();

            try {
                if (field.scope != null) {
                    JSONObject innerObject = new JSONObject();
                    JSONObject scopeObject = new JSONObject();
                    scopeObject.put("scope", field.scope.name().toLowerCase());
                    innerObject.put("value", field.value);
                    innerObject.put("options", scopeObject);
                    object.put(field.key, innerObject);
                } else {
                    object.put(field.key, field.value);
                }
            } catch (JSONException var8) {
                var8.printStackTrace();
            }
        }

        Log.d("VC", object.toString());
        editor.putString("CUSTOM_FIELDS", object.toString());
        editor.apply();
    }

    class CustomField {
        String key;
        String value;
        ExcaliburConfig.Scope scope;

        public CustomField(String key, String value, ExcaliburConfig.Scope scope) {
            this.key = key;
            this.value = value;
            this.scope = scope;
        }
    }

    public static enum Scope {
        USER,
        ROOM;

        private Scope() {
        }
    }
}
