package com.pichillilorenzo.flutter_inappwebview_android.credential_database;

import android.content.Context;
import android.webkit.WebViewDatabase;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.pichillilorenzo.flutter_inappwebview_android.InAppWebViewFlutterPlugin;
import com.pichillilorenzo.flutter_inappwebview_android.credential_database.URLCredentialContract;
import com.pichillilorenzo.flutter_inappwebview_android.credential_database.URLProtectionSpaceContract;
import com.pichillilorenzo.flutter_inappwebview_android.types.ChannelDelegateImpl;
import com.pichillilorenzo.flutter_inappwebview_android.types.URLCredential;
import com.pichillilorenzo.flutter_inappwebview_android.types.URLProtectionSpace;
import ed.q;
import ed.t;
import ed.u;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/* JADX INFO: loaded from: classes.dex */
@RequiresApi(api = 26)
public class CredentialDatabaseHandler extends ChannelDelegateImpl {
    protected static final String LOG_TAG = "CredentialDatabaseHandler";
    public static final String METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_credential_database";

    @Nullable
    public static CredentialDatabase credentialDatabase;

    @Nullable
    public InAppWebViewFlutterPlugin plugin;

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

    public static void init(@NonNull InAppWebViewFlutterPlugin inAppWebViewFlutterPlugin) {
        if (credentialDatabase == null) {
            credentialDatabase = CredentialDatabase.getInstance(inAppWebViewFlutterPlugin.applicationContext);
        }
    }

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

    @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) {
        Context context;
        InAppWebViewFlutterPlugin inAppWebViewFlutterPlugin = this.plugin;
        if (inAppWebViewFlutterPlugin != null) {
            init(inAppWebViewFlutterPlugin);
        }
        String str = qVar.f25842a;
        str.getClass();
        switch (str) {
            case "clearAllAuthCredentials":
                CredentialDatabase credentialDatabase2 = credentialDatabase;
                if (credentialDatabase2 != null) {
                    credentialDatabase2.clearAllAuthCredentials();
                    InAppWebViewFlutterPlugin inAppWebViewFlutterPlugin2 = this.plugin;
                    if (inAppWebViewFlutterPlugin2 != null && (context = inAppWebViewFlutterPlugin2.applicationContext) != null) {
                        WebViewDatabase.getInstance(context).clearHttpAuthUsernamePassword();
                    }
                    tVar.success(Boolean.TRUE);
                    break;
                } else {
                    tVar.success(Boolean.FALSE);
                    break;
                }
                break;
            case "getHttpAuthCredentials":
                ArrayList arrayList = new ArrayList();
                if (credentialDatabase != null) {
                    Iterator<URLCredential> it = credentialDatabase.getHttpAuthCredentials((String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_HOST), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_PROTOCOL), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_REALM), (Integer) qVar.argument("port")).iterator();
                    while (it.hasNext()) {
                        arrayList.add(it.next().toMap());
                    }
                }
                tVar.success(arrayList);
                break;
            case "removeHttpAuthCredential":
                if (credentialDatabase != null) {
                    credentialDatabase.removeHttpAuthCredential((String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_HOST), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_PROTOCOL), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_REALM), (Integer) qVar.argument("port"), (String) qVar.argument(URLCredentialContract.FeedEntry.COLUMN_NAME_USERNAME), (String) qVar.argument(URLCredentialContract.FeedEntry.COLUMN_NAME_PASSWORD));
                    tVar.success(Boolean.TRUE);
                    break;
                } else {
                    tVar.success(Boolean.FALSE);
                    break;
                }
                break;
            case "setHttpAuthCredential":
                if (credentialDatabase != null) {
                    credentialDatabase.setHttpAuthCredential((String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_HOST), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_PROTOCOL), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_REALM), (Integer) qVar.argument("port"), (String) qVar.argument(URLCredentialContract.FeedEntry.COLUMN_NAME_USERNAME), (String) qVar.argument(URLCredentialContract.FeedEntry.COLUMN_NAME_PASSWORD));
                    tVar.success(Boolean.TRUE);
                    break;
                } else {
                    tVar.success(Boolean.FALSE);
                    break;
                }
                break;
            case "removeHttpAuthCredentials":
                if (credentialDatabase != null) {
                    credentialDatabase.removeHttpAuthCredentials((String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_HOST), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_PROTOCOL), (String) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_REALM), (Integer) qVar.argument("port"));
                    tVar.success(Boolean.TRUE);
                    break;
                } else {
                    tVar.success(Boolean.FALSE);
                    break;
                }
                break;
            case "getAllAuthCredentials":
                ArrayList arrayList2 = new ArrayList();
                CredentialDatabase credentialDatabase3 = credentialDatabase;
                if (credentialDatabase3 != null) {
                    for (URLProtectionSpace uRLProtectionSpace : credentialDatabase3.protectionSpaceDao.getAll()) {
                        ArrayList arrayList3 = new ArrayList();
                        Iterator<URLCredential> it2 = credentialDatabase.credentialDao.getAllByProtectionSpaceId(uRLProtectionSpace.getId()).iterator();
                        while (it2.hasNext()) {
                            arrayList3.add(it2.next().toMap());
                        }
                        HashMap map = new HashMap();
                        map.put("protectionSpace", uRLProtectionSpace.toMap());
                        map.put("credentials", arrayList3);
                        arrayList2.add(map);
                    }
                }
                tVar.success(arrayList2);
                break;
            default:
                tVar.notImplemented();
                break;
        }
    }
}
