package com.psykar.cookiemanager;

import com.facebook.react.modules.network.ForwardingCookieHandler;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.Callback;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CookieManagerModule extends ReactContextBaseJavaModule {

    private ForwardingCookieHandler cookieHandler;

    public CookieManagerModule(ReactApplicationContext context) {
        super(context);
        this.cookieHandler = new ForwardingCookieHandler(context);
    }

    public String getName() {
        return "RNCookieManagerAndroid";
    }

    @ReactMethod
    public void set(ReadableMap cookie, final Callback callback) throws Exception {
        throw new Exception("Cannot call on android, try setFromResponse");
    }

    @ReactMethod
    public void setFromResponse(String url, String value, final Callback callback) throws URISyntaxException, IOException {
        Map headers = new HashMap<String, List<String>>();
        // Pretend this is a header
        headers.put("Set-cookie", Collections.singletonList(value));
        URI uri = new URI(url);
        this.cookieHandler.put(uri, headers);
        callback.invoke(true);
    }

    @ReactMethod
    public void getAll(Callback callback) throws Exception {
        throw new Exception("Cannot get all cookies on android, try getCookieHeader(url)");
    }

    @ReactMethod
    public void get(String url, Callback callback) throws URISyntaxException, IOException {
        URI uri = new URI(url);

        Map<String, List<String>> cookieMap = this.cookieHandler.get(uri, new HashMap());
        // If only the variables were public
        List<String> cookieList = cookieMap.get("Cookie");
        if (cookieList != null) {
            callback.invoke(cookieList.get(0));
        } else {
            callback.invoke(cookieList);
        }
    }

    @ReactMethod
    public void clearAll(final Callback callback) {
        this.cookieHandler.clearCookies(callback);
    }
}

@@ -348,16 +348,106 @@
 allback;
+%0Aimport com.facebook.react.bridge.Arguments;%0Aimport com.facebook.react.bridge.WritableMap;
 %0A%0Aimport
@@ -1588,12 +1588,18 @@
 oke(
-true
+null, null
 );%0A 
@@ -2113,24 +2113,73 @@
 (%22Cookie%22);%0A
+        WritableMap map = Arguments.createMap();%0A
         if (
@@ -2212,32 +2212,35 @@
         
-callback.invoke(
+String%5B%5D cookies = 
 cookieLi
@@ -2248,16 +2248,26 @@
 t.get(0)
+.split(%22;%22
 );%0A     
@@ -2273,55 +2273,162 @@
     
-%7D else %7B%0A            callback.invoke(
+    for (int i = 0; i %3C cookies.length; i++) %7B%0A                String%5B%5D cookie = cookies%5Bi%5D.split(%22=%22);%0A                map.putString(cookie%5B0%5D, 
 cookie
-List
+%5B1%5D
 );%0A 
@@ -2434,17 +2434,67 @@
         
-%7D
+    %7D%0A        %7D%0A        callback.invoke(null, map);
 %0A    %7D%0A%0A
@@ -2602,24 +2602,150 @@
 Cookies(
-callback
+new Callback() %7B%0A            public void invoke(Object... args) %7B%0A                callback.invoke(null, null);%0A            %7D%0A        %7D
 );%0A    %7D

Fix android callback types