package peergos.shared.storage;

import jsinterop.annotations.*;
import peergos.shared.cbor.*;

import java.util.*;

public final class PaymentProperties  implements Cborable {

    public final Optional<String> paymentServerUrl;
    public final String clientSecret;

    public PaymentProperties() {
        this.paymentServerUrl = Optional.empty();
        this.clientSecret = "";
    }

    public PaymentProperties(String paymentServerUrl, String clientSecret) {
        this.paymentServerUrl = Optional.of(paymentServerUrl);
        this.clientSecret = clientSecret;
    }

    @JsMethod
    public boolean isPaid() {
        return paymentServerUrl.isPresent();
    }

    @JsMethod
    public String getUrl() {
        return paymentServerUrl.get();
    }

    @JsMethod
    public String getClientSecret() {
        return clientSecret;
    }

    @Override
    public CborObject toCbor() {
        SortedMap<String, Cborable> state = new TreeMap<>();
        paymentServerUrl.ifPresent(url -> state.put("url", new CborObject.CborString(url)));
        if (clientSecret.length() > 0)
            state.put("client_secret", new CborObject.CborString(clientSecret));
        return CborObject.CborMap.build(state);
    }

    public static PaymentProperties fromCbor(Cborable cbor) {
        if (! (cbor instanceof CborObject.CborMap))
            throw new IllegalStateException("Invalid cbor for FileProperties! " + cbor);
        CborObject.CborMap m = (CborObject.CborMap) cbor;
        Optional<String> url = m.getOptional("url", c -> ((CborObject.CborString)c).value);
        Optional<String> client_secret = m.getOptional("client_secret", c -> ((CborObject.CborString) c).value);
        if (url.isPresent())
            return new PaymentProperties(url.get(), client_secret.orElse(""));
        return new PaymentProperties();
    }
}

@@ -260,16 +260,52 @@
 tSecret;
+%0A    public final long desiredQuota;
 %0A%0A    pu
@@ -413,16 +413,47 @@
 t = %22%22;%0A
+        this.desiredQuota = 0;%0A
     %7D%0A%0A 
@@ -524,16 +524,35 @@
 ntSecret
+, long desiredQuota
 ) %7B%0A    
@@ -644,32 +644,74 @@
 = clientSecret;%0A
+        this.desiredQuota = desiredQuota;%0A
     %7D%0A%0A    @JsMe
@@ -1078,24 +1078,98 @@
 reeMap%3C%3E();%0A
+        state.put(%22desiredQuota%22, new CborObject.CborLong(desiredQuota));%0A
         paym
@@ -1882,32 +1882,87 @@
 ing) c).value);%0A
+        long desiredQuota = m.getLong(%22desiredQuota%22);%0A
         if (url.
@@ -2050,16 +2050,30 @@
 Else(%22%22)
+, desiredQuota
 );%0A     

Add desired quota to PaymentProperties