package xyz.netfly.mobile.plugin;

import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.google.android.gms.internal.pal.o3;
import com.google.android.gms.measurement.internal.u1;
import com.pichillilorenzo.flutter_inappwebview_android.credential_database.URLProtectionSpaceContract;
import dd.a0;
import ed.s;
import ed.t;
import ed.u;
import io.flutter.view.p;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import kotlinx.coroutines.y;
import okhttp3.HttpUrl;
import org.drive_hunter.R;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.player.MediaSource;
import tv.danmaku.ijk.media.player.misc.ExoTrackInfo;
import tv.danmaku.ijk.media.player.misc.ITrackInfo;
import tv.danmaku.ijk.media.player.ui.IjkPlayerRuntime;
import tv.danmaku.ijk.media.player.ui.IjkVideoView;
import xyz.stream.api.entity.LiveChannelEntity;
import xyz.stream.api.q;
import xyz.stream.utils.f0;
import xyz.stream.utils.o;

/* JADX INFO: loaded from: classes3.dex */
public class m implements ad.c, s, IMediaPlayer.Listener {
    public int Q;
    public SurfaceTexture R;
    public ITrackInfo T;
    public List V;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public u f35080a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public p f35082b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Activity f35084c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public IjkVideoView f35086d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public ed.j f35088e;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public Timer f35092g;

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    public int f35093g0;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public j f35094h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public String f35096i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public String f35098j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public Integer f35100k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public String f35102l;

    /* JADX INFO: renamed from: l0, reason: collision with root package name */
    public int f35103l0;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f35104m;

    /* JADX INFO: renamed from: m0, reason: collision with root package name */
    public int f35105m0;

    /* JADX INFO: renamed from: n0, reason: collision with root package name */
    public String f35106n0;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f35090f = true;
    public final Object S = new Object();
    public boolean U = false;
    public String W = "";
    public final ArrayList X = new ArrayList();
    public boolean Y = false;
    public boolean Z = false;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public boolean f35081a0 = false;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    public boolean f35083b0 = false;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public boolean f35085c0 = false;

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    public final ExecutorService f35087d0 = Executors.newSingleThreadExecutor();

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    public int f35089e0 = 0;

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    public final Handler f35091f0 = new Handler(Looper.getMainLooper());

    /* JADX INFO: renamed from: h0, reason: collision with root package name */
    public final i f35095h0 = new i(this, 0);

    /* JADX INFO: renamed from: i0, reason: collision with root package name */
    public final xyz.netfly.ads.h f35097i0 = new xyz.netfly.ads.h(this, 5);

    /* JADX INFO: renamed from: j0, reason: collision with root package name */
    public long f35099j0 = 0;

    /* JADX INFO: renamed from: k0, reason: collision with root package name */
    public final Handler f35101k0 = new Handler(Looper.getMainLooper());

    /* JADX INFO: renamed from: o0, reason: collision with root package name */
    public final i f35107o0 = new i(this, 1);

    /* JADX INFO: renamed from: p0, reason: collision with root package name */
    public int f35108p0 = 0;

    public m(Activity activity) {
        this.f35084c = activity;
    }

    public static void a(m mVar) {
        if (mVar.f35085c0) {
            return;
        }
        mVar.f35085c0 = true;
        mVar.b();
        mVar.f35101k0.removeCallbacks(mVar.f35107o0);
        if (mVar.f35088e == null) {
            return;
        }
        HashMap mapO = u1.o("event", "liveError");
        mapO.put("errorCode", mVar.f35103l0 + "-" + mVar.f35105m0);
        mVar.f35088e.a(mapO);
    }

    public final void b() {
        j jVar = this.f35094h;
        if (jVar != null) {
            jVar.cancel();
            this.f35094h = null;
        }
    }

    public final void c() {
        this.f35089e0++;
        Handler handler = this.f35101k0;
        handler.removeCallbacksAndMessages(null);
        this.f35091f0.removeCallbacksAndMessages(null);
        IjkVideoView ijkVideoView = this.f35086d;
        if (ijkVideoView != null) {
            if (ijkVideoView.getPlaybackState() == 1) {
                xyz.netfly.api.c.b().i();
            }
            if (this.W != null) {
                q.g().b(this.W);
                this.W = null;
            }
            handler.removeCallbacks(this.f35107o0);
            this.f35086d.removeListener();
            this.f35086d.stop();
            this.f35086d.release();
        }
    }

    public final String d() {
        List list = this.V;
        if (list != null && !list.isEmpty()) {
            for (LiveChannelEntity.PlayUrlListEntity playUrlListEntity : this.V) {
                if (playUrlListEntity.getType() == 1) {
                    return playUrlListEntity.getPlayUrl();
                }
            }
        }
        return null;
    }

    public final String e() {
        List list = this.V;
        if (list == null || list.isEmpty()) {
            return this.f35096i;
        }
        for (LiveChannelEntity.PlayUrlListEntity playUrlListEntity : this.V) {
            if (playUrlListEntity.getType() == 0) {
                return playUrlListEntity.getPlayUrl();
            }
        }
        return ((LiveChannelEntity.PlayUrlListEntity) this.V.get(0)).getPlayUrl();
    }

    public final void f() {
        IjkVideoView ijkVideoView = this.f35086d;
        if (ijkVideoView == null) {
            return;
        }
        if (ijkVideoView.isPlaying()) {
            xyz.netfly.api.c.b().c();
        }
        b();
        Timer timer = this.f35092g;
        if (timer != null) {
            timer.cancel();
            this.f35092g = null;
        }
        this.f35086d.stop();
        this.f35086d.release();
        this.X.clear();
        String strD = this.Y ? d() : e();
        if (strD == null || strD.isEmpty()) {
            strD = e();
        }
        if (strD == null || strD.isEmpty()) {
            return;
        }
        this.f35098j = kotlinx.coroutines.flow.h.C();
        ed.j jVar = this.f35088e;
        if (jVar != null) {
            jVar.a(Integer.valueOf(this.f35086d.getPlaybackState()));
        }
        Timer timer2 = this.f35092g;
        if (timer2 != null) {
            timer2.cancel();
            this.f35092g = null;
        }
        Timer timer3 = new Timer();
        this.f35092g = timer3;
        timer3.schedule(new l(this, 0), 0L, 1000L);
        this.f35086d.setExoPlayer(this.R);
        this.f35086d.decode(1);
        this.f35086d.setResizeMode(f0.o().p("sp_live_video_ratio", 0));
        this.f35086d.removeListener();
        this.f35086d.addListener(this);
        this.U = false;
        int i10 = this.f35089e0 + 1;
        this.f35089e0 = i10;
        Integer num = this.f35100k;
        if (!xyz.stream.utils.e.g(strD) || num == null) {
            g(strD, strD, num, i10);
        } else {
            this.f35087d0.execute(new b7.e(this, num, strD, i10));
        }
    }

    public final void g(String str, String str2, Integer num, int i10) {
        if (this.f35086d != null && i10 == this.f35089e0 && Objects.equals(this.f35100k, num)) {
            this.W = str;
            xyz.stream.utils.h.b("initLivePlayer: " + this.W);
            String str3 = this.W;
            if (str3 == null || str3.isEmpty()) {
                xyz.stream.utils.h.c("live play url is empty: " + str2);
                return;
            }
            xyz.netfly.api.c.b().p(6, num == null ? 0 : num.intValue(), UUID.randomUUID().toString(), str2);
            HashMap map = new HashMap();
            map.put(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_PROTOCOL, String.valueOf(this.Q));
            boolean z10 = this.Z;
            String str4 = SessionDescription.SUPPORTED_SDP_VERSION;
            map.put("is_premium", z10 ? "1" : SessionDescription.SUPPORTED_SDP_VERSION);
            if (this.f35081a0) {
                str4 = "1";
            }
            map.put("is_sports", str4);
            map.put("channel_id", String.valueOf(this.f35100k));
            this.f35086d.setMediaSourceLive(new MediaSource(map, Uri.parse(this.W)));
            if (this.f35090f) {
                this.f35086d.setVolume(0.0f, 0.0f);
            } else {
                this.f35086d.setVolume(1.0f, 1.0f);
            }
            j jVar = this.f35094h;
            if (jVar != null) {
                jVar.cancel();
                this.f35094h = null;
            }
            j jVar2 = new j(this, 0);
            this.f35094h = jVar2;
            jVar2.start();
        }
    }

    @Override // ad.c
    public final void onAttachedToEngine(ad.b bVar) {
        this.f35082b = bVar.f444c;
        ed.i iVar = bVar.f443b;
        u uVar = new u(iVar, "Player");
        this.f35080a = uVar;
        uVar.setMethodCallHandler(this);
        new ed.l(iVar, "xyz.stream/player").a(new o3(this, 1));
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onAudioTrackSelect(ITrackInfo iTrackInfo) {
        super.onAudioTrackSelect(iTrackInfo);
        ed.j jVar = this.f35088e;
        if (jVar != null) {
            jVar.a(456);
        }
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onBitrateChange(int i10, long j10, long j11) {
        super.onBitrateChange(i10, j10, j11);
        this.f35099j0 = j11;
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onCompletion(IMediaPlayer iMediaPlayer) {
        this.f35103l0 = 0;
        this.f35106n0 = "onCompletion";
        synchronized (this) {
            this.f35101k0.removeCallbacks(this.f35107o0);
            this.f35101k0.postDelayed(this.f35107o0, 1000L);
        }
    }

    @Override // ad.c
    public final void onDetachedFromEngine(ad.b bVar) {
        this.f35089e0++;
        this.f35087d0.shutdownNow();
        c();
        Timer timer = this.f35092g;
        if (timer != null) {
            timer.cancel();
            this.f35092g = null;
        }
        b();
        this.f35080a.setMethodCallHandler(null);
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final boolean onError(IMediaPlayer iMediaPlayer, int i10, int i11, String str) {
        xyz.stream.utils.h.c("live play onError: " + i10 + ", extra: " + i11);
        this.f35103l0 = i10;
        this.f35105m0 = i11;
        this.f35083b0 = true;
        this.f35106n0 = str;
        if (i10 == 100333 && i11 == 100333) {
            y.a0(this.f35084c);
            return true;
        }
        synchronized (this) {
            this.f35101k0.removeCallbacks(this.f35107o0);
            this.f35101k0.postDelayed(this.f35107o0, 1000L);
        }
        return false;
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onInfo(IMediaPlayer iMediaPlayer, int i10, int i11) {
        if (i10 == 3) {
            xyz.netfly.api.c.b().r();
            b();
            ed.j jVar = this.f35088e;
            if (jVar != null) {
                jVar.a(Integer.valueOf(this.f35086d.getPlaybackState()));
            }
            this.T = this.f35086d.getSelectVideoTrackInfo();
            if (this.U) {
                return;
            } else {
                this.U = true;
            }
        }
        Handler handler = this.f35091f0;
        i iVar = this.f35095h0;
        if (i10 == 701) {
            if (this.f35093g0 == i10) {
                return;
            }
            xyz.netfly.api.c.b().q();
            this.f35093g0 = IMediaPlayer.MEDIA_INFO_BUFFERING_START;
            handler.postDelayed(iVar, 200L);
            return;
        }
        if ((i10 == 702 || i10 == 10008) && this.f35093g0 != i10) {
            xyz.netfly.api.c.b().j();
            this.f35093g0 = IMediaPlayer.MEDIA_INFO_BUFFERING_END;
            handler.removeCallbacks(iVar);
            b();
            ed.j jVar2 = this.f35088e;
            if (jVar2 != null) {
                jVar2.a(Integer.valueOf(this.f35093g0));
            }
        }
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onLoadedData(String str, long j10, int i10, int i11, int i12, String str2) {
        super.onLoadedData(str, j10, i10, i11, i12, str2);
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("width", Integer.valueOf(i11));
        linkedHashMap.put("height", Integer.valueOf(i12));
        linkedHashMap.put("bandwidth", Long.valueOf(j10));
        linkedHashMap.put("frameRate", Integer.valueOf(i10));
        linkedHashMap.put("codecs", str2);
        xyz.netfly.api.c.b().g(str, linkedHashMap);
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onLog(IMediaPlayer iMediaPlayer, String str) {
    }

    @Override // ed.s
    public final void onMethodCall(ed.q qVar, t tVar) {
        IjkVideoView ijkVideoView;
        if (qVar.f25842a.equals("exitLive")) {
            xyz.netfly.api.c.b().f();
            xyz.netfly.api.c.b().s();
        }
        if (qVar.f25842a.equals("setPlayer")) {
            synchronized (this.S) {
                try {
                    if (this.f35086d == null) {
                        IjkVideoView ijkVideoView2 = new IjkVideoView(this.f35084c);
                        this.f35086d = ijkVideoView2;
                        ijkVideoView2.setFocusable(false);
                        this.f35086d.setFocusableInTouchMode(false);
                    }
                    this.f35093g0 = -1;
                    this.Y = false;
                    this.f35083b0 = false;
                    this.f35085c0 = false;
                    this.f35103l0 = 0;
                    this.f35105m0 = 0;
                    this.f35101k0.removeCallbacksAndMessages(null);
                    this.f35091f0.removeCallbacksAndMessages(null);
                    if (this.W != null) {
                        q.g().b(this.W);
                        this.W = null;
                    }
                    this.f35096i = (String) qVar.argument(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL);
                    String str = (String) qVar.argument("urlList");
                    if (str != null && !str.isEmpty()) {
                        this.V = (List) xyz.stream.utils.m.f36162a.fromJson(str, new PlayerPlugin$2().getType());
                    }
                    Integer num = (Integer) qVar.argument(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_PROTOCOL);
                    this.Q = num == null ? 0 : num.intValue();
                    this.f35100k = (Integer) qVar.argument("channelId");
                    this.f35102l = (String) qVar.argument("channelName");
                    Boolean bool = Boolean.TRUE;
                    this.f35090f = bool.equals(qVar.argument("mute"));
                    boolean zEquals = bool.equals(qVar.argument("isIptv"));
                    this.Z = bool.equals(qVar.argument("isPremiumChannel"));
                    this.f35081a0 = bool.equals(qVar.argument("isSportsChannel"));
                    xyz.netfly.api.c cVarB = xyz.netfly.api.c.b();
                    IjkVideoView ijkVideoView3 = this.f35086d;
                    cVarB.getClass();
                    cVarB.f34905f = new WeakReference(ijkVideoView3);
                    cVarB.f34904e = zEquals;
                    io.flutter.embedding.engine.renderer.h hVarD = ((io.flutter.embedding.engine.renderer.j) this.f35082b).d();
                    this.R = hVarD.f28028b.surfaceTexture();
                    this.f35104m = 10;
                    f();
                    ((a0) tVar).success(Long.valueOf(hVarD.f28027a));
                } finally {
                }
            }
        }
        if (qVar.f25842a.equals("closePlayer")) {
            c();
            Timer timer = this.f35092g;
            if (timer != null) {
                timer.cancel();
                this.f35092g = null;
            }
            b();
            if (this.W != null) {
                q.g().b(this.W);
                this.W = null;
            }
        }
        if (qVar.f25842a.equals("getVideoTrack")) {
            if (this.f35086d == null) {
                ((a0) tVar).success(HttpUrl.PATH_SEGMENT_ENCODE_SET_URI);
                return;
            }
            ArrayList arrayList = new ArrayList();
            List<ITrackInfo> trackInfo = this.f35086d.getTrackInfo();
            if (trackInfo != null && !trackInfo.isEmpty()) {
                for (ITrackInfo iTrackInfo : trackInfo) {
                    if (iTrackInfo.getTrackType() == 2) {
                        arrayList.add(iTrackInfo);
                    }
                }
                if (arrayList.size() > 1) {
                    arrayList.add(0, new ExoTrackInfo(2, -100, -100, this.f35084c.getString(R.string.auto)));
                }
            }
            ((a0) tVar).success(xyz.stream.utils.m.a(arrayList));
        }
        if (qVar.f25842a.equals("getAudioTrack")) {
            if (this.f35086d == null) {
                ((a0) tVar).success(HttpUrl.PATH_SEGMENT_ENCODE_SET_URI);
                return;
            }
            ArrayList arrayList2 = new ArrayList();
            List<ITrackInfo> trackInfo2 = this.f35086d.getTrackInfo();
            if (trackInfo2 != null && !trackInfo2.isEmpty()) {
                for (ITrackInfo iTrackInfo2 : trackInfo2) {
                    if (iTrackInfo2.getTrackType() == 1) {
                        arrayList2.add(new ExoTrackInfo(iTrackInfo2.getTrackType(), iTrackInfo2.getIndex(), iTrackInfo2.getGroupIndex(), o.c().d(iTrackInfo2.getLanguage())));
                    }
                }
            }
            ((a0) tVar).success(xyz.stream.utils.m.a(arrayList2));
        }
        if (qVar.f25842a.equals("getSelectVideoTrack")) {
            if (f0.o().p(IjkPlayerRuntime.SP_LIVE_BITRATE, -100) == -100) {
                ((a0) tVar).success(0);
            } else {
                ((a0) tVar).success(Integer.valueOf(this.f35086d.getSelectedTrack(2) + 1));
            }
        }
        if (qVar.f25842a.equals("getSelectAudioTrack")) {
            int selectedGroupTrack = this.f35086d.getSelectedGroupTrack(1);
            int selectedTrack = this.f35086d.getSelectedTrack(1);
            HashMap map = new HashMap();
            u1.t(selectedGroupTrack, map, "groupIndex", selectedTrack, "trackIndex");
            ((a0) tVar).success(xyz.stream.utils.m.a(map));
        }
        if (qVar.f25842a.equals("selectVideoTrack")) {
            Integer num2 = (Integer) qVar.argument("groupIndex");
            Integer num3 = (Integer) qVar.argument("trackIndex");
            if (num2 == null || num3 == null) {
                return;
            } else {
                this.f35086d.selectGroupTrack(2, num2.intValue(), num3.intValue());
            }
        }
        if (qVar.f25842a.equals("selectAudioTrack")) {
            Integer num4 = (Integer) qVar.argument("groupIndex");
            Integer num5 = (Integer) qVar.argument("trackIndex");
            if (num4 == null || num5 == null) {
                return;
            } else {
                this.f35086d.selectGroupTrack(1, num4.intValue(), num5.intValue());
            }
        }
        if (qVar.f25842a.equals("setVideoRatio")) {
            Integer num6 = (Integer) qVar.argument("ratio");
            if (num6 == null) {
                return;
            }
            f0.o().v(num6.intValue(), "sp_live_video_ratio");
            this.f35086d.setResizeMode(num6.intValue());
        }
        if (qVar.f25842a.equals("getVideoRatio")) {
            ((a0) tVar).success(Integer.valueOf(f0.o().p("sp_live_video_ratio", 0)));
        }
        if (qVar.f25842a.equals("getVideoSize")) {
            HashMap map2 = new HashMap();
            IjkVideoView ijkVideoView4 = this.f35086d;
            if (ijkVideoView4 != null) {
                map2.put("width", Integer.valueOf(ijkVideoView4.getVideoWidth()));
                map2.put("height", Integer.valueOf(this.f35086d.getVideoHeight()));
            } else {
                u1.t(0, map2, "width", 0, "height");
            }
            ((a0) tVar).success(xyz.stream.utils.m.a(map2));
        }
        if (!qVar.f25842a.equals("setVolume") || (ijkVideoView = this.f35086d) == null) {
            return;
        }
        ijkVideoView.setVolume(1.0f, 1.0f);
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onPrepared(IMediaPlayer iMediaPlayer) {
        this.f35104m = 10;
        ed.j jVar = this.f35088e;
        if (jVar != null) {
            jVar.a(Integer.valueOf(this.f35086d.getPlaybackState()));
            b();
        }
        xyz.stream.utils.l.f36161a.execute(new xyz.stream.utils.i(this.f35098j, kotlinx.coroutines.flow.h.C(), this.f35100k.intValue(), this.f35102l));
    }

    @Override // tv.danmaku.ijk.media.player.IMediaPlayer.Listener
    public final void onVideoTrackSelect(ITrackInfo iTrackInfo) {
        ed.j jVar;
        super.onVideoTrackSelect(iTrackInfo);
        if (iTrackInfo == null) {
            return;
        }
        ed.j jVar2 = this.f35088e;
        if (jVar2 != null) {
            jVar2.a(123);
        }
        if (this.T == null) {
            this.T = iTrackInfo;
        }
        if (this.T.getBitrate() != iTrackInfo.getBitrate()) {
            int iP = f0.o().p(IjkPlayerRuntime.SP_LIVE_BITRATE, -100);
            if (iP != -100 && iP == iTrackInfo.getBitrate() && (jVar = this.f35088e) != null) {
                jVar.a(789);
            }
            this.T = iTrackInfo;
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            linkedHashMap.put("width", Integer.valueOf(this.T.getWidth()));
            linkedHashMap.put("height", Integer.valueOf(this.T.getHeight()));
            linkedHashMap.put("bandwidth", Integer.valueOf(this.T.getBitrate()));
            linkedHashMap.put("frameRate", Integer.valueOf(this.T.getFrameRate()));
            linkedHashMap.put("codecs", this.T.getCodecs());
            xyz.netfly.api.c.b().l(linkedHashMap);
        }
    }
}
