package com.pichillilorenzo.flutter_inappwebview_android.webview.web_message;

import a4.b;
import a4.g0;
import a4.k0;
import android.net.Uri;
import android.webkit.ValueCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.pichillilorenzo.flutter_inappwebview_android.types.Disposable;
import com.pichillilorenzo.flutter_inappwebview_android.types.WebMessageCompatExt;
import com.pichillilorenzo.flutter_inappwebview_android.types.WebMessagePort;
import com.pichillilorenzo.flutter_inappwebview_android.types.WebMessagePortCompatExt;
import com.pichillilorenzo.flutter_inappwebview_android.webview.InAppWebViewInterface;
import com.pichillilorenzo.flutter_inappwebview_android.webview.in_app_webview.InAppWebView;
import defpackage.e;
import ed.t;
import ed.u;
import java.lang.reflect.InvocationHandler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import xyz.stream.download.config.InnerConstant;
import z3.b0;
import z3.c0;
import z3.p;
import z3.q;
import z3.r;

/* JADX INFO: loaded from: classes.dex */
public class WebMessageChannel implements Disposable {
    protected static final String LOG_TAG = "WebMessageChannel";
    public static final String METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_web_message_channel_";

    @Nullable
    public WebMessageChannelChannelDelegate channelDelegate;
    public final List<r> compatPorts;

    /* JADX INFO: renamed from: id, reason: collision with root package name */
    @NonNull
    public String f24824id;
    public final List<WebMessagePort> ports;

    @Nullable
    public InAppWebViewInterface webView;

    public WebMessageChannel(@NonNull String str, @NonNull InAppWebViewInterface inAppWebViewInterface) {
        r[] rVarArrPortsToCompat;
        this.f24824id = str;
        this.channelDelegate = new WebMessageChannelChannelDelegate(this, new u(inAppWebViewInterface.getPlugin().messenger, e.i(METHOD_CHANNEL_NAME_PREFIX, str)));
        if (inAppWebViewInterface instanceof InAppWebView) {
            InAppWebView inAppWebView = (InAppWebView) inAppWebViewInterface;
            Uri uri = b0.f37032a;
            b bVar = k0.f340w;
            if (bVar.a()) {
                rVarArrPortsToCompat = g0.portsToCompat(inAppWebView.createWebMessageChannel());
            } else {
                if (!bVar.b()) {
                    throw k0.a();
                }
                b0.c(inAppWebView);
                InvocationHandler[] invocationHandlerArrCreateWebMessageChannel = b0.e(inAppWebView).f346a.createWebMessageChannel();
                r[] rVarArr = new r[invocationHandlerArrCreateWebMessageChannel.length];
                for (int i10 = 0; i10 < invocationHandlerArrCreateWebMessageChannel.length; i10++) {
                    rVarArr[i10] = new g0(invocationHandlerArrCreateWebMessageChannel[i10]);
                }
                rVarArrPortsToCompat = rVarArr;
            }
            this.compatPorts = new ArrayList(Arrays.asList(rVarArrPortsToCompat));
            this.ports = new ArrayList();
        } else {
            this.ports = Arrays.asList(new WebMessagePort("port1", this), new WebMessagePort("port2", this));
            this.compatPorts = new ArrayList();
        }
        this.webView = inAppWebViewInterface;
    }

    public void closeForInAppWebView(Integer num, @NonNull t tVar) {
        if (this.webView == null || this.compatPorts.size() <= 0 || !c0.a("WEB_MESSAGE_PORT_CLOSE")) {
            tVar.success(Boolean.TRUE);
            return;
        }
        try {
            this.compatPorts.get(num.intValue()).a();
            tVar.success(Boolean.TRUE);
        } catch (Exception e10) {
            tVar.error(LOG_TAG, e10.getMessage(), null);
        }
    }

    @Override // com.pichillilorenzo.flutter_inappwebview_android.types.Disposable
    public void dispose() {
        if (c0.a("WEB_MESSAGE_PORT_CLOSE")) {
            Iterator<r> it = this.compatPorts.iterator();
            while (it.hasNext()) {
                try {
                    it.next().a();
                } catch (Exception unused) {
                }
            }
        }
        WebMessageChannelChannelDelegate webMessageChannelChannelDelegate = this.channelDelegate;
        if (webMessageChannelChannelDelegate != null) {
            webMessageChannelChannelDelegate.dispose();
            this.channelDelegate = null;
        }
        this.compatPorts.clear();
        this.webView = null;
    }

    public void initJsInstance(InAppWebViewInterface inAppWebViewInterface, final ValueCallback<WebMessageChannel> valueCallback) {
        if (inAppWebViewInterface != null) {
            inAppWebViewInterface.evaluateJavascript(e.o(new StringBuilder("(function() {window.flutter_inappwebview._webMessageChannels['"), this.f24824id, "'] = new MessageChannel();})();"), null, new ValueCallback<String>() { // from class: com.pichillilorenzo.flutter_inappwebview_android.webview.web_message.WebMessageChannel.1
                @Override // android.webkit.ValueCallback
                public void onReceiveValue(String str) {
                    valueCallback.onReceiveValue(this);
                }
            });
        } else {
            valueCallback.onReceiveValue(this);
        }
    }

    public void onMessage(int i10, @Nullable WebMessageCompatExt webMessageCompatExt) {
        WebMessageChannelChannelDelegate webMessageChannelChannelDelegate = this.channelDelegate;
        if (webMessageChannelChannelDelegate != null) {
            webMessageChannelChannelDelegate.onMessage(i10, webMessageCompatExt);
        }
    }

    public void postMessageForInAppWebView(Integer num, @NonNull WebMessageCompatExt webMessageCompatExt, @NonNull t tVar) {
        if (this.webView == null || this.compatPorts.size() <= 0 || !c0.a("WEB_MESSAGE_PORT_POST_MESSAGE")) {
            tVar.success(Boolean.TRUE);
            return;
        }
        r rVar = this.compatPorts.get(num.intValue());
        ArrayList arrayList = new ArrayList();
        List<WebMessagePortCompatExt> ports = webMessageCompatExt.getPorts();
        if (ports != null) {
            for (WebMessagePortCompatExt webMessagePortCompatExt : ports) {
                WebMessageChannel webMessageChannel = this.webView.getWebMessageChannels().get(webMessagePortCompatExt.getWebMessageChannelId());
                if (webMessageChannel != null) {
                    arrayList.add(webMessageChannel.compatPorts.get(webMessagePortCompatExt.getIndex()));
                }
            }
        }
        Object data = webMessageCompatExt.getData();
        try {
            if (c0.a("WEB_MESSAGE_ARRAY_BUFFER") && data != null && webMessageCompatExt.getType() == 1) {
                rVar.b(new p((byte[]) data, (r[]) arrayList.toArray(new r[0])));
            } else {
                rVar.b(new p(data != null ? data.toString() : null, (r[]) arrayList.toArray(new r[0])));
            }
            tVar.success(Boolean.TRUE);
        } catch (Exception e10) {
            tVar.error(LOG_TAG, e10.getMessage(), null);
        }
    }

    public void setWebMessageCallbackForInAppWebView(final int i10, @NonNull t tVar) {
        if (this.webView == null || this.compatPorts.size() <= 0 || !c0.a("WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK")) {
            tVar.success(Boolean.TRUE);
            return;
        }
        try {
            this.compatPorts.get(i10).c(new q() { // from class: com.pichillilorenzo.flutter_inappwebview_android.webview.web_message.WebMessageChannel.2
                @Override // z3.q
                public void onMessage(@NonNull r rVar, @Nullable p pVar) {
                    super.onMessage(rVar, pVar);
                    this.onMessage(i10, pVar != null ? WebMessageCompatExt.fromMapWebMessageCompat(pVar) : null);
                }
            });
            tVar.success(Boolean.TRUE);
        } catch (Exception e10) {
            tVar.error(LOG_TAG, e10.getMessage(), null);
        }
    }

    public Map<String, Object> toMap() {
        HashMap map = new HashMap();
        map.put(InnerConstant.Db.f36069id, this.f24824id);
        return map;
    }
}
