package xyz.stream.api;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.text.TextUtils;
import androidx.media3.common.C;
import androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import com.google.android.gms.measurement.internal.u1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import tv.danmaku.ijk.media.player.IjkMediaMeta;
import xyz.stream.api.database.entity.RecordEntity;
import xyz.stream.utils.entity.CheckResultEntity;
import xyz.stream.utils.f0;

/* JADX INFO: loaded from: classes3.dex */
public final class o {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static o f35913d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final HashMap f35914e;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final String f35915a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f35916b;

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

    static {
        HashMap map = new HashMap();
        f35914e = map;
        map.put("email/reg", "psk_app_nf_reg");
        map.put("email/reset_password", "psk_app_nf_rstpw");
        map.put("app/check", "psk_app_nf_check");
        map.put("app/code", "psk_app_nf_code");
        map.put("app/active", "psk_app_nf_active");
        map.put("search", "psk_app_nf_search");
        map.put("live/channels/play", "psk_app_nf_c_play");
        map.put("play", "psk_app_nf_play");
        map.put("user/device/login", "psk_app_nf_login");
        map.put("user/login/sn", "psk_app_nf_sn");
        map.put("app/device/token", "psk_app_nf_token");
        map.put("email/login", "psk_app_nf_e_login");
        map.put("email/send", "psk_app_nf_e_send");
        map.put("live/channels/hot", "psk_app_nf_c_hot");
        map.put("translation/subtitle", "psk_app_nf_subtitle");
        map.put("translation/task/progress", "psk_app_nf_progress");
        map.put("user/password/lost", "psk_app_nf_lost");
        map.put("live/categories/list", "psk_app_nf_list");
        map.put("live/channels/list", "psk_app_nf_c_list");
        map.put("live/channels/epgs", "psk_app_nf_c_epgs");
        map.put("cms/subtitle", "psk_app_nf_cms_stt");
        map.put("live/channels/properties", "psk_app_nf_prop");
        map.put("video/search", "psk_app_nf_v_search");
        map.put("video/list", "psk_app_nf_v_list");
        map.put("video/info", "psk_app_nf_v_info");
        map.put("video/random", "psk_app_nf_v_random");
        map.put("video/top", "psk_app_nf_v_top");
        map.put("config/key", "psk_app_nf_v_key");
        map.put("video/tags", "psk_app_nf_v_tags");
        map.put("support/config", "psk_app_nf_s_config");
        map.put("video/episodes", "psk_app_nf_v_episodes");
        map.put("video/episodes/detail", "psk_app_nf_ve_detail");
        map.put("video/episodes/list", "psk_app_nf_ve_list");
        map.put("video/episodes/sources", "psk_app_nf_ve_sources");
        map.put("app/info", "psk_app_nf_info");
        map.put("collection/list", "psk_app_nf_l_list");
        map.put("collection/media/list", "psk_app_nf_l_mlist");
        map.put("user/order/status", "psk_app_nf_u_status");
        map.put("people/search", "psk_app_nf_p_search");
        map.put("live-collection/banner", "psk_app_nf_live_c_banner");
        map.put("live/category", "psk_app_nf_live_category");
        map.put("live/channels", "psk_app_nf_live_channels");
        map.put("live-collection/list", "psk_app_nf_live_c_list");
        map.put("live-collection/channel/list", "psk_app_nf_live_c_c_list");
        map.put("collection/live/list", "psk_app_nf_c_live_list");
        map.put("user/lock", "psk_app_nf_u_lock");
        map.put("user/lock/set", "psk_app_nf_u_lock_set");
        map.put("user/send/email", "psk_app_nf_u_send_email");
        map.put("live/channels/adult", "psk_app_nf_live_c_adult");
        map.put("users/url", "psk_app_nf_u_url");
        map.put("sports/football/leagues/matchs", "psk_app_nf_fo_leagues_matchs");
        map.put("sports/football/leagues", "psk_app_nf_fo_leagues");
        map.put("app/latest", "psk_app_nf_latest");
        map.put("user/watchers", "psk_app_nf_watchers");
        map.put("user/play/history", "psk_app_nf_p_history");
        map.put("user/play/history/detail", "psk_app_nf_p_history_detail");
        map.put("user/play/history/del", "psk_app_nf_p_history_del");
        map.put("user/dislike", "psk_app_nf_dislike");
        map.put("user/live_channel/history", "psk_app_nf_lc_history");
        map.put("user/live_channel/history/del", "psk_app_nf_lc_history_del");
        map.put("user/live_channel/favorite", "psk_app_nf_lc_favorite");
        map.put("user/live_channel/favorite/del", "psk_app_nf_lc_favorite_del");
        map.put("user/favorite", "psk_app_nf_favorite");
        map.put("user/favorite/del", "psk_app_nf_favorite_del");
    }

    public o(String str, String str2, String str3) {
        this.f35915a = str;
        this.f35916b = str2;
        this.f35917c = str3;
    }

    public static boolean D() {
        CheckResultEntity checkResultEntityC = xyz.stream.utils.e.d().c();
        return checkResultEntityC == null || checkResultEntityC.getIsPremium() == null || checkResultEntityC.getIsPremium().intValue() != 1;
    }

    public static void a(o oVar, String str, e eVar, Exception exc, String str2) {
        oVar.getClass();
        try {
            xyz.stream.utils.h.c("API request failed: " + str.replace("", "") + ", error: " + exc.getMessage() + ", params: " + str2);
            if (eVar != null) {
                eVar.a(exc.getMessage());
            }
            if (exc.getMessage() == null || !exc.getMessage().equals("restart app")) {
                return;
            }
            xyz.stream.utils.h.b("Restarting app due to API error");
            xyz.stream.utils.e.d().i();
        } catch (Exception e10) {
            u.a.g(e10, new StringBuilder("Error handling exception: "));
        }
    }

    public static void b(o oVar, String str, int i10, String str2, String str3) {
        oVar.getClass();
        xyz.stream.utils.h.c("API Error Code: " + str.replace("", "") + ", error code: " + i10 + ", message: " + str2 + ", params: " + str3);
    }

    public static o f() {
        o oVar = f35913d;
        if (oVar != null) {
            return oVar;
        }
        xyz.stream.utils.h.b("RemoteApiHelper instance is null, re-initializing");
        xyz.stream.utils.e.d().i();
        return new o("", "", "");
    }

    public static void g(String str, HashMap map, HashMap map2, xyz.stream.utils.y yVar) {
        q qVarG = q.g();
        String strP = p(str);
        qVarG.getClass();
        if (map2 != null) {
            StringBuilder sb = new StringBuilder(u1.j(strP, "?"));
            for (Map.Entry entry : map2.entrySet()) {
                sb.append((String) entry.getKey());
                sb.append("=");
                sb.append((String) entry.getValue());
                sb.append("&");
            }
            strP = u1.h(sb.toString(), 1, 0);
        }
        StringBuilder sb2 = new StringBuilder("");
        for (Map.Entry entry2 : map.entrySet()) {
            sb2.append((String) entry2.getKey());
            sb2.append("=");
            sb2.append((String) entry2.getValue());
            sb2.append(";");
        }
        String string = sb2.toString();
        if (string.endsWith(";")) {
            string = u1.h(string, 1, 0);
        }
        qVarG.d("GET", string, strP, null, yVar);
    }

    public static String p(String str) {
        for (Map.Entry entry : f35914e.entrySet()) {
            if (str.equals("" + ((String) entry.getKey()))) {
                return (String) entry.getValue();
            }
        }
        return str;
    }

    public static void v(String str, HashMap map, String str2, xyz.stream.utils.y yVar) {
        q qVarG = q.g();
        String strP = p(str);
        qVarG.getClass();
        StringBuilder sb = new StringBuilder("");
        for (Map.Entry entry : map.entrySet()) {
            sb.append((String) entry.getKey());
            sb.append("=");
            sb.append((String) entry.getValue());
            sb.append(";");
        }
        String string = sb.toString();
        if (string.endsWith(";")) {
            string = u1.h(string, 1, 0);
        }
        qVarG.d(HlsMediaPlaylist.Interstitial.CUE_TRIGGER_POST, string, strP, str2, yVar);
    }

    public final void A(int i10, int i11) {
        HashMap map = new HashMap();
        u1.t(i10, map, "tmdb_id", i11, "type");
        v("search", n(), xyz.stream.utils.m.a(map), new e.y(this));
    }

    public final void B() {
        if (xyz.stream.utils.e.h()) {
            new Thread(new p000if.e(this, 17)).start();
        }
    }

    public final void C(String str, e eVar) {
        HashMap map = new HashMap();
        map.put("q", str.trim());
        g("video/search", n(), map, new j(this, eVar, "video/search", map, 9));
    }

    public final void c() {
        if (xyz.stream.utils.e.h()) {
            g("app/check", n(), null, new k(this, new xyz.netfly.player.i(this, 3), "app/check", 4));
        }
    }

    public final void d(boolean z10, Integer num) {
        if (D()) {
            return;
        }
        String strQ = f0.o().q("flutter.selectRoles", "");
        HashMap map = new HashMap();
        if (z10) {
            map.put("is_all", Boolean.TRUE);
            map.put("role_id", strQ);
        } else {
            map.put("is_all", Boolean.FALSE);
            map.put("channel_id", num);
            map.put("role_id", strQ);
        }
        v("user/live_channel/history/del", n(), xyz.stream.utils.m.a(map), new n(this, "user/live_channel/history/del", map, 6));
    }

    public final void e(boolean z10, RecordEntity recordEntity, Integer num) {
        if (D()) {
            return;
        }
        HashMap map = new HashMap();
        if (z10) {
            map.put("is_all", Boolean.TRUE);
            if (num != null && num.intValue() > 0) {
                map.put("source_type", num);
            }
            map.put("role_id", f0.o().q("flutter.selectRoles", ""));
        } else {
            if (recordEntity == null) {
                return;
            }
            map.put("is_all", Boolean.FALSE);
            map.put("video_id", recordEntity.getVideoId());
            map.put("series_id", recordEntity.getSeriesId());
            map.put("season_number", recordEntity.getSeasonNumber());
            map.put("episode_number", recordEntity.getEpisodeNumber());
            map.put("role_id", recordEntity.getRoleId());
        }
        v("user/play/history/del", n(), xyz.stream.utils.m.a(map), new n(this, "user/play/history/del", map, 4));
    }

    public final void h() {
        HashMap map = new HashMap();
        g("app/info", n(), map, new n(this, "app/info", map, 2));
    }

    public final String i() {
        boolean z10 = xyz.stream.utils.e.d().f36119h;
        String str = this.f35915a;
        String str2 = this.f35917c;
        if (z10) {
            return str + "_tablet/" + str2;
        }
        if (xyz.stream.utils.e.d().f36121j) {
            return this.f35916b + "_mobile/" + str2;
        }
        return str + "/" + str2;
    }

    public final void j() {
        HashMap mapO = u1.o("key", "all");
        g("config/key", n(), mapO, new n(this, "config/key", mapO, 1));
    }

    public final void k() {
        if (D()) {
            return;
        }
        HashMap map = new HashMap();
        map.put("role_id", f0.o().q("flutter.selectRoles", ""));
        g("user/dislike", n(), map, new n(this, "user/dislike", map, 0));
    }

    public final void l(int i10, int i11, e eVar) {
        HashMap map = new HashMap();
        map.put("video_id", String.valueOf(i11));
        map.put("series_id", String.valueOf(i10));
        g("video/episodes/sources", n(), map, new j(this, "video/episodes/sources", map, eVar, 19));
    }

    public final List m(int i10, Integer num, Integer num2) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        l lVar = new l(atomicReference, countDownLatch, i10);
        String str = "video/episodes/list";
        HashMap map = new HashMap();
        map.put("series_id", String.valueOf(i10));
        if (num2 != null) {
            map.put("season_number", String.valueOf(num2));
        }
        if (num != null) {
            map.put("video_id", String.valueOf(num));
        }
        g("video/episodes/list", n(), map, new j(this, str, map, lVar, 18));
        try {
            countDownLatch.await();
            return (List) atomicReference.get();
        } catch (InterruptedException e10) {
            xyz.stream.utils.h.c(e10.getMessage());
            return null;
        }
    }

    public final HashMap n() {
        Signature[] signatureArr;
        Signature[] apkContentsSigners;
        HashMap mapO = u1.o(RtspHeaders.CONTENT_TYPE, "application/json");
        xyz.stream.utils.e.d().getClass();
        mapO.put("Accept-Language", xyz.stream.utils.e.e());
        mapO.put("device-id", xyz.stream.utils.e.d().b());
        mapO.put("mac", kotlinx.coroutines.flow.h.v());
        mapO.put("app", i());
        Context context = xyz.stream.utils.e.d().f36112a;
        String strZ = null;
        if (context != null) {
            try {
                PackageManager packageManager = context.getPackageManager();
                String packageName = context.getPackageName();
                if (Build.VERSION.SDK_INT >= 28) {
                    PackageInfo packageInfo = packageManager.getPackageInfo(packageName, C.BUFFER_FLAG_FIRST_SAMPLE);
                    if (packageInfo != null && packageInfo.signingInfo != null && (apkContentsSigners = packageInfo.signingInfo.getApkContentsSigners()) != null && apkContentsSigners.length != 0) {
                        strZ = kotlinx.coroutines.y.Z(apkContentsSigners[0].toByteArray());
                    }
                } else {
                    PackageInfo packageInfo2 = packageManager.getPackageInfo(packageName, 64);
                    if (packageInfo2 != null && (signatureArr = packageInfo2.signatures) != null && signatureArr.length != 0) {
                        strZ = kotlinx.coroutines.y.Z(signatureArr[0].toByteArray());
                    }
                }
            } catch (Exception unused) {
                xyz.stream.utils.f.p().y("XyzStream", "get app signature error");
            }
        }
        mapO.put("x-sign-256", strZ);
        mapO.put("app-version", xyz.stream.utils.e.d().f36113b);
        return mapO;
    }

    public final void o(int i10, String str, xyz.netfly.ui.live.epg.k kVar) {
        String str2 = "live/channels/epgs";
        HashMap map = new HashMap();
        map.put("channelId", String.valueOf(i10));
        xyz.stream.utils.e.d().getClass();
        map.put(IjkMediaMeta.IJKM_KEY_LANGUAGE, xyz.stream.utils.e.f());
        map.put("timezone", kotlinx.coroutines.flow.h.D());
        if (str != null && !str.isEmpty()) {
            map.put("date", str);
        }
        g("live/channels/epgs", n(), map, new j(this, kVar, str2, map, 5));
    }

    public final void q(List list, int i10, List list2, e eVar) {
        t(1, 20, i10, 0, null, null, (list == null || list.isEmpty()) ? null : kotlinx.coroutines.y.L(",", list), "release_date", null, null, list2, null, eVar);
    }

    public final void r(String str, boolean z10, xyz.stream.api.viewmodel.l lVar) {
        String str2 = "sports/football/leagues/matchs";
        HashMap map = new HashMap();
        map.put("type", z10 ? "all" : "preview");
        if (str != null) {
            map.put("sportType", str);
        }
        map.put("tz", kotlinx.coroutines.flow.h.D());
        g("sports/football/leagues/matchs", n(), map, new m(this, str2, map, lVar, 0));
    }

    public final void s(int i10, int i11, e eVar) {
        HashMap map = new HashMap();
        map.put("video_id", String.valueOf(i10));
        map.put("source_type", String.valueOf(i11));
        g("video/info", n(), map, new j(this, eVar, "video/info", map, 13));
    }

    public final void t(int i10, int i11, int i12, int i13, String str, String str2, String str3, String str4, String str5, String str6, List list, Integer num, e eVar) {
        String str7 = "video/list";
        HashMap map = new HashMap();
        map.put("page", String.valueOf(i10));
        map.put("page_size", String.valueOf(i11));
        if (i12 > 0) {
            map.put("type", String.valueOf(i12));
        }
        if (i13 > 0) {
            map.put("network", String.valueOf(i13));
        }
        if (!TextUtils.isEmpty(str)) {
            map.put("audios_type", str);
        }
        if (!TextUtils.isEmpty(str2) && !str2.equalsIgnoreCase("all")) {
            map.put("region", str2);
        }
        if (str3 != null && !str3.isEmpty() && !str3.equals(SessionDescription.SUPPORTED_SDP_VERSION) && !str3.equals("null") && !str3.equals("-1")) {
            map.put("tag_ids", str3);
        }
        if (!TextUtils.isEmpty(str4)) {
            map.put("sort", str4);
        }
        if (str5 != null) {
            map.put("year_max", str5);
        }
        if (str6 != null) {
            map.put("year_min", str6);
        }
        if (num != null) {
            map.put("person_id", String.valueOf(num));
        }
        if (list != null && !list.isEmpty()) {
            ArrayList arrayList = new ArrayList();
            Iterator it = list.iterator();
            while (it.hasNext()) {
                arrayList.add(String.valueOf((Integer) it.next()));
            }
            map.put("ids", kotlinx.coroutines.y.L(",", arrayList));
        }
        g("video/list", n(), map, new j(this, eVar, str7, map, 14));
    }

    public final List u(int i10, int i11) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference atomicReference = new AtomicReference();
        f().l(i10, i11, new c(this, atomicReference, countDownLatch, 1));
        try {
            countDownLatch.await();
            return (List) atomicReference.get();
        } catch (InterruptedException e10) {
            xyz.stream.utils.h.c(e10.getMessage());
            return null;
        }
    }

    public final void w(int i10) {
        if (D()) {
            return;
        }
        HashMap map = new HashMap();
        map.put("channel_id", Integer.valueOf(i10));
        map.put("role_id", f0.o().q("flutter.selectRoles", ""));
        v("user/live_channel/favorite", n(), xyz.stream.utils.m.a(map), new n(this, "user/live_channel/favorite", map, 7));
    }

    public final void x(int i10) {
        if (D()) {
            return;
        }
        HashMap map = new HashMap();
        map.put("channel_id", Integer.valueOf(i10));
        map.put("role_id", f0.o().q("flutter.selectRoles", ""));
        v("user/live_channel/history", n(), xyz.stream.utils.m.a(map), new n(this, "user/live_channel/history", map, 5));
    }

    public final void y(int i10) {
        HashMap map = new HashMap();
        map.put("channel_id", Integer.valueOf(i10));
        v("live/channels/play", n(), xyz.stream.utils.m.a(map), new rf.u(this));
    }

    public final void z(int i10, int i11) {
        HashMap map = new HashMap();
        u1.t(i10, map, "tmdb_id", i11, "type");
        v("play", n(), xyz.stream.utils.m.a(map), new i(this));
    }
}
