package com.ripple.client.subscriptions;

import com.ripple.client.pubsub.Publisher;
import com.ripple.core.coretypes.AccountID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Set;
import java.util.TreeSet;

public class SubscriptionManager extends Publisher<SubscriptionManager.events> {
    public static abstract class events<T>      extends Publisher.Callback<T> {}

    public abstract static class OnSubscribed extends events<JSONObject> {}
    public abstract static class OnUnSubscribed extends events<JSONObject> {}

    public enum Stream {
        server,
        ledger,
        transactions,
        transactions_propose
    }

    Set<Stream>                  streams = new TreeSet<Stream>();
    Set<AccountID>              accounts = new TreeSet<AccountID>();

    <T> Set<T> single(T element) {
        Set<T> set = new TreeSet<T>();
        set.add(element);
        return set;
    }

    public void addStream(Stream s) {
        streams.add(s);
        emit(OnSubscribed.class, basicSubscriptionObject(single(s), null));
    }
    public void removeStream(Stream s) {
        streams.remove(s);
        emit(OnUnSubscribed.class, basicSubscriptionObject(single(s), null));
    }
    public void addAccount(AccountID a) {
        accounts.add(a);
        emit(OnSubscribed.class, basicSubscriptionObject(null, single(a)));
    }
    public void removeAccount(AccountID a) {
        accounts.remove(a);
        emit(OnUnSubscribed.class, basicSubscriptionObject(null, single(a)));
    }

    private JSONObject basicSubscriptionObject(Set<Stream> streams, Set<AccountID> accounts) {
        JSONObject subs = new JSONObject();
        try {
            if (streams != null && streams.size() > 0) subs.put("streams", new JSONArray(streams));
            if (accounts != null && accounts.size() > 0) subs.put("accounts", new JSONArray(accounts));
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return subs;
    }

    public JSONObject allSubscribed() {
        return basicSubscriptionObject(streams, accounts);
    }
}

@@ -209,16 +209,45 @@
 bject;%0A%0A
+import java.util.Collection;%0A
 import j
@@ -1810,32 +1810,31 @@
 (%22streams%22, 
-new JSON
+getJson
 Array(stream
@@ -1916,24 +1916,23 @@
 ounts%22, 
-new JSON
+getJson
 Array(ac
@@ -2050,32 +2050,252 @@
 rn subs;%0A    %7D%0A%0A
+    private JSONArray getJsonArray(Collection%3C?%3E streams) %7B%0A        JSONArray jsonArray = new JSONArray();%0A        for (Object obj : streams) %7B%0A            jsonArray.put(obj);%0A        %7D%0A%0A        return jsonArray;%0A    %7D%0A%0A
     public JSONO

Fix issue with android 4.4 where `JSONArray(Collection col)` constructor wasn't working