package com.pichillilorenzo.flutter_inappwebview_android;

import a4.k0;
import a4.l0;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.google.android.gms.measurement.internal.u1;
import com.pichillilorenzo.flutter_inappwebview_android.types.ChannelDelegateImpl;
import defpackage.e;
import ed.q;
import ed.t;
import ed.u;
import ee.b;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.chromium.support_lib_boundary.WebViewCookieManagerBoundaryInterface;
import org.chromium.support_lib_boundary.WebkitToCompatConverterBoundaryInterface;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import u.a;
import z3.c0;

/* JADX INFO: loaded from: classes.dex */
public class MyCookieManager extends ChannelDelegateImpl {
    protected static final String LOG_TAG = "MyCookieManager";
    public static final String METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_cookiemanager";

    @Nullable
    public static CookieManager cookieManager;

    @Nullable
    public InAppWebViewFlutterPlugin plugin;

    public MyCookieManager(@NonNull InAppWebViewFlutterPlugin inAppWebViewFlutterPlugin) {
        super(new u(inAppWebViewFlutterPlugin.messenger, METHOD_CHANNEL_NAME));
        this.plugin = inAppWebViewFlutterPlugin;
    }

    public static String getCookieExpirationDate(Long l10) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        return simpleDateFormat.format(new Date(l10.longValue()));
    }

    @Nullable
    private static CookieManager getCookieManager() throws Exception {
        if (cookieManager == null) {
            try {
                cookieManager = CookieManager.getInstance();
            } catch (IllegalArgumentException unused) {
                return null;
            } catch (Exception e10) {
                if (e10.getMessage() == null || !e10.getClass().getCanonicalName().equals("android.webkit.WebViewFactory.MissingWebViewPackageException")) {
                    throw e10;
                }
                return null;
            }
        }
        return cookieManager;
    }

    public static void init() {
        if (cookieManager == null) {
            cookieManager = getCookieManager();
        }
    }

    public void deleteAllCookies(final t tVar) throws Exception {
        CookieManager cookieManager2 = getCookieManager();
        cookieManager = cookieManager2;
        if (cookieManager2 == null) {
            tVar.success(Boolean.FALSE);
        } else {
            cookieManager.removeAllCookies(new ValueCallback<Boolean>() { // from class: com.pichillilorenzo.flutter_inappwebview_android.MyCookieManager.3
                @Override // android.webkit.ValueCallback
                public void onReceiveValue(Boolean bool) {
                    tVar.success(bool);
                }
            });
            cookieManager.flush();
        }
    }

    public void deleteCookie(String str, String str2, String str3, String str4, final t tVar) throws Exception {
        CookieManager cookieManager2 = getCookieManager();
        cookieManager = cookieManager2;
        if (cookieManager2 == null) {
            tVar.success(Boolean.FALSE);
            return;
        }
        String strJ = str2 + "=; Path=" + str4 + "; Max-Age=-1";
        if (str3 != null) {
            strJ = e.j(strJ, "; Domain=", str3);
        }
        cookieManager.setCookie(str, u1.j(strJ, ";"), new ValueCallback<Boolean>() { // from class: com.pichillilorenzo.flutter_inappwebview_android.MyCookieManager.2
            @Override // android.webkit.ValueCallback
            public void onReceiveValue(Boolean bool) {
                tVar.success(bool);
            }
        });
        cookieManager.flush();
    }

    public void deleteCookies(String str, String str2, String str3, t tVar) throws Exception {
        CookieManager cookieManager2 = getCookieManager();
        cookieManager = cookieManager2;
        if (cookieManager2 == null) {
            tVar.success(Boolean.FALSE);
            return;
        }
        String cookie = cookieManager2.getCookie(str);
        if (cookie != null) {
            for (String str4 : cookie.split(";")) {
                String strJ = str4.split("=", 2)[0].trim() + "=; Path=" + str3 + "; Max-Age=-1";
                if (str2 != null) {
                    strJ = e.j(strJ, "; Domain=", str2);
                }
                cookieManager.setCookie(str, u1.j(strJ, ";"), null);
            }
            cookieManager.flush();
        }
        tVar.success(Boolean.TRUE);
    }

    @Override // com.pichillilorenzo.flutter_inappwebview_android.types.ChannelDelegateImpl, com.pichillilorenzo.flutter_inappwebview_android.types.Disposable
    public void dispose() {
        super.dispose();
        this.plugin = null;
    }

    public List<Map<String, Object>> getCookies(String str) throws Exception {
        ArrayList arrayList = new ArrayList();
        CookieManager cookieManager2 = getCookieManager();
        cookieManager = cookieManager2;
        if (cookieManager2 == null) {
            return arrayList;
        }
        List<String> arrayList2 = new ArrayList<>();
        String str2 = "GET_COOKIE_INFO";
        if (c0.a("GET_COOKIE_INFO")) {
            CookieManager cookieManager3 = cookieManager;
            if (!k0.Q.b()) {
                throw k0.a();
            }
            arrayList2 = ((WebViewCookieManagerBoundaryInterface) b.castToSuppLibClass(WebViewCookieManagerBoundaryInterface.class, ((WebkitToCompatConverterBoundaryInterface) l0.f344a.f31183b).convertCookieManager(cookieManager3))).getCookieInfo(str);
        } else {
            String cookie = cookieManager.getCookie(str);
            if (cookie != null) {
                arrayList2 = Arrays.asList(cookie.split(";"));
            }
        }
        Iterator<String> it = arrayList2.iterator();
        while (it.hasNext()) {
            String[] strArrSplit = it.next().split(";");
            if (strArrSplit.length != 0) {
                String[] strArrSplit2 = strArrSplit[0].split("=", 2);
                HashMap mapU = e.u("name", strArrSplit2[0].trim(), "value", strArrSplit2.length > 1 ? strArrSplit2[1].trim() : "");
                mapU.put("expiresDate", null);
                mapU.put("isSessionOnly", null);
                mapU.put("domain", null);
                mapU.put("sameSite", null);
                mapU.put("isSecure", null);
                mapU.put("isHttpOnly", null);
                mapU.put("path", null);
                if (c0.a(str2)) {
                    Boolean bool = Boolean.FALSE;
                    mapU.put("isSecure", bool);
                    mapU.put("isHttpOnly", bool);
                    int i10 = 1;
                    while (i10 < strArrSplit.length) {
                        Iterator<String> it2 = it;
                        String[] strArrSplit3 = strArrSplit[i10].split("=", 2);
                        String strTrim = strArrSplit3[0].trim();
                        String[] strArr = strArrSplit;
                        String str3 = str2;
                        String strTrim2 = strArrSplit3.length > 1 ? strArrSplit3[1].trim() : "";
                        if (strTrim.equalsIgnoreCase(RtspHeaders.EXPIRES)) {
                            try {
                                Date date = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US).parse(strTrim2);
                                if (date != null) {
                                    mapU.put("expiresDate", Long.valueOf(date.getTime()));
                                }
                            } catch (NumberFormatException | ParseException unused) {
                            }
                        } else if (strTrim.equalsIgnoreCase("Max-Age")) {
                            mapU.put("expiresDate", Long.valueOf(System.currentTimeMillis() + Long.parseLong(strTrim2)));
                        } else if (strTrim.equalsIgnoreCase("Domain")) {
                            mapU.put("domain", strTrim2);
                        } else if (strTrim.equalsIgnoreCase("SameSite")) {
                            mapU.put("sameSite", strTrim2);
                        } else if (strTrim.equalsIgnoreCase("Secure")) {
                            mapU.put("isSecure", Boolean.TRUE);
                        } else if (strTrim.equalsIgnoreCase("HttpOnly")) {
                            mapU.put("isHttpOnly", Boolean.TRUE);
                        } else if (strTrim.equalsIgnoreCase("Path")) {
                            mapU.put("path", strTrim2);
                        }
                        i10++;
                        strArrSplit = strArr;
                        it = it2;
                        str2 = str3;
                    }
                }
                arrayList.add(mapU);
                it = it;
                str2 = str2;
            }
        }
        return arrayList;
    }

    @Override // com.pichillilorenzo.flutter_inappwebview_android.types.ChannelDelegateImpl, com.pichillilorenzo.flutter_inappwebview_android.types.IChannelDelegate, ed.s
    public void onMethodCall(@NonNull q qVar, @NonNull t tVar) throws Exception {
        init();
        String str = qVar.f25842a;
        str.getClass();
        switch (str) {
            case "deleteCookie":
                deleteCookie((String) qVar.argument(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL), (String) qVar.argument("name"), (String) qVar.argument("domain"), (String) qVar.argument("path"), tVar);
                break;
            case "removeSessionCookies":
                removeSessionCookies(tVar);
                break;
            case "setCookie":
                String str2 = (String) qVar.argument(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL);
                String str3 = (String) qVar.argument("name");
                String str4 = (String) qVar.argument("value");
                String str5 = (String) qVar.argument("domain");
                String str6 = (String) qVar.argument("path");
                String str7 = (String) qVar.argument("expiresDate");
                setCookie(str2, str3, str4, str5, str6, str7 != null ? new Long(str7) : null, (Integer) qVar.argument("maxAge"), (Boolean) qVar.argument("isSecure"), (Boolean) qVar.argument("isHttpOnly"), (String) qVar.argument("sameSite"), tVar);
                break;
            case "deleteCookies":
                deleteCookies((String) qVar.argument(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL), (String) qVar.argument("domain"), (String) qVar.argument("path"), tVar);
                break;
            case "deleteAllCookies":
                deleteAllCookies(tVar);
                break;
            case "getCookies":
                tVar.success(getCookies((String) qVar.argument(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL)));
                break;
            default:
                tVar.notImplemented();
                break;
        }
    }

    public void removeSessionCookies(final t tVar) throws Exception {
        CookieManager cookieManager2 = getCookieManager();
        cookieManager = cookieManager2;
        if (cookieManager2 == null) {
            tVar.success(Boolean.FALSE);
        } else {
            cookieManager.removeSessionCookies(new ValueCallback<Boolean>() { // from class: com.pichillilorenzo.flutter_inappwebview_android.MyCookieManager.4
                @Override // android.webkit.ValueCallback
                public void onReceiveValue(Boolean bool) {
                    tVar.success(bool);
                }
            });
            cookieManager.flush();
        }
    }

    public void setCookie(String str, String str2, String str3, String str4, String str5, Long l10, Integer num, Boolean bool, Boolean bool2, String str6, final t tVar) throws Exception {
        CookieManager cookieManager2 = getCookieManager();
        cookieManager = cookieManager2;
        if (cookieManager2 == null) {
            tVar.success(Boolean.FALSE);
            return;
        }
        String strJ = str2 + "=" + str3 + "; Path=" + str5;
        if (str4 != null) {
            strJ = e.j(strJ, "; Domain=", str4);
        }
        if (l10 != null) {
            StringBuilder sbD = a.d(strJ, "; Expires=");
            sbD.append(getCookieExpirationDate(l10));
            strJ = sbD.toString();
        }
        if (num != null) {
            StringBuilder sbD2 = a.d(strJ, "; Max-Age=");
            sbD2.append(num.toString());
            strJ = sbD2.toString();
        }
        if (bool != null && bool.booleanValue()) {
            strJ = u1.j(strJ, "; Secure");
        }
        if (bool2 != null && bool2.booleanValue()) {
            strJ = u1.j(strJ, "; HttpOnly");
        }
        if (str6 != null) {
            strJ = e.j(strJ, "; SameSite=", str6);
        }
        cookieManager.setCookie(str, u1.j(strJ, ";"), new ValueCallback<Boolean>() { // from class: com.pichillilorenzo.flutter_inappwebview_android.MyCookieManager.1
            @Override // android.webkit.ValueCallback
            public void onReceiveValue(Boolean bool3) {
                tVar.success(bool3);
            }
        });
        cookieManager.flush();
    }
}
